Thursday, March 9, 2017

CGEventPost - hold a key (shift)

Leave a Comment

I'm looking for a way to design a little panel with modifier keys on it (shift, command for example) and have to possibility to click on it like a virtual keyboard.

I'd like it to have this behavior :

  • click on the virtual key (shift).
  • the shift button holds, and keeps being pressed.
  • type something with my standard keyboard.
  • click another time on the virtual shift key to release it.

here is the code I'm using :

CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); CGEventRef shiftKeyDown = CGEventCreateKeyboardEvent(source, (CGKeyCode)56, YES); CGEventRef shiftKeyUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)56, NO);  CGEventPost(kCGAnnotatedSessionEventTap, shiftKeyDown); CGEventPost(kCGAnnotatedSessionEventTap, shiftKeyUp);  CFRelease(shiftKeyUp); CFRelease(shiftKeyDown); CFRelease(source); 

I can't find a way to keep it pressed until I click on it another time. I though "Push On Push Off" Button Cell type was the key but unfortunately no. :-)

any help ?

thanks in advance.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment