Tuesday, 30 April 2019

What are the keycodes `getwch` returns?

I'm using msvcrt.getwch to make an interactive prompt in Python targeting Windows.

The documentation says:

Read a keypress and return the resulting character as a byte string. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed. If the pressed key was a special function key, this will return '\000' or '\xe0'; the next call will return the keycode. The Control-C keypress cannot be read with this function.

I have found F10 to use the former - \x00D, and F11 to use the latter \xe0\x85.

However this doesn't say what the keycodes are. This is important as I don't have a \xe0 key (à), among other keys, and so can't test the output of entering this value.


Additional side context

For example, I've tried pasting this key into the prompt in two ways.

  • CTRL-v - Which enters the keycode \x16. Which is the synchronous idle key in ASCII. [1] (Which is the key combination I entered.)
  • Right clicking (Paste in PS) - Results in just \xe0 being entered.

    I'm unsure if Power Shell and Visual C++ are kindly letting me paste escape sequences, or this is the actual handling of that key.


Given that msvcrt seems to be a wrapper around Visual C++ , I also looked at that documentation too.

The _getch and _getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

This also doesn't say what the keycodes are.

I've looked online but I can only find information about virtual keycodes, however this says F10 is \x79, and F11 is \x7A. As this function isn't returning these values, I know it's not referring to these keycodes.



from What are the keycodes `getwch` returns?

No comments:

Post a Comment