Question

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?

Was it helpful?

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

OTHER TIPS

You don't need to simulate Alt+Shift keystrokes for changing input language. You can use InputLanguage class in System.Windows.Forms namespace:

        var currentLang = InputLanguage.CurrentInputLanguage;
        var installedLangs = InputLanguage.InstalledInputLanguages;
        if (installedLangs.Count > 1)
            InputLanguage.CurrentInputLanguage =
                installedLangs[1 - installedLangs.IndexOf(currentLang)];

When you have exactly 2 input languages installed, this code fragments switches between those two. You get the idea. Right?

You don't really want to find a way to send the Alt key, right? What you really want is to be able to change the input language. In that case, go straight to the source.

The Windows API has methods to change the current active keyboard layout. They're not part of the .NET Framework, but you can use P/Invoke to call them from C#.

The MSDN documentation for the ActivateKeyboardLayout is here.

And the P/Invoke signatures can be found here.

MSDN gives the RMenu enum entry for right-alt.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top