当用户点击某个键盘键(CapsLock)时,我以编程方式生成鼠标单击。 因此,当CapsLock打开时我向左鼠标按下,然后在CapsLock关闭时向左鼠标向上。
这行为正确,例如,如果我将鼠标放在窗口标题栏上,单击CapsLock,然后移动鼠标,然后单击CapsLock,窗口正确移动。即我正确地“拖动”窗口,就像我在移动鼠标时按住鼠标左键一样。
然而,有一个问题 - 当我移动鼠标时窗口不移动,它仅在我第二次点击CapsLock后移动到最终位置。即在我'释放'鼠标按钮之后。
我需要做些什么才能确保在鼠标移动过程中刷新屏幕?
有趣的是,我也迷上了
[NSEvent addGlobalMonitorForEventsMatchingMask:NSLeftMouseDraggedMask
发现我的NSLog语句只在之后输出我发布了鼠标左键(真正的鼠标左键)
鼠标点击代码如下,我可以在必要时发布所有代码,但没有多少代码..
// simulate mouse down
// get current mouse pos
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventSetType(theEvent, kCGEventLeftMouseDown);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
// simulate mouse up
// get current mouse pos
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseUp, point, kCGMouseButtonLeft);
CGEventSetType(theEvent, kCGEventLeftMouseUp);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
答案 0 :(得分:7)
如果您希望能够拖动窗口,问题是您还需要发布LeftMouseDragged
事件。
只需致电beginEventMonitoring
即可开始侦听大写锁定键事件和鼠标移动事件。事件处理程序将按照您的需要模拟鼠标左键和移动。这是我的博客的链接,您可以在其中下载Xcode 4的完整工作示例:http://www.jakepetroules.com/2011/06/25/simulating-mouse-events-in-cocoa
该示例位于公共领域,随心所欲地做任何事情。 :)
根据Apple(NSEvent documentation),需要在“系统偏好设置”>中检查“启用辅助设备的访问权限”。通用访问为此工作,但我没有检查它,这不是一个问题。只是一个抬头。
如果您有任何其他问题,请告诉我,我会尽力帮助您。
// Begin listening for caps lock key presses and mouse movements
- (void)beginEventMonitoring
{
// Determines whether the caps lock key was initially down before we started listening for events
wasCapsLockDown = CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, kVK_CapsLock);
capsLockEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSFlagsChangedMask) handler: ^(NSEvent *event)
{
// Determines whether the caps lock key was pressed and posts a mouse down or mouse up event depending on its state
bool isCapsLockDown = [event modifierFlags] & NSAlphaShiftKeyMask;
if (isCapsLockDown && !wasCapsLockDown)
{
[self simulateMouseEvent: kCGEventLeftMouseDown];
wasCapsLockDown = true;
}
else if (wasCapsLockDown)
{
[self simulateMouseEvent: kCGEventLeftMouseUp];
wasCapsLockDown = false;
}
}];
mouseMovementEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSMouseMovedMask) handler:^(NSEvent *event)
{
[self simulateMouseEvent: kCGEventLeftMouseDragged];
}];
}
// Cease listening for caps lock key presses and mouse movements
- (void)endEventMonitoring
{
if (capsLockEventMonitor)
{
[NSEvent removeMonitor: capsLockEventMonitor];
capsLockEventMonitor = nil;
}
if (mouseMovementEventMonitor)
{
[NSEvent removeMonitor: mouseMovementEventMonitor];
mouseMovementEventMonitor = nil;
}
}
-(void)simulateMouseEvent:(CGEventType)eventType
{
// Get the current mouse position
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint mouseLocation = CGEventGetLocation(ourEvent);
// Create and post the event
CGEventRef event = CGEventCreateMouseEvent(CGEventSourceCreate(kCGEventSourceStateHIDSystemState), eventType, mouseLocation, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
}