.NET 中的 KeyDown 和 KeyPress 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1367700/
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-09-03 13:15:38  来源:igfitidea点击:

What's the difference between KeyDown and KeyPress in .NET?

.net

提问by Josh Kodroff

What is the difference between the KeyDownand KeyPressevents in .net?

中的KeyDownKeyPress事件和有什么不一样.net

回答by P Daddy

There is apparently a lotof misunderstanding about this!

显然,人们对此有很多误解!

The only practical difference between KeyDownand KeyPressis that KeyPressrelays the character resulting from a keypress, and is only called if there is one.

KeyDown和之间的唯一实际区别KeyPressKeyPress中继由按键产生的字符,并且仅在有按键时才被调用。

In other words, if you press Aon your keyboard, you'll get this sequence of events:

换句话说,如果您按下A键盘,您将获得以下事件序列:

  1. KeyDown: KeyCode=Keys.A, KeyData=Keys.A, Modifiers=Keys.None
  2. KeyPress: KeyChar='a'
  3. KeyUp: KeyCode=Keys.A
  1. KeyDown:KeyCode=Keys.A,KeyData=Keys.A,Modifiers=Keys.None
  2. 按键:KeyChar='a'
  3. KeyUp:KeyCode=Keys.A

But if you press Shift+A, you'll get:

但是如果你按下Shift+ A,你会得到:

  1. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  2. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  3. KeyPress: KeyChar='A'
  4. KeyUp: KeyCode=Keys.A
  5. KeyUp: KeyCode=Keys.ShiftKey
  1. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  2. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  3. 按键:KeyChar='A'
  4. KeyUp:KeyCode=Keys.A
  5. KeyUp:KeyCode=Keys.ShiftKey

If you hold down the keys for a while, you'll get something like:

如果你按住键一段时间,你会得到类似的东西:

  1. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  2. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  3. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  4. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  5. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  6. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  7. KeyPress: KeyChar='A'
  8. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  9. KeyPress: KeyChar='A'
  10. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  11. KeyPress: KeyChar='A'
  12. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  13. KeyPress: KeyChar='A'
  14. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  15. KeyPress: KeyChar='A'
  16. KeyUp: KeyCode=Keys.A
  17. KeyUp: KeyCode=Keys.ShiftKey
  1. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  2. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  3. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  4. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  5. KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
  6. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  7. 按键:KeyChar='A'
  8. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  9. 按键:KeyChar='A'
  10. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  11. 按键:KeyChar='A'
  12. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  13. 按键:KeyChar='A'
  14. KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
  15. 按键:KeyChar='A'
  16. KeyUp:KeyCode=Keys.A
  17. KeyUp:KeyCode=Keys.ShiftKey

Notice that KeyPressoccurs in betweenKeyDownand KeyUp, notafter KeyUp, as many of the other answers have stated, that KeyPressis not called when a character isn't generated, and that KeyDownis repeated while the key is held down, also contrary to many of the other answers.

请注意,KeyPress发生KeyDownand之间KeyUp而不是after KeyUp,正如许多其他答案所述,KeyPress当没有生成字符时不会调用它,并且KeyDown在按住键时重复,这也与许多其他答案相反.

Examples of keys that do notdirectly result in calls to KeyPress:

直接导致调用的键的示例KeyPress

  • Shift, Ctrl, Alt
  • F1through F12
  • Arrow keys
  • Shift, Ctrl,Alt
  • F1通过 F12
  • 方向键

Examples of keys that doresult in calls to KeyPress:

该键的例子的调用结果KeyPress

  • Athrough Z, 0through 9, etc.
  • Spacebar
  • Tab(KeyChar='\t', ASCII 9)
  • Enter(KeyChar='\r', ASCII 13)
  • Esc(KeyChar='\x1b', ASCII 27)
  • Backspace(KeyChar='\b', ASCII 8)
  • A通过Z0通过9等。
  • Spacebar
  • Tab(KeyChar='\t', ASCII 9)
  • Enter(KeyChar='\r', ASCII 13)
  • Esc(KeyChar='\x1b', ASCII 27)
  • Backspace(KeyChar='\b', ASCII 8)

For the curious, KeyDownroughly correlates to WM_KEYDOWN, KeyPressto WM_CHAR, and KeyUpto WM_KEYUP. WM_KEYDOWNcanbe called fewer than the the number of key repeats, but it sends a repeat count, which, IIRC, WinForms uses to generate exactly one KeyDown per repeat.

