3

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()

1 Answers1

1

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()
James
  • 32,991
  • 4
  • 47
  • 70
  • I created a Pastebin to show you the code because StackOverflow won't allow that many characters, anyways here's the problem https://pastebin.com/QmVAyA97 –  Dec 13 '19 at 22:47
  • 1
    Sorry. I dropped one of the import statements. Try it now. Also, it seems to not work on the first 'a', but only subsequent ones for me. – James Dec 14 '19 at 22:44