Use CGEventPost instead of HIDEvent to make it work during sleep
krackers opened this issue · 0 comments
krackers commented
Thanks for this project, and great reverse engineering work!
One improvement: currently the simulated keyboard events are not sent if the display goes to sleep. In fact, it doesn't even wake the computer like it does when the keyboard is normally pressed during sleep. I'm not 100% sure why this is, but it seems like IOHID has some logic about consuming keypresses if display is sleep. There are also other reports of IOHIDPostEvent not working in sleep.
The fix is simple, use CGEventPost, which also ends up making the code a lot shorter
// Simulate the input of a keystroke based on an associated keycode
static void HIDPostAuxKey(int key) {
// create and send down key event
NSEvent* key_event = [NSEvent otherEventWithType:NSSystemDefined location:CGPointZero modifierFlags:0xa00 timestamp:0 windowNumber:0 context:0 subtype:8 data1:((key << 16) | (0xa << 8)) data2:-1];
CGEventPost(0, key_event.CGEvent);
// create and send up key event
key_event = [NSEvent otherEventWithType:NSSystemDefined location:CGPointZero modifierFlags:0xb00 timestamp:0 windowNumber:0 context:0 subtype:8 data1:((key << 16) | (0xb << 8)) data2:-1];
CGEventPost(0, key_event.CGEvent);
}
With this, things work as expected.