0

In macos I have the following code to detect if the user is typing (literally if the user is typing any key):

[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDown
                                       handler:^(NSEvent *event){
                                           NSLog(@"keydown: %@", event.characters);

                                       }];

This code absolutely does not work. Is there an alternative to NSKeyDown for global monitor?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3352739
  • 29
  • 1
  • 7
  • What doesn't work? Does your code reach this method? – koen Apr 01 '18 at 16:42
  • Possible duplicate of [addGlobalMonitorForEventsMatchingMask not working](https://stackoverflow.com/questions/25496336/addglobalmonitorforeventsmatchingmask-not-working) – koen Apr 01 '18 at 16:42
  • thanks guys you are right I had a duplicate function. I had NSKeyDown and I had another NSEvent global monitor functions with NSRightMouseUp. But what if I want to run both at the same time? Can I run both? – user3352739 Apr 01 '18 at 16:52

1 Answers1

0

It looks like you should be using NSEventMaskKeyDown, not NSKeyDown.

So this should work

[NSEvent addGlobalMonitorForEventsMatchingMask: NSEventMaskKeyDown | NSEventMaskRightMouseUp
                                       handler:^(NSEvent *event){
                                           NSLog(@"keydown: %@", event.characters);

                                       }];
Keith Knauber
  • 752
  • 6
  • 13