I'm trying to suppress keypresses and remap them to some other keys. The code looks something like this:
from time import sleep
import keyboard
import os
os.chdir(os.path.dirname(__file__))
keys = {
'up':'e',
'down':'s',
'left':'q'
}
for each in zip(keys.keys(), keys.values()):
keyboard.on_press_key(each[0], lambda _:keyboard.press(each[1]), True)
keyboard.on_release_key(each[0], lambda _:keyboard.release(each[1]), True)
while not keyboard.is_pressed('shift'):
sleep(0.5)
But when I run this thing, I expect to get key press of 'e' key when I press up, 's' when I press down and 'q' only when I press left key, But I'm getting 'q' key press for all these three remapped keys!. What am I doing wrong here?!
This happens because of mechanics how python functions bind values and arguments. So you define three callbacks, each of them uses "q" letter. And please, don't
zip(keys.keys(), keys.values())- although since python 3.7 it is probably safe, it is weird.