CGEventPostToPSN-如何使用不同的键盘语言环境发送键? [英] CGEventPostToPSN - How to send keys with different keyboard locales?

查看:129
本文介绍了CGEventPostToPSN-如何使用不同的键盘语言环境发送键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我要将按键发送到另一个应用程序,例如:

So I'm sending key presses to another application, like so:

- (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

Basically, I can send keys FINE if they are using a US Keyboard, such as sending: kVK_ANSI_A

问题在于非美式键盘,如果键盘布局未设置为美式,我该如何调整这些键以仍然正确发送?

The problem is with non-US keyboards, how can I adapt these keys to still send correctly if the keyboard layout is NOT set to US?

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

Here are the virtual key codes I'm having trouble with: http://pastebin.com/qXnXHb5M

提前谢谢!

推荐答案

我认为它与如何将ASCII字符转换为CGKeyCode?

这篇关于CGEventPostToPSN-如何使用不同的键盘语言环境发送键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