简体   繁体   English

Visual Studio 中 KeyDown 事件、KeyPress 事件和 KeyUp 事件的区别

[英]Difference between the KeyDown Event, KeyPress Event and KeyUp Event in Visual Studio

Can anyone tell me the difference between the KeyDown event, the KeyPress event and the KeyUp event?谁能告诉我KeyDown事件、 KeyPress事件和KeyUp事件之间的区别? I checked the msdn site and it does not explain it much.我检查了 msdn 站点,它并没有解释太多。

Can anyone tell me in simple logical sense when each of the event occurs?任何人都可以在每个事件发生时简单地告诉我吗? I feel that all the above event occurs when a key is pressed.我觉得所有上述事件都是在按下一个键时发生的。 So what is the exact difference between them.那么它们之间的确切区别是什么。

The MSDN documentation states the order in which the three events occur fairly clearly: MSDN 文档相当清楚地说明了这三个事件发生的顺序:

Key events occur in the following order:关键事件按以下顺序发生:

  1. KeyDown按键
  2. KeyPress按键
  3. KeyUp键升

KeyDown is raised as soon as the user presses a key on the keyboard, while they're still holding it down.一旦用户按下键盘上的一个键, KeyDown就会被触发,而他们仍然按住它。

KeyPress is raised for character keys (unlike KeyDown and KeyUp, which are also raised for noncharacter keys) while the key is pressed. KeyPress字符键引发(与 KeyDown 和 KeyUp 不同,它们也为非字符键引发),同时按下键。 This is a "higher-level" event than either KeyDown or KeyUp, and as such, different data is available in the EventArgs .这是比 KeyDown 或 KeyUp 的“更高级别”事件,因此,在EventArgs中有不同的数据可用。

KeyUp is raised after the user releases a key on the keyboard. KeyUp在用户释放键盘上的键后引发。

Generally, you should handle the KeyUp event in your application.通常,您应该在应用程序中处理KeyUp事件。 Actions should not be initiated in the UI until after the user releases the key.用户释放密钥之前,不应在 UI 中启动操作。 And since KeyUp is a lower-level event than KeyPress , you'll always have plenty of information at your fingertips about the key that was pressed, and it will even work for handling non-character keys.而且由于KeyUp是比KeyPress更低级别的事件,因此您总是可以随时获得有关所按下键的大量信息,它甚至可以用于处理非字符键。


The thing to note about all of these events, however, is that they are only raised by the control that has the focus.然而,关于所有这些事件需要注意的是,它们仅由具有焦点的控件引发。 That means if a button control on your form currently has the focus, none of the key events for your form will ever get raised.这意味着如果表单上的按钮控件当前具有焦点,则表单的任何关键事件都不会被引发。 This is often confusing for programmers new to .NET.对于刚接触 .NET 的程序员来说,这常常令人困惑。 The best way to handle this is by overriding the form's ProcessCmdKey method :处理此问题的最佳方法是覆盖表单的ProcessCmdKey方法

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.A))
    {
        MessageBox.Show("You pressed Ctrl+A!");
    }
    return base.ProcessCmdKey(ref msg, keyData);
}
  • KeyDown: happens when the person presses a key (when the keyboard first detects a finger on a key, this happens when the key is pressed down). KeyDown:当人按下一个键时发生(当键盘第一次检测到一个键上的手指时,这发生在按下键时)。

  • KeyPress: happens when a key is pressed and then released. KeyPress:当一个键被按下然后释放时发生。

  • KeyUp: happens when the key is released KeyUp:释放键时发生

You are right that all of these events occur when a key is pressed and then released, in the order I described above.你是对的,所有这些事件都发生在一个键被按下然后释放时,按照我上面描述的顺序。

Here's a case when you DON'T want to use KeyUp:这是您不想使用 KeyUp 的情况:

