1

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

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?

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

Thanks in advance!

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Geesu
  • 5,928
  • 11
  • 43
  • 72

1 Answers1

1

I think its the same question as How to convert ASCII character to CGKeyCode?

Community
  • 1
  • 1
Chanok
  • 790
  • 6
  • 23