我想将给定的一组System.Windows.Forms.Keys
和 a翻译System.Windows.Forms.InputLanguage
成相应的System.Char
.
尝试了一些实验MapVirtualKeyEx
,但现在有办法考虑键盘状态,而且死键ToUnicodeEx
很痛苦。
我的目标是一个函数......
static char? FromKeys(Keys keys, InputLanguage inputLanguage)
{
// As I think what can be helpful and I got trying to find a solution for this problem:
Keys vkCode = keys & Keys.KeyCode;
Keys modifiers = keys & Keys.Modifiers;
byte[] keyboardState = new byte[256];
keyboardState[vkCode] = 1 << 7;
if (modifiers.HasFlag(Keys.Shift))
{
keyboardState[(int)Keys.ShiftKey] = 1 << 7;
}
if (modifiers.HasFlag(Keys.Control))
{
keyboardState[(int)Keys.ControlKey] = 1 << 7;
}
if (modifiers.HasFlag(Keys.Alt))
{
keyboardState[(int)Keys.Menu] = 1 << 7;
}
// [Put your code here]
}
...应该这样称呼:
FromKeys(Keys.A | Keys.Shift, InputLanguage.CurrentInputLanguage); // = 'A'
FromKeys(Keys.Escape, InputLanguage.DefaultInputLanguage); // = null
FromKeys(Keys.Oemtilde, InputLanguage.FromCulture(new CultureInfo("de-DE"))); // = 'ö'
FromKeys(Keys.E | Keys.Control | Keys.Alt, InputLanguage.FromCulture(new CultureInfo("de-DE"))); // = '€'
(注意Keys.Shift
和Keys.ShiftKey
!)
这个功能会是什么样子?
我将选择的答案应该提供一种将带有修饰符( , , )System.Windows.Forms.Keys
的可选组合的“正常”转换为 a 的方法。多次连续调用必须产生相同的字符。没有硬编码的键盘布局,定义了要使用的布局。Keys.Shift
Keys.Alt
Keys.Control
System.Char
InputLanguage
作为帮助理解我想要做的事情:KeyPressEventArgs
仅使用给定的KeyEventArgs
(和正确的InputLangauge
)生成一个。
死键不应该影响FromKeys
!