2010-12-14 106 views
0

我正在嘗試發佈鍵盤事件以觸發空間更改。發佈鍵盤事件

默認情況下,你可以改變空間與控制鍵+你想要改變的空間的數字鍵,這裏是我使用的,但不工作的代碼:

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); 

回答

2

OK,發現解決方案中,發佈事件的正確代碼是:

CGEventPost(kCGHIDEventTap, spaceDownEvent);