我想限制鼠标移动到OS X 10.11屏幕上的特定矩形区域。我修改了一些来自MouseTools(下面)的代码来做到这一点,但是当你点击屏幕边缘时它很紧张。我怎样才能摆脱这种抖动?限制鼠标移动到OS X中没有抖动的区域
// gcc -Wall constrain.cpp -framework ApplicationServices -o constrain
#include <ApplicationServices/ApplicationServices.h>
int main (int argc, const char * argv[]) {
if(argc != 5) {
printf("Usage: constrain left top right bottom\n");
return 0;
}
int leftBound = strtol(argv[1], NULL, 10);
int topBound = strtol(argv[2], NULL, 10);
int rightBound = strtol(argv[3], NULL, 10);
int bottomBound = strtol(argv[4], NULL, 10);
CGEventTapLocation tapLocation = kCGHIDEventTap;
CGEventSourceRef sourceRef = CGEventSourceCreate(kCGEventSourceStatePrivate);
while(true) {
CGEventRef mouseEvent = CGEventCreate(NULL);
CGPoint mouseLoc = CGEventGetLocation(mouseEvent);
int x = mouseLoc.x, y = mouseLoc.y;
if(x < leftBound || x > rightBound || y < topBound || y > bottomBound) {
if(x < leftBound) x = leftBound;
if(x > rightBound) x = rightBound;
if(y < topBound) y = topBound;
if(y > bottomBound) y = bottomBound;
CGEventRef moveMouse = CGEventCreateMouseEvent(sourceRef, kCGEventMouseMoved, CGPointMake(x, y), 0);
CGEventPost(tapLocation, moveMouse);
CFRelease(moveMouse);
}
CFRelease(mouseEvent);
usleep(8*1000); // 8ms, ~120fps
}
CFRelease(sourceRef);
return 0;
}
这里有一些其他的事情我想:Receiving, Filtering, and Modifying Mouse Events演示如何创建使用CGEventTapCreate
鼠标移动回调。在代码中它说:“我们可以改变鼠标事件的各个方面,例如,我们可以使用CGEventSetLocation(event, newLocation)
。”不幸的是,这实际上并没有改变鼠标位置(完整示例here)。我能够改变鼠标位置的唯一方法是做CGEventPost
或CGWarpMouseCursorPosition
。我还尝试使用 kCGHIDEventTap
而不是kCGSessionEventTap
,并且检查了我为应用程序启用了超级用户权限和辅助功能。它似乎应该工作,因为有modifying key presses正常工作的另一个例子。而不是使用CGEventSetLocation
我也尝试使用CGEventSetIntegerValueField
(如在键盘示例中)将x和y偏移量设置为0,但这并没有改变任何东西。
最后,我也试过用和CGWarpMouseCursorPosition
里面的CGEventCallback
,“隐形边框”还是比上面的代码更加紧张。
您的问题的描述是详细的。除非您遇到问题,否则没有人关心您使用AppleScript程序所做的工作。 –
@ElTomato我通过删除解释我对AppleScript的第一次尝试的两个句子以及我对它的问题来减少了冗长的问题。假设你对这个话题有所了解,我很乐意在你提出的问题上提出反馈意见。谢谢! –
谁的鼠标移动将被禁止?任何人使用您的计算机或您的应用程序的用户? –