有什么区别KeyDownKeyPress.NET中的事件?

答案

显然有很多误会了这一点!

唯一的实际区别KeyDownKeyPress就是它KeyPress继电器由键盘产生的字符,只有在有键盘时才会调用。

换句话说,如果您按A在您的键盘上,您将获得一系列事件:

  1. keydown:keycode = keys.a,keydata = keys.a,modifiers = keys.none
  2. 按键:KeyChar=‘a’
  3. keyup:keyCode = keys.a

但如果你按转移+A, 你会得到:

  1. keydown:keycode = keys.shiftkey,keydata = keys.shiftkey,shift,modifiers = keys.shift
  2. keydown:keyCode = keys.a,keydata = keys.a |keys.shift,modifier = keys.shift
  3. 按键: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.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 |
  7. 按键:KeyChar=‘A’
  8. keydown:keyCode = keys.a,keydata = keys.a |keys.shift,modifier = keys.shift
  9. KeyPress: KeyChar=‘A’
  10. keydown:keyCode = keys.a,keydata = keys.a |keys.shift,modifier = keys.shift
  11. 按键:KeyChar=‘A’
  12. keydown:keyCode = keys.a,keydata = keys.a |keys.shift,modifier = keys.shift
  13. 按键:KeyChar=‘A’
  14. keydown:keyCode = keys.a,keydata = keys.a |keys.shift,modifier = keys.shift
  15. 按键:KeyChar=‘A’
  16. keyup:keyCode = keys.a
  17. keyup:keyCode = keys.shiftkey

注意KeyPress发生之间 KeyDownKeyUp,notKeyUp,正如许多其他答案所说,KeyPress并不时调用一个字符不是生成的,,KeyDown重复而关键是压低,也与许多其他的答案。

键的示例not 直接导致电话KeyPress

  • 转移,,,,Ctrl,,,,alt
  • F1通过F12
  • 方向键

键的示例do 导致电话KeyPress

  • A通过z,,,,0通过9, ETC。
  • 空格键
  • 标签(keychar =’\ t’,ascii 9)
  • 进入(keychar =’\ r’,ascii 13)
  • Esc键(keychar =’\ x1b’,ascii 27)
  • Backpace(keychar =’\ b’,ascii 8)

为了好奇,KeyDown大致相关WM_KEYDOWN,,,,KeyPressWM_CHAR, 和KeyUpWM_KEYUPWM_KEYDOWN 被称为少于密钥重复序列的数量,但它发送了一个重复计数,iirc,winforms用来精确地生成一个重复一个键down。

来自: stackoverflow.com