C#:在 KeyDown 事件中,我应该使用什么来检查哪个键被按下?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/564338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:21:12  来源:igfitidea点击:

C#: In a KeyDown event, what should I use to check what key is down?

c#keyboard

提问by Svish

In a KeyDown event, I have the KeyEventArgs to work with. It has (among other things) these three properties:

在 KeyDown 事件中,我可以使用 KeyEventArgs。它具有(除其他外)这三个属性:

  • e.KeyCode
  • e.KeyData
  • e.KeyValue
  • e.KeyCode
  • e.KeyData
  • e.KeyValue

Which one should I use for what?

我应该使用哪一个?

采纳答案by Richard Szalay

Edit:Somehow I misread your question to include checking a valid character. Did you modify it? I've added a description of each.

编辑:不知何故,我误读了您的问题,包括检查有效字符。你修改了吗?我已经添加了每个的描述。

  • KeyCodeis the Keysenumeration value for the key that is down
  • KeyDatais the same as KeyCode, but combined with any SHIFT/CTRL/ALT keys
  • KeyValueis simply an integer representation of KeyCode
  • KeyCode是按下的键的Keys枚举值
  • KeyData与 KeyCode 相同,但与任何 SHIFT/CTRL/ALT 键结合使用
  • KeyValue只是 KeyCode 的整数表示

If you justneed the character, I'd probably recommend using the KeyPress event and using the KeyPressEventArgs.KeyCharproperty. You can then use Char.IsLetterOrDigit()to find out if it's a valid character.

如果您需要这个角色,我可能会建议使用 KeyPress 事件和KeyPressEventArgsKeyChar属性。然后您可以使用Char.IsLetterOrDigit()来确定它是否是有效字符。

Alternatively, you might be able to cast KeyEventArgs.KeyCodeto a char and then use Char.IsLetterOrDigit on that.

或者,您可以将KeyEventArgs.KeyCode转换为字符,然后在其上使用 Char.IsLetterOrDigit。

回答by Cerebrus

I would suggest using the KeyCodeproperty to check against the Keysenumeration for most operations. However some of the basic differences below might help you to better decide which one you need for your situation.

我建议使用该KeyCode属性来检查Keys大多数操作的枚举。但是,下面的一些基本差异可能会帮助您更好地决定根据您的情况需要哪一种。

Differences:

区别:

  • KeyCode- Represents the Keysenumeration value that represents the key that is currently in Down state.

  • KeyData- Same as KeyCode, except that it has additional information in the form of modifiers - Shift/Ctrl/Alt etc.

  • KeyValue- The numeric value of the KeyCode.

  • KeyCode- 表示Keys枚举值,表示当前处于 Down 状态的键。

  • KeyData- 与 相同KeyCode,除了它具有修饰符形式的附加信息 - Shift/Ctrl/Alt 等。

  • KeyValue- 的数值KeyCode

回答by Soltys

Very basic using of KeyDown

KeyDown 的非常基本的使用

   private void tbSomeText_KeyDown (object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.B && e.Modifiers != Keys.Shift) {

            MessageBox.Show("You Pressed b");
        }
        else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Shift) {
            MessageBox.Show("You Pressed Shift+A");
        }
    }

回答by Treb

See my answerto your other question:

请参阅对您的另一个问题的回答

Use the KeyPressedevent.

使用KeyPressed事件。

Quoting MSDN:

引用 MSDN:

A KeyPressEventArgs specifies the character that is composed when the user presses a key. For example, when the user presses SHIFT + K, the KeyChar property returns an uppercase K.

KeyPressEventArgs 指定用户按下某个键时组合的字符。例如,当用户按下 SHIFT + K 时,KeyChar 属性返回一个大写的 K。

This way you don't need to mess around with e.KeyCode, e.KeyDataand e.KeyValue.

这样你就不需要搞乱e.KeyCode, e.KeyDatae.KeyValue