PreviewKeyDown is not seeing Alt-modifiers

后端 未结 2 1871
名媛妹妹
名媛妹妹 2021-01-03 18:33

I have some code which is (supposed to be) capturing keystrokes. The top level window has a

Keyboard.PreviewKeyDown=\"Window_PreviewKeyDown\"
2条回答
  •  有刺的猬
    2021-01-03 19:11

    The trouble is that when Alt is held down your KeyEventArgs has:

    Key = Key.System
    SystemKey = the real key
    

    so when checking for Alt you need to use e.SystemKey instead of e.Key, like this:

    if (e.KeyboardDevice.Modifiers == ModifierKeys.Alt) {   
        switch (e.SystemKey) { 
          ...
    

    Explanation

    Under Windows, the "Alt" key is handled specially. When the Alt key itself is pressed or another key is pressed while the Alt key is held down, it is considered a "System" keypress. "System" keypresses are handled differently than regular keypresses in many ways.

    It all starts out when Windows passes the keypress to your application. Normal key down events generate a WM_KEYDOWN, but if the Alt key is pressed it generates a WM_SYSKEYDOWN. By the same token a WM_KEYUP is translated into a WM_SYSKEYUP.

    Throughout Windows, including in WPF, the special handling of the Alt key is used with MenuItems, Buttons and Labels that include "access text". For example, if a button has content of "Say _Hi", then presing Alt-H will be treated as a a button click.

    When the Alt key is down, letters come in as three pairs of events: KeyDown, KeyUp and TextInput, each with their associated preview versions. The primary differences here are:

    • The KeyDown and KeyUp events have their Key property set to "Key.System" rather than the actual key that was pressed, and the SystemKey set to the actual key pressed.
    • The TextInput event is passed normally but then handled as an AccessKey if it is not consumed

提交回复
热议问题