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 :
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.
A shift key virtually pressed that way, will get released automatically when followed by an event which doesn't contain a shift flag. This seems to be a limitation, or possibly a bug, considering the documentation sort of indicates otherwise: https://developer.apple.com/reference/coregraphics/cgevent/1456564-init#discussion
The only way I found to achieve what you are looking for, is to setup an event listener, and add the shift flag to every event which should be modified by the Shift key.
Example on how to listen to keyboard events: https://stackoverflow.com/a/31898592/1445812 (Swift)
Example on how to had the shift flag to intercepted events:
event?.flags.insert(.maskShift)
Hope this helps. If anyone knows how to do the same without having to add the flags to every event, please do tell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With