Key Press or Holdkey in Cocoa

Discussion in 'Mac Programming' started by MorphingDragon, May 1, 2010.

  1. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #1
    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?
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    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.
     
  3. MorphingDragon thread starter macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #3
    I was hoping to have a solution more suited for real time interaction. I'll just have to keep looking.

    Thanks Though
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     

Share This Page