之间的区别是什么

之间的区别是什么

本文介绍了是什么在.NET KeyDown和关键preSS之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间的区别是什么的KeyDown 键preSS 事件.NET

What is the difference between the KeyDown and KeyPress events in .net?

推荐答案

有明显的的误解关于这个问题,有很多的!

的KeyDown 键preSS 键之间的唯一区别preSS 继电器的特性,从一键preSS造成的,如果有一个只调用。

The only practical difference between KeyDown and KeyPress is that KeyPress relays the character resulting from a keypress, and is only called if there is one.

在换句话说,如果你preSS A 键盘上,你会得到这一系列事件:

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

  1. 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A,修饰符= Keys.None
  2. 键preSS:KeyChar ='A'
  3. 的KeyUp:主要code = Keys.A

但是,如果你preSS 移 + A ,您将获得:

But if you press +, you'll get:

  1. 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
  2. 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
  3. 键preSS:KeyChar ='A'
  4. 的KeyUp:主要code = Keys.A
  5. 的KeyUp:主要code = Keys.ShiftKey

如果你按住按键一会儿,你会得到这样的:

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

  1. 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
  2. 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
  3. 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
  4. 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
  5. 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
  6. 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
  7. 键preSS:KeyChar ='A'
  8. 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
  9. 键preSS:KeyChar ='A'
  10. 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
  11. 键preSS:KeyChar ='A'
  12. 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
  13. 键preSS:KeyChar ='A'
  14. 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
  15. 键preSS:KeyChar ='A'
  16. 的KeyUp:主要code = Keys.A
  17. 的KeyUp:主要code = 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. 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

注意键preSS 出现的之间的KeyDown 的KeyUp 不可以的KeyUp ,因为很多其他的答案中所指出的,该键preSS 不叫的时候不产生字符,而的KeyDown 被重复,而键被按下,也违背了很多其他的答案。

Notice that KeyPress occurs in between KeyDown and KeyUp, not after KeyUp, as many of the other answers have stated, that KeyPress is not called when a character isn't generated, and that KeyDown is repeated while the key is held down, also contrary to many of the other answers.

这做的不可以直接导致调用键preSS 键的例子:

Examples of keys that do not directly result in calls to KeyPress:

  • ,控制,替代
  • F1 到 F12
  • 箭头键
  • , ,
  • through
  • Arrow keys

这是键的例子做结果调用键preSS

  • A 到以Z , 0 到 9
  • 空格键
  • 标签(KeyChar ='\ T',ASCII 9)
  • 输入(KeyChar ='\ r',即ASCII 13)
  • Esc键(KeyChar ='\ X1B,ASCII 27)
  • 退格(KeyChar ='\ B',ASCII 8)
  • through , through , etc.
  • (KeyChar='\t', ASCII 9)
  • (KeyChar='\r', ASCII 13)
  • (KeyChar='\x1b', ASCII 27)
  • (KeyChar='\b', ASCII 8)

有关好奇,的KeyDown 大致关联到 WM_KEYDOWN 键preSS WM_CHAR 的KeyUp WM_KEYUP WM_KEYDOWN 可以的被称为比键重复的次数较少,但它发送一个重复计数,其中,IIRC,WinForms的使用产生只有一个每重复KEYDOWN。

For the curious, KeyDown roughly correlates to WM_KEYDOWN, KeyPress to WM_CHAR, and KeyUp to WM_KEYUP. WM_KEYDOWN can be 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.

这篇关于是什么在.NET KeyDown和关键preSS之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:53