开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