Search code examples
c#winformssendkeysalt

Sendkeys.Send() for right alt key? any alternatives?


I am working on a winform app for a touch screen monitor. The app consists of a web browser and a on screen keyboard. I have most everything I need, but the problem I am facing is that there are two input languages, English and Korean. Anyone familiar with using two languages can tell you that the right alt key is used to go back and forth between languages. I need to simulate this keystroke, but I can't find anything for it.

I have found ways to simulate left/right shift keys, and left/right ctrl keys. But nothing for left/right alt keys.

Do I have any alternatives?


Solution

  • You can use keybd_event with RALT keycode VK_RMENU. A complete list of keycodes are here

    You would have to P/Invoke the keystroke like this:

    [DllImport("user32.dll", SetLastError = true)] 
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);  
    
    public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag 
    public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag 
    public const int VK_RMENU = 0xA5;
    
    keybd_event(VK_RMENU, 0, KEYEVENTF_EXTENDEDKEY, 0); 
    keybd_event(VK_RMENU, 0, KEYEVENTF_KEYUP, 0);