问题描述
之间的区别是什么的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:
- 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A,修饰符= Keys.None
- 键preSS:KeyChar ='A'
- 的KeyUp:主要code = Keys.A
但是,如果你preSS 移骨节病> + A 骨节病>,您将获得:
But if you press +, you'll get:
- 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
- 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
- 键preSS:KeyChar ='A'
- 的KeyUp:主要code = Keys.A
- 的KeyUp:主要code = Keys.ShiftKey
如果你按住按键一会儿,你会得到这样的:
If you hold down the keys for a while, you'll get something like:
- 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
- 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
- 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
- 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
- 的KeyDown:主要code = Keys.ShiftKey,KEYDATA = Keys.ShiftKey,移位,修饰符= Keys.Shift
- 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
- 键preSS:KeyChar ='A'
- 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
- 键preSS:KeyChar ='A'
- 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
- 键preSS:KeyChar ='A'
- 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
- 键preSS:KeyChar ='A'
- 的KeyDown:主要code = Keys.A,KEYDATA = Keys.A | Keys.Shift,修饰符= Keys.Shift
- 键preSS:KeyChar ='A'
- 的KeyUp:主要code = Keys.A
- 的KeyUp:主要code = Keys.ShiftKey
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.ShiftKey, KeyData=Keys.ShiftKey, Shift, Modifiers=Keys.Shift
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyDown: KeyCode=Keys.A, KeyData=Keys.A | Keys.Shift, Modifiers=Keys.Shift
- KeyPress: KeyChar='A'
- KeyUp: KeyCode=Keys.A
- 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之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!