boppreh / keyboard

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

[Windows] keyboard.send() not working with scan codes #551

Closed henriquelino closed 2 years ago

henriquelino commented 2 years ago

using keyboard version 0.13.5 Windows 10 Python v3.9.7

Using keyboard.send(int) does not work until you call any keyboard.send(str)

It seens that the mapping of the scan_code -> char only happens after the first call of send with a char, then the subsequent calls works fine

import keyboard

keyboard.send(30) # scan code to A, this does not works
keyboard.send((30,)) # scan code to A, this does not works

keyboard.send("b") # this works
keyboard.send(30) # scan code to A, this works

key = keyboard.key_to_scan_codes("a")
print(key) # 30, the scan code to A
keyboard.send(key) # this works

Expected aabaa Output: baa

henriquelino commented 2 years ago

Solved using this:

import keyboard
from keyboard._winkeyboard import _setup_name_tables

_setup_name_tables()
keyboard.send(30)

Also made a PR with another way to fix I think