Reputation:
Hi everyone I want to know if there is any way to reference backspace for my project, I want to do if the person presses 'a' then it deletes it and types 'ree' can someone pls help me with this?
Code:
from pynput import keyboard
from pynput.keyboard import Key, Controller
COMBINATIONS = [
{keyboard.KeyCode(char='a')}
]
current = set()
def execute():
keyboard = Controller()
if keyboard.on_press('a'):
*insert code here*
keyboard.type('ree')
def on_press(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
current.add(key)
if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
execute()
def on_release(key):
if any([key in COMBO for COMBO in COMBINATIONS]):
current.remove(key)
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
Upvotes: 3
Views: 1643
Reputation: 36608
To overwrite a letter, you can send the '\b'
which will move the cursor backwards one letter. Subsequent letters will then write over it.
from pynput.keyboard import Listener, Controller
from pynput import keyboard
key_ctrl = Controller()
def on_press(key):
if key == keyboard.Key.esc:
return False
elif key == keyboard.KeyCode.from_char('a'):
key_ctrl.type('\bree')
with Listener(on_press=on_press) as listener:
listener.join()
Upvotes: 1