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

ⅰ亾dé卋堺 提交于 2019-12-13 02:25:31

问题


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?


回答1:


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



回答2:


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?




回答3:


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.




回答4:


MSDN gives the RMenu enum entry for right-alt.



来源:https://stackoverflow.com/questions/9330498/sendkeys-send-for-right-alt-key-any-alternatives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!