How to detect all cursor movements and key presses on a Mac?
What is the best method to detect all cursor movements and key presses on a Mac us开发者_StackOverflow社区ing objective-c on OSX Lion?
I did it by installing an event monitor
- (void)monitorEvents
{
// Monitor all events
NSUInteger eventMasks = NSLeftMouseDownMask | NSRightMouseDownMask | NSMouseMovedMask | NSScrollWheelMask | NSKeyDownMask | NSMouseMovedMask | NSEventTypeBeginGesture | NSEventTypeEndGesture;
eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:eventMasks handler:^(NSEvent *incomingEvent)
{
NSEvent *result = incomingEvent;
return result;
}];
}
Typically, you would subclass NSApplication
and override the -sendEvent:
method.
精彩评论