Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MorphingDragon

macrumors 603
Original poster
Mar 27, 2009
5,159
6
The World Inbetween
I looked on Google but I'm not sure if I'm searching the right thing.

In Cocoa, is there anything that would detect if a key on the keyboard is being pressed or held down?
 
One possibility: Set a boolean to indicate when the key is pressed and released; when you set it to pressed, start an NSTimer with a short delay. If the key is still pressed when the timer fires, treat it as holding it down.
 
One possibility: Set a boolean to indicate when the key is pressed and released; when you set it to pressed, start an NSTimer with a short delay. If the key is still pressed when the timer fires, treat it as holding it down.

I was hoping to have a solution more suited for real time interaction. I'll just have to keep looking.

Thanks Though
 
I was hoping to have a solution more suited for real time interaction. I'll just have to keep looking.

Key press and key release events. Also modifier change events.

If you get a key press, then the key is down until you get a key release event.

If you get a modifier change event, then one of the modifier keys changed.

For single keys and modifiers, there's CGEventSourceKeyState() and CGEventSourceFlagsState().

There's also the legacy GetKeys() function that returns an array of 128 bits, where 1's represent a pressed key. I don't know if that function still works.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.