boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.79k stars 432 forks source link

calling keyboard.send() with a scan code produces wrong output #649

Open ddrowley opened 3 months ago

ddrowley commented 3 months ago

I have a program that is basically just waiting for a keypress and then passing the scan code through to keyboard.send() (running on Windows with a standard US keyboard layout), but it's outputting incorrect characters. For example, pressing the semicolon key (;) produces scan code 39, but keyboard.send(39) outputs a grave (`) character.

The core issue seems to be that the calls to MapVirtualKeyExW() (in _setup_name_tables()) are producing incorrect mappings. In _setup_name_tables(), the semicolon (name == ';') is being mapped to these entries: [(26, 186, ...), (39, 192, ...)]. I think the correct scan code for semicolon is 39, and the correct vk is 186, so the scancode->vk mapping (which is output from MapVirtualKeyExW) is incorrect - it seems to be mixed up with the scan code and vk for the grave key (26 and 192, respectively). Any idea why it's outputting an incorrect mapping, or how to fix it?