Key Events: ProcessCmdKey

后端 未结 2 813
夕颜
夕颜 2020-11-29 07:04

I am trying to get some keyboard response happening on a little test Windows Form Application, and I have a rough solution, which is to override ProcessCmdKey. However, ther

2条回答
  •  清歌不尽
    2020-11-29 07:34

    The Message structure passed to ProcessCmdKey() contains the WINAPI message number in its Msg property:

    • WM_KEYDOWN is 0x100 (256),
    • WM_KEYUP is 0x101 (257),
    • WM_CHAR (roughly equivalent to KeyPress) is 0x102 (258),
    • WM_SYSKEYDOWN is 0x104 (260),
    • WM_SYSKEYUP is 0x105 (261).

    Concerning your question about KeyPress, it is true that non-character keys such as the arrow keys do not generate WM_CHAR messages internally, but they do generate WM_KEYDOWN, and that message is also sent multiple times for repeated input.

    Also note that I'm not sure ProcessCmdKey() is the right method to achieve what you want. The documentation describes it as only handling main menu command keys and MDI accelerators, which may only be a subset of the keys you want to catch. You might want to override ProcessKeyPreview() instead, which handles all the keyboard messages received by child controls.

提交回复
热议问题