Search code examples
c#.netkeyboard-eventssendkeys

ALT key keyboard event is not working in all keyboard layout?


I am trying to send characters to an application using keyboard events. i want to press alt key and then release it. I am using the following code to do this.

const int VK_ALT = 0x12;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;

keybd_event((byte)VK_ALT, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event((byte)VK_ALT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

But the alt key is working only in English US layout keyboard and not in other keyboards. I don't know why this is happening.How can i make it independent of keyboard layout


Solution

  • It may have something to do with your second parameter (the scan code) being 0.

    For 'ALT' the hardware 'Make Code' is 56 or 0x38, and the 'Break Code' is 184 or 0xB8. I've seen applications use either of these for key down/up. My keyboard seems to send 0x38 for both.

    Also note the EXTENDEDKEY of 0x0001 signifies that you are using the "Right ALT" whereas 0x0000 is for "Left ALT" (just in case that makes a difference for your application).