大家好,我想知道是否有任何方法可以引用我的项目的退格键,我想做的是,如果该人按“ a”,然后将其删除,然后键入“ ree”,有人可以帮助我吗?
码:
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()
最佳答案
要覆盖字母,您可以发送'\b'
,它将使光标向后移动一个字母。随后的字母将覆盖它。
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()
关于python - 有没有办法用Pynput引用python中的退格键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59330209/