获取键盘按键事件并将键名写在标签上
Get the Keyboard Keydown event and Write the key name on a Label
我的要求是写keyup事件,写在标签上,
我已经为它开发了功能,但它没有效率或优化,我需要一些帮助来优化代码,以及一些方向来继续。
我是 c# 的新手,所以请多多包涵,谢谢。
我将包含代码供您参考。
private void Form1_KeyUp(object sender, KeyEventArgs e)
label1.Text = (Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)) ? "LShift" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)) ? "RShift" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.LControlKey)) ? "Lcontrol" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.RControlKey)) ? "Rcontrol" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.LMenu)) ? "L Alt" : "")
+ (e.KeyCode == Keys.Escape ? "Ëscape" : "")
+ (e.KeyCode == Keys.Enter ? "Return" : "")
+ (e.KeyCode == Keys.CapsLock ? "CapsLock" : "")
+ (e.KeyCode == Keys.LWin ? "Windows key" : "")
+ (e.KeyCode == Keys.D0 ? " D0 " : "")
+ (e.KeyCode == Keys.D1 ? " D1 " : "")
+ (e.KeyCode == Keys.D2 ? " D2 " : "")
+ (e.KeyCode == Keys.D3 ? " D3 " : "")
+ (e.KeyCode == Keys.D4 ? " D4 " : "")
+ (e.KeyCode == Keys.D5 ? " D5 " : "")
+ (e.KeyCode == Keys.D6 ? " D6 " : "")
+ (e.KeyCode == Keys.D7 ? " D7 " : "")
+ (e.KeyCode == Keys.D8 ? " D8 " : "")
+ (e.KeyCode == Keys.D9 ? " D9 " : "")
+ (e.KeyCode == Keys.NumPad0 ? "Numpad0 " : "")
+ (e.KeyCode == Keys.NumPad1 ? "Numpad1 " : "")
+ (e.KeyCode == Keys.NumPad2 ? "Numpad2 " : "")
+ (e.KeyCode == Keys.NumPad3 ? "Numpad3 " : "")
+ (e.KeyCode == Keys.NumPad4 ? "Numpad4 " : "")
+ (e.KeyCode == Keys.NumPad5 ? "Numpad5 " : "")
+ (e.KeyCode == Keys.NumPad6 ? "Numpad6 " : "")
+ (e.KeyCode == Keys.NumPad7 ? "Numpad7 " : "")
+ (e.KeyCode == Keys.NumPad8 ? "Numpad8 " : "")
+ (e.KeyCode == Keys.NumPad9 ? "Numpad9 " : "")
+ (e.KeyCode == Keys.A ? " A" : "")
+ (e.KeyCode == Keys.B ? " B" : "")
+ (e.KeyCode == Keys.C ? " C" : "")
+ (e.KeyCode == Keys.D ? " D" : "")
+ (e.KeyCode == Keys.E ? " E" : "")
+ (e.KeyCode == Keys.F ? " F" : "")
+ (e.KeyCode == Keys.G ? " G" : "")
+ (e.KeyCode == Keys.H ? " H" : "")
+ (e.KeyCode == Keys.I ? " I" : "")
+ (e.KeyCode == Keys.J ? " J" : "")
+ (e.KeyCode == Keys.K ? " K" : "")
+ (e.KeyCode == Keys.L ? " L" : "")
+ (e.KeyCode == Keys.M ? " M" : "")
+ (e.KeyCode == Keys.N ? " N" : "")
+ (e.KeyCode == Keys.O ? " O" : "")
+ (e.KeyCode == Keys.P ? " P" : "")
+ (e.KeyCode == Keys.Q ? " Q" : "")
+ (e.KeyCode == Keys.R ? " R" : "")
+ (e.KeyCode == Keys.S ? " S" : "")
+ (e.KeyCode == Keys.T ? " T" : "")
+ (e.KeyCode == Keys.U ? " U" : "")
+ (e.KeyCode == Keys.V ? " V" : "")
+ (e.KeyCode == Keys.W ? " W" : "")
+ (e.KeyCode == Keys.X ? " X" : "")
+ (e.KeyCode == Keys.Y ? " Y" : "")
+ (e.KeyCode == Keys.Z ? " Z" : "")
+ (e.KeyCode == Keys.Oem1 ? "Semi colon" : "")
+ (e.KeyCode == Keys.Oem2 ? "Slash" : "")
+ (e.KeyCode == Keys.Oem3 ? "Left quote" : "")
+ (e.KeyCode == Keys.Oem4 ? "Open Brackets" : "")
+ (e.KeyCode == Keys.Oem5 ? "Backslash" : "")
+ (e.KeyCode == Keys.Oem6 ? "Close Brackets" : "")
+ (e.KeyCode == Keys.Oem7 ? "Double quotes" : "")
+ (e.KeyCode == Keys.Oem8 ? "Oem8" : "")
+ (e.KeyCode == Keys.OemPeriod ? "period" : "")
+ (e.KeyCode == Keys.Oemcomma ? "comma" : "")
+ (e.KeyCode == Keys.OemMinus ? "Oem minus" : "")
+ (e.KeyCode == Keys.Oemplus ? "Oem equals" : "")
+ (e.KeyCode == Keys.NumLock ? "NumLock" : "")
+ (e.KeyCode == Keys.Add ? "Add" : "")
+ (e.KeyCode == Keys.Subtract ? "Subtract" : "")
+ (e.KeyCode == Keys.Multiply ? "Multiply" : "")
+ (e.KeyCode == Keys.Divide ? "Divide" : "")
+ (e.KeyCode == Keys.Decimal ? "Decimal" : "")
+ (e.KeyCode == Keys.F1 ? "F1 pressed" : "")
+ (e.KeyCode == Keys.F2 ? "F2 pressed" : "")
+ (e.KeyCode == Keys.F3 ? "F3 pressed" : "")
+ (e.KeyCode == Keys.F4 ? "F4 pressed" : "")
+ (e.KeyCode == Keys.F5 ? "F5 pressed" : "")
+ (e.KeyCode == Keys.F6 ? "F6 pressed" : "")
+ (e.KeyCode == Keys.F7 ? "F7 pressed" : "")
+ (e.KeyCode == Keys.F8 ? "F8 pressed" : "")
+ (e.KeyCode == Keys.F9 ? "F9 pressed" : "")
+ (e.KeyCode == Keys.F10 ? "F10 pressed" : "")
+ (e.KeyCode == Keys.F11 ? "F11 pressed" : "")
+ (e.KeyCode == Keys.F12 ? "F12 pressed" : "")
+ (e.KeyCode == Keys.VolumeDown ? "Volume Down" : "")
+ (e.KeyCode == Keys.VolumeUp ? "Volume Up" : "")
+ (e.KeyCode == Keys.VolumeMute ? "Volume Mute" : "")
+ (e.KeyCode == Keys.MediaPlayPause ? "Play / Pause" : "")
+ (e.KeyCode == Keys.MediaNextTrack ? "Play Next" : "")
+ (e.KeyCode == Keys.MediaPreviousTrack ? "Play Previous" : "")
+ (e.KeyCode == Keys.NumLock ? "NumLock" : "")
+ (e.KeyData == Keys.Tab ? "Tab" : "");
大部分情况下使用字符串格式化程序,例如。枚举的 $"{key}" 将起作用。当它们对人类不友好时,则需要更具体的实现 - 正如@Reza Aghaei 所建议的那样。
类似
switch(e.Keycode)
{
case Keys.Oem1:
label1Text = $"{Semi Colon}";
break;
case default:
label1Text = $"{key}";
break;
}
据我所知,按住组合键 - 例如 'Shift' - 是活动中的额外部分,因此也需要查看。
您可以使用 e.KeyCode.ToString()
来简化您的代码。
这将为您提供大多数您想要的键的键 'text'。
但是,对于某些键,您需要使用条件。
示例:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
string keyText = e.KeyCode.ToString();
keyText =
keyText == "Oem1" ? "Semi colon" :
keyText == "Oem2" ? "Slash" :
keyText == "Oem3" ? "Left quote" :
keyText == "Oem4" ? "Open Brackets" :
keyText == "Oem5" ? "Backslash" :
keyText == "Oem6" ? "Close Brackets" :
keyText == "Oem7" ? "Double quotes" :
keyText == "OemPeriod" ? "period" :
keyText == "Oemcomma" ? "comma" :
keyText == "OemMinus" ? "Oem minus" :
keyText == "Oemplus" ? "Oem equals" : keyText;
label1.Text+= keyText;
}
我的要求是写keyup事件,写在标签上,
我已经为它开发了功能,但它没有效率或优化,我需要一些帮助来优化代码,以及一些方向来继续。 我是 c# 的新手,所以请多多包涵,谢谢。 我将包含代码供您参考。
private void Form1_KeyUp(object sender, KeyEventArgs e)
label1.Text = (Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)) ? "LShift" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)) ? "RShift" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.LControlKey)) ? "Lcontrol" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.RControlKey)) ? "Rcontrol" : "")
+ (Convert.ToBoolean(GetAsyncKeyState(Keys.LMenu)) ? "L Alt" : "")
+ (e.KeyCode == Keys.Escape ? "Ëscape" : "")
+ (e.KeyCode == Keys.Enter ? "Return" : "")
+ (e.KeyCode == Keys.CapsLock ? "CapsLock" : "")
+ (e.KeyCode == Keys.LWin ? "Windows key" : "")
+ (e.KeyCode == Keys.D0 ? " D0 " : "")
+ (e.KeyCode == Keys.D1 ? " D1 " : "")
+ (e.KeyCode == Keys.D2 ? " D2 " : "")
+ (e.KeyCode == Keys.D3 ? " D3 " : "")
+ (e.KeyCode == Keys.D4 ? " D4 " : "")
+ (e.KeyCode == Keys.D5 ? " D5 " : "")
+ (e.KeyCode == Keys.D6 ? " D6 " : "")
+ (e.KeyCode == Keys.D7 ? " D7 " : "")
+ (e.KeyCode == Keys.D8 ? " D8 " : "")
+ (e.KeyCode == Keys.D9 ? " D9 " : "")
+ (e.KeyCode == Keys.NumPad0 ? "Numpad0 " : "")
+ (e.KeyCode == Keys.NumPad1 ? "Numpad1 " : "")
+ (e.KeyCode == Keys.NumPad2 ? "Numpad2 " : "")
+ (e.KeyCode == Keys.NumPad3 ? "Numpad3 " : "")
+ (e.KeyCode == Keys.NumPad4 ? "Numpad4 " : "")
+ (e.KeyCode == Keys.NumPad5 ? "Numpad5 " : "")
+ (e.KeyCode == Keys.NumPad6 ? "Numpad6 " : "")
+ (e.KeyCode == Keys.NumPad7 ? "Numpad7 " : "")
+ (e.KeyCode == Keys.NumPad8 ? "Numpad8 " : "")
+ (e.KeyCode == Keys.NumPad9 ? "Numpad9 " : "")
+ (e.KeyCode == Keys.A ? " A" : "")
+ (e.KeyCode == Keys.B ? " B" : "")
+ (e.KeyCode == Keys.C ? " C" : "")
+ (e.KeyCode == Keys.D ? " D" : "")
+ (e.KeyCode == Keys.E ? " E" : "")
+ (e.KeyCode == Keys.F ? " F" : "")
+ (e.KeyCode == Keys.G ? " G" : "")
+ (e.KeyCode == Keys.H ? " H" : "")
+ (e.KeyCode == Keys.I ? " I" : "")
+ (e.KeyCode == Keys.J ? " J" : "")
+ (e.KeyCode == Keys.K ? " K" : "")
+ (e.KeyCode == Keys.L ? " L" : "")
+ (e.KeyCode == Keys.M ? " M" : "")
+ (e.KeyCode == Keys.N ? " N" : "")
+ (e.KeyCode == Keys.O ? " O" : "")
+ (e.KeyCode == Keys.P ? " P" : "")
+ (e.KeyCode == Keys.Q ? " Q" : "")
+ (e.KeyCode == Keys.R ? " R" : "")
+ (e.KeyCode == Keys.S ? " S" : "")
+ (e.KeyCode == Keys.T ? " T" : "")
+ (e.KeyCode == Keys.U ? " U" : "")
+ (e.KeyCode == Keys.V ? " V" : "")
+ (e.KeyCode == Keys.W ? " W" : "")
+ (e.KeyCode == Keys.X ? " X" : "")
+ (e.KeyCode == Keys.Y ? " Y" : "")
+ (e.KeyCode == Keys.Z ? " Z" : "")
+ (e.KeyCode == Keys.Oem1 ? "Semi colon" : "")
+ (e.KeyCode == Keys.Oem2 ? "Slash" : "")
+ (e.KeyCode == Keys.Oem3 ? "Left quote" : "")
+ (e.KeyCode == Keys.Oem4 ? "Open Brackets" : "")
+ (e.KeyCode == Keys.Oem5 ? "Backslash" : "")
+ (e.KeyCode == Keys.Oem6 ? "Close Brackets" : "")
+ (e.KeyCode == Keys.Oem7 ? "Double quotes" : "")
+ (e.KeyCode == Keys.Oem8 ? "Oem8" : "")
+ (e.KeyCode == Keys.OemPeriod ? "period" : "")
+ (e.KeyCode == Keys.Oemcomma ? "comma" : "")
+ (e.KeyCode == Keys.OemMinus ? "Oem minus" : "")
+ (e.KeyCode == Keys.Oemplus ? "Oem equals" : "")
+ (e.KeyCode == Keys.NumLock ? "NumLock" : "")
+ (e.KeyCode == Keys.Add ? "Add" : "")
+ (e.KeyCode == Keys.Subtract ? "Subtract" : "")
+ (e.KeyCode == Keys.Multiply ? "Multiply" : "")
+ (e.KeyCode == Keys.Divide ? "Divide" : "")
+ (e.KeyCode == Keys.Decimal ? "Decimal" : "")
+ (e.KeyCode == Keys.F1 ? "F1 pressed" : "")
+ (e.KeyCode == Keys.F2 ? "F2 pressed" : "")
+ (e.KeyCode == Keys.F3 ? "F3 pressed" : "")
+ (e.KeyCode == Keys.F4 ? "F4 pressed" : "")
+ (e.KeyCode == Keys.F5 ? "F5 pressed" : "")
+ (e.KeyCode == Keys.F6 ? "F6 pressed" : "")
+ (e.KeyCode == Keys.F7 ? "F7 pressed" : "")
+ (e.KeyCode == Keys.F8 ? "F8 pressed" : "")
+ (e.KeyCode == Keys.F9 ? "F9 pressed" : "")
+ (e.KeyCode == Keys.F10 ? "F10 pressed" : "")
+ (e.KeyCode == Keys.F11 ? "F11 pressed" : "")
+ (e.KeyCode == Keys.F12 ? "F12 pressed" : "")
+ (e.KeyCode == Keys.VolumeDown ? "Volume Down" : "")
+ (e.KeyCode == Keys.VolumeUp ? "Volume Up" : "")
+ (e.KeyCode == Keys.VolumeMute ? "Volume Mute" : "")
+ (e.KeyCode == Keys.MediaPlayPause ? "Play / Pause" : "")
+ (e.KeyCode == Keys.MediaNextTrack ? "Play Next" : "")
+ (e.KeyCode == Keys.MediaPreviousTrack ? "Play Previous" : "")
+ (e.KeyCode == Keys.NumLock ? "NumLock" : "")
+ (e.KeyData == Keys.Tab ? "Tab" : "");
大部分情况下使用字符串格式化程序,例如。枚举的 $"{key}" 将起作用。当它们对人类不友好时,则需要更具体的实现 - 正如@Reza Aghaei 所建议的那样。
类似
switch(e.Keycode)
{
case Keys.Oem1:
label1Text = $"{Semi Colon}";
break;
case default:
label1Text = $"{key}";
break;
}
据我所知,按住组合键 - 例如 'Shift' - 是活动中的额外部分,因此也需要查看。
您可以使用 e.KeyCode.ToString()
来简化您的代码。
这将为您提供大多数您想要的键的键 'text'。
但是,对于某些键,您需要使用条件。
示例:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
string keyText = e.KeyCode.ToString();
keyText =
keyText == "Oem1" ? "Semi colon" :
keyText == "Oem2" ? "Slash" :
keyText == "Oem3" ? "Left quote" :
keyText == "Oem4" ? "Open Brackets" :
keyText == "Oem5" ? "Backslash" :
keyText == "Oem6" ? "Close Brackets" :
keyText == "Oem7" ? "Double quotes" :
keyText == "OemPeriod" ? "period" :
keyText == "Oemcomma" ? "comma" :
keyText == "OemMinus" ? "Oem minus" :
keyText == "Oemplus" ? "Oem equals" : keyText;
label1.Text+= keyText;
}