Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 612
Peilonrayz Avatar asked Apr 27 '19 00:04

Peilonrayz


1 Answers

Will respond this with help of visual c++ only.

Function getwch will return scan code not virtual key code. hence reference to virtual keycodes is invalid.

Additionally, as per MSDN _getch, _getwch documents, 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.

Following code will return valid scan code:

#include<stdio.h>
#include <ctype.h>

int main()
{
    wchar_t ch = _getwch();  //Will return '0'
    ch = _getwch();          //Will return valid scan code
}

To answer your question, list of valid scan codes are present in Keyboard Scan Code Specification.

For example, second call of above function will return 68 (0x44) for F10 key press and same will be in MSDN: F10 scan code

Same list present in Key Scan Codes, however cannot be authenticated due to older document.

like image 128
Santosh Dhanawade Avatar answered Sep 21 '22 15:09

Santosh Dhanawade