Reputation: 33
I'm trying to post a keyboard event to trigger a Space change.
By default you can change Spaces with the control key + the number key for the space you wish to change to, here's the code I'm using but isn't working:
int spaceToChangeTo = 3;
int keyCodeForSpace = space + 17; // keycode 18 = number 1 on the keyboard, and onwards.
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef spaceDownEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode)keyCodeForSpace, YES);
CGEventSetFlags(spaceDownEvent, kCGEventFlagMaskControl);
CGEventRef spaceUpEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode)keyCodeForSpace, NO);
CGEventPost(kCGAnnotatedSessionEventTap, spaceDownEvent);
CGEventPost(kCGAnnotatedSessionEventTap, spaceUpEvent);
CFRelease(spaceDownEvent);
CFRelease(spaceUpEvent);
CFRelease(source);
Upvotes: 0
Views: 446
Reputation: 33
OK, found the solution, where posting the event the correct code is:
CGEventPost(kCGHIDEventTap, spaceDownEvent);
Upvotes: 2