You have a list box and pressing the Enter key on a row invokes an editor dialog.您有一个列表框,在一行上按 Enter 键会调用一个编辑器对话框。 Problem: If the user presses the Enter key on the OK button of the editor, a KeyUp(e.KeyCode=Enter) event will leak back to your list box, causing the editor to reopen.问题:如果用户在编辑器的 OK 按钮上按下 Enter 键,KeyUp(e.KeyCode=Enter) 事件将泄漏回您的列表框,导致编辑器重新打开。 This doesn't happen if the user presses the space bar on the editor's OK button;如果用户按下编辑器的 OK 按钮上的空格键,则不会发生这种情况; in that case the KeyUp(e.KeyCode=Space) event is handled by the editor before it closes.在这种情况下,KeyUp(e.KeyCode=Space) 事件在编辑器关闭之前由编辑器处理。

Here's a selection heuristic I use:这是我使用的选择启发式方法:

If I'm handling the Enter key and I need to guard against a case like the one above
  then I use KeyDown    
Else if I'm handling key combinations (e.g. CTRL+C)
   then I favor* KeyDown (KeyUp can make these awkward)   
Else if I am allowing press & hold autorepeat
  then I use KeyDown    
Else 
  I use KeyUp

*If the action is a one that can be done in a commonly used product, say Microsoft Office, such as CTRL+A (for 'Select All'), then I mimic the Microsoft behavior, since that is what users are used to. *如果该操作是可以在常用产品(例如 Microsoft Office)中执行的操作,例如 CTRL+A(用于“全选”),那么我会模仿 Microsoft 的行为,因为这是用户习惯的行为。

KeyDown then KeyPress then KeyUp is the order I find. KeyDown然后KeyPress然后KeyUp是我找到的顺序。

Usually you want to hook KeyDown when it is for an application where a user holds down a key for multi-mode input with control key mode modification, like in a shift-click operation.通常,当用户按住一个键进行多模式输入并修改控制键模式的应用程序时,您希望挂钩KeyDown ,例如在 shift-click 操作中。 KeyPress is for simple key entry type logic -- just getting the key strokes. KeyPress用于简单的键输入类型逻辑 - 只是获取击键。 KeyUp is hooked to put in logic that executes after something else processes KeyPress , like to modify the contents of a text edit box after it's main KeyPress logic has taken effect. KeyUp被挂钩以放入在其他处理KeyPress之后执行的逻辑,例如在其主要KeyPress逻辑生效后修改文本编辑框的内容。 Frankly, I don't use KeyUp all that much, but sometimes it is the only way to get a message after something else has processed KeyPress and you need to check on / fixup what happened.坦率地说,我并没有那么多地使用KeyUp ,但有时它是在其他东西处理KeyPress之后获取消息的唯一方法,您需要检查/修复发生了什么。

In addition to the other answers:除了其他答案:

When trying to figure to which of these events you should hookup your action, mind that the KeyDown event will be fired multiple times while the key is held down.当试图确定您应该将您的操作与这些事件中的哪个事件联系起来时,请注意KeyDown事件将在按住键时多次触发。 Sometimes you want this behavior, sometimes not.有时您想要这种行为,有时则不需要。 Based on that I suggest the following usage (based on my experience):基于此,我建议以下用法(根据我的经验):

(Order in which events are fired) (触发事件的顺序)

KeyDown按键

Occurs: When key is pressed and while held down发生:当按键被按下并按住时
Usage: Perform action immediately on button press or even multiple times when held down用法:按下按钮时立即执行操作,甚至按住时执行多次
Example: Moving cursor with arrow keys示例:用箭头键移动 cursor

. .

KeyPress按键

Occurs: Character key is pressed (Higher level event)发生:字符键被按下(更高级别的事件)
Usage: Anything typing related用法:任何与打字相关的东西
Example: Handle textbox input示例:处理文本框输入

. .

KeyUp键升

Occurs: Key is released发生:密钥被释放
Usage: Perform critical action which should only occur once per keystroke用法:执行每次击键仅应发生一次的关键操作
Example: Write data to file示例:将数据写入文件

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

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