简体   繁体   English

键盘检测错误(C#)

[英]Bad detection of my keyboard(C#)

Why when i click for example "a" on my keyboard program seems bad key "F8". 为什么当我在键盘程序上单击“ a”时似乎出现了错误的键“ F8”。 Diffrent examples: 不同的例子:

"1" = "NumPad7"   
"A" =  "V"
"B" = "W"

Problem is in: 问题出在:

label.Content = (Key)key;

When i change (Key) for (char) it works but only for az and AZ keys. 当我为(char)更改(Key)时,它可以工作,但仅适用于z和AZ键。 How can i properly detect my keyboard? 如何正确检测键盘?

    public void Window_KeyDown(object sender, KeyEventArgs e)
    {
        if (condition == true)
        {
            int key;
            int keyState;
            for (key = 0; key < 127; key++)
            {
                keyState = GetAsyncKeyState(key);
                if (keyState == 1 || keyState == -32767)
                {
                    if(Keyboard.IsKeyDown(Key.LeftShift) || 
                     Keyboard.IsKeyDown(Key.RightShift ))
                    {
                        Save_With_Big_Letters(key);
                        label.Content = (Key)key;
                    }
                    else
                    {
                        Save_With_Small_Letters(key);
                        label.Content = (Key)(key + 32);
                    }
                }

            }
        }     
    }

GetAsyncKeyState uses the older, WindowsForms key values. GetAsyncKeyState使用较旧的WindowsForms键值。 To make this work, either use System.Windows.Forms.Keys enum, or just use the KeyEventArgs.Key value. 为此,可以使用System.Windows.Forms.Keys枚举,也可以只使用KeyEventArgs.Key值。

Like this: 像这样:

label.Content = (System.Windows.Forms.Keys)key;

or the better way: 或更好的方法:

label.Content = e.Key;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM