我这里有一些代码,把鼠标限制在屏幕上的一个区域,它工作得比较好,只有一个大问题。当鼠标沿着区域边缘运行时,移动并不干净/流畅,而是以一种非常剧烈的方式跳跃,我相信这可能是由于CGWarpMouseCursorPosition造成了每一个“翘曲”的延迟。
可以告诉任何人,是我代码中的什么东西导致了这种延迟,还是实际上是鼠标扭曲函数造成的。如果是鼠标扭曲函数,有什么方法可以让鼠标顺利地重新定位吗?,我在flash中做了同样的事情,它运行得非常完美,我知道这个循环不需要花费太多的时间来执行,因为它只运行了4或5次。
CGEventRef
mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
CGPoint point = CGEventGetLocation(event);
float tX = point.x;
float tY = point.y;
if( tX <= 700 && tX >= 500 && tY <= 800 && tY >= 200){
// target is inside O.K. area, do nothing
}else{
CGPoint target;
//point inside restricted region:
float iX = 600; // inside x
float iY = 500; // inside y
// delta to midpoint between iX,iY and tX,tY
float dX;
float dY;
float accuracy = .5; //accuracy to loop until reached
do {
dX = (tX-iX)/2;
dY = (tY-iY)/2;
if((tX-dX) <= 700 && (tX-dX) >= 500 && (tY-dY) <= 800 && (tY-dY) >= 200){
iX += dX;
iY += dY;
} else {
tX -= dX;
tY -= dY;
}
} while (abs(dX)>accuracy || abs(dY)>accuracy);
target = CGPointMake(roundf(tX), roundf(tY));
CGWarpMouseCursorPosition(target);
}
return event;
}
int
main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CGEventMask event_mask;
event_mask = CGEventMaskBit(kCGEventMouseMoved) | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged) | CGEventMaskBit(kCGEventOtherMouseDragged);
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0, event_mask, mouse_filter, NULL);
if (!eventTap) {
NSLog(@"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
发布于 2013-07-09 21:58:49
正如您所发现的,CGSetLocalEventsSuppressionInterval
解决了您的问题。
然而,它在10.6岁时就被否决了。苹果公司的文档声明:
此功能不推荐用于一般用途,因为没有记录的特殊情况和不良副作用。建议将此函数替换为
CGEventSourceSetLocalEventsSuppressionInterval
,它允许针对特定事件源调整抑制间隔,只影响使用该事件源发布的事件。
不幸的是,替代的CGEventSourceSetLocalEventsSuppressionInterval
不适用于CGWarpMouseCursorPosition
移动。
相反,在翘曲之后立即使用CGAssociateMouseAndMouseCursorPosition(true)
:
CGPoint warpPoint = CGPointMake(42, 42);
CGWarpMouseCursorPosition(warpPoint);
CGAssociateMouseAndMouseCursorPosition(true);
文档没有提到这种行为,但它似乎在翘曲之后取消了抑制间隔。
发布于 2018-08-10 13:14:13
我可以让CGEventSourceSetLocalEventsSuppressionInterval
以下列方式使用CGWarpMouseCursorPosition
:
auto eventSourceRef = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventSourceSetLocalEventsSuppressionInterval(eventSourceRef, 0);
这在CGWarpMouseCursorPosition
中很好(不被反对),而CGAssociateMouseAndMouseCursorPosition(true)
对我没有任何帮助。
发布于 2011-11-24 03:36:56
您可能正在寻找CGSetLocalEventsSuppressionInterval(),这是一个在10.6岁时被废弃的方法..。但在10.7中,它仍然有效。
https://stackoverflow.com/questions/8215413
复制相似问题