CGEventPostToPSN - 如何使用不同的键盘区域设置发送密钥?

时间:2011-06-02 17:28:17

标签: objective-c xcode operating-system

所以我将按键发送到另一个应用程序,如:

- (void)keyPress:(int)hotkey withModifier:(unsigned int)modifier withKeyDown:(BOOL)keyDown{


// verify the process still exists
ProcessSerialNumber psn = [self myPSN];
CGEventRef keyEvent = NULL;

// create our source
CGEventSourceRef source = NULL; // this didn't used to be NULL, does this matter?
if ( source || 1 == 1 ){

    keyEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode)hotkey, keyDown);

    // set flags for the event (does this even matter? No.)
    CGEventSetFlags( keyEvent, modifier );

    // hit any specified modifier keys
    if ( modifier & NSAlternateKeyMask ){
        PostKeyboardEvent( source, kVK_Option, keyDown );
    }
    if ( modifier & NSShiftKeyMask ){
        PostKeyboardEvent( source, kVK_Shift, keyDown );
    }
    if ( modifier & NSControlKeyMask ){
        PostKeyboardEvent( source, kVK_Control, keyDown );
    }
    if ( modifier & NSCommandKeyMask ){
        PostKeyboardEvent( source, kVK_Command, keyDown );
    }

    // post the actual event
    CGEventPostToPSN(&psn, keyEvent);
    usleep(30000);

    // post it again if we're doing key up, just in case!
    if ( !keyDown ){
        CGEventPostToPSN(&psn, keyEvent);
    }

    // release
    if ( keyEvent ){
        CFRelease(keyEvent);
    }
}
}

基本上,如果使用美式键盘,我可以发送FINE键,例如发送:kVK_ANSI_A

问题出在非美国键盘上,如果键盘布局未设置为美国,如何才能使这些键仍能正确发送?

以下是我遇到问题的虚拟键码:http://pastebin.com/qXnXHb5M

提前致谢!

1 个答案:

答案 0 :(得分:1)

我认为它与How to convert ASCII character to CGKeyCode?

的问题相同