Cocoa/Carbon: catch global hotkey when key is kept pressed (held down)

1.4k Views Asked by At

Using InstallApplicationEventHandler and RegisterEventHotKey from Carbon framework, I'm able to catch whatever key I want.

On the contrary I can't find a way to handle correctly the event when a key is held down (kept pressed). I mean, when I held down a key, RegisterEventHotKey responds like if I have pressed and then released the key; I want it, instead, to send the "event" continuously until the key is released.

What I really want to achieve, to be precise, when a key is kept pressed is this:

as soon as the key is pressed and held down I want that InstallApplicationEventHandler and RegisterEventHotKey create an event but not only once (as I have now) but every x milliseconds or so until the key is released.

The particular keys that I'm considering are not modifier keys.

Can you help me? I'm really becoming crazy on this!

2

There are 2 best solutions below

0
Dave DeLong On BEST ANSWER

Maybe start firing a timer every 0.25 seconds or something that checks to see if the appropriate keys are still down. If they are, re-invoke the action. Otherwise, invalidate the timer.

6
Nicholas Riley On

You need to use an event tap; RegisterEventHotKey isn't flexible enough to do what you want. Check out Event Taps Testbench and the documentation.