WPF wrong KeyDown ASCII codes

后端 未结 4 940
北荒
北荒 2020-12-21 02:51

I need to get the numeric values of keyboard keys based on ASCII table .As WPF doesn\'t have built in solution I have tried several hacks:

1. This on

4条回答
  •  -上瘾入骨i
    2020-12-21 03:14

    This is just not the way keyboards work. Every PC in the world has the same keyboard. Pressing a key generates a virtual key value, the same value on every keyboard in the world. The e.Key value you get in the KeyDown event.

    This is where it stops for keys like the function keys, cursor keys, Shift key, etcetera. Unless the key is a typing key that produces a character. Like the letter A. Internally, the KeyDown event is processed by Windows and translated to a character. The actual character that's produced depends on the keyboard layout. Users in different countries have different keyboard layouts, favoring the glyphs that are used most often in their language. Or for that matter the personal preference of a user, a programmer often likes the Dvorak layout.

    The character produced also depends on the keyboard state. Like the Shift key, hold it down to produce an A, not down to produce an a. This can get pretty convoluted on non-English keyboards, other layouts also have dead keys that, when pressed, alters the character produced by a the next keystroke. Languages that have a lot of diacritics use them.

    The implication is clear, translating the virtual key you get from a KeyDown event is a very perilous undertaking. You should therefore not attempt to do this. And you don't have to, use the TextInput event.

提交回复
热议问题