对于好奇,KeyDown大致关联到WM_KEYDOWNKeyPressWM_CHARKeyUpWM_KEYUPWM_KEYDOWN可以调用少于重复键的次数,但它会发送一个重复计数,IIRC,WinForms 使用它来每次重复生成一个 KeyDown。

回答by Jon Spokes

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

KeyPress 事件不是由非字符键引发的;但是,非字符键确实会引发 KeyDown 和 KeyUp 事件。

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

回答by stevehipwell

KeyPress is only fired by printable characters and is fired after the KeyDown event. Depending on the typing delay settings there can be multiple KeyDown and KeyPress events but only one KeyUp event.

KeyPress 仅由可打印字符触发,并在 KeyDown 事件之后触发。根据输入延迟设置,可以有多个 KeyDown 和 KeyPress 事件,但只有一个 KeyUp 事件。

KeyDown
KeyPress
KeyUp

KeyDown
KeyPress
KeyUp

回答by Jeff Hornby

KeyPress is a higher level of abstraction than KeyDown (and KeyUp). KeyDown and KeyUp are hardware related: the actual action of a key on the keyboard. KeyPress is more "I received a character from the keyboard".

KeyPress 是比 KeyDown(和 KeyUp)更高的抽象级别。KeyDown 和 KeyUp 与硬件相关:键盘上按键的实际操作。KeyPress 更像是“我从键盘收到一个字符”。

回答by Jon B

From MSDN:

来自 MSDN:

Key events occur in the following order:

  1. KeyDown

  2. KeyPress

  3. KeyUp

关键事件按以下顺序发生:

  1. 按键按下

  2. 按键

  3. 键升

Furthermore, KeyPress gives you a chance to declare the action as "handled" to prevent it from doing anything.

此外,KeyPress 使您有机会将操作声明为“已处理”以防止其执行任何操作。

回答by John Boker

I've always thought keydown happened as soon as you press the key down, keypress is the action of pressing the key and releasing it.

我一直认为只要按下键就会发生keydown,keypress是按下键并释放它的动作。

i found this which gives a little different explaination: http://bytes.com/topic/net/answers/649131-difference-keypress-keydown-event

我发现这给出了一些不同的解释:http: //bytes.com/topic/net/answers/649131-difference-keypress-keydown-event

回答by Rob Cowell

Keydown is pressing the key without releasing it, Keypress is a complete press-and-release cycle.

Keydown 是按下键而不释放它,Keypress 是一个完整的按下和释放循环。

Put another way, KeyDown + KeyUp = Keypress

换句话说,KeyDown + KeyUp = Keypress

回答by Abdul Latheef Mohamed

From Blogging Developer:

来自博客开发者

In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character"and a "key". A "key"is a physical button on the computer's keyboard while a "character"is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keysbeing pressed or released, while the keypress event represents a characterbeing typed. The implementation of the theory is not same in all browsers.

为了理解 keydown 和 keypress 之间的区别,了解“字符”“键”之间的区别很有用。一个“关键”是计算机的键盘上的物理按键,而一个“字”是按一个按钮输入的符号。理论上,keydown 和 keyup 事件表示按键被按下或释放,而 keypress 事件表示正在输入一个字符。该理论的实现在所有浏览器中并不相同。

Note:You can also try out the Key Event Tester(available on the above-mentioned site) to understand this concept.

注意:您还可以尝试使用Key Event Tester(可在上述站点上获得)来理解这个概念。

回答by Michael Haephrati

KEYUP will be captured only once, upon release of the key pressed, regardless of how long will the key be held down, so if you want to capture such press only once, KEYUP is the suitable event to capture.

KEYUP 将仅捕获一次,在释放按下的键时,无论该键将被按住多长时间,因此如果您只想捕获此类按下一次,则 KEYUP 是适合捕获的事件。

回答by Michael Haephrati

Easiest explanation:

最简单的解释:

I held down the 'd' key for a second and then released.

我按住“d”键一秒钟,然后松开。

dddddd

滴滴滴

the keydown event happened once before the first d appeared on the screen, the keypress event happened 6 times and the keyup event happened after the last d appeared on the screen.

keydown 事件发生在第一个 d 出现在屏幕上之前,keypress 事件发生了 6 次,keyup 事件发生在屏幕上最后一个 d 出现之后。