使用模块键盘输出重复

问题描述

我正在开发一个使用 python 模块“键盘”捕获键的键盘记录器。但是我的输出有问题。当我输入“Hello World”时,输出是flase。像这样:“shift-H-shift-e-e-l-l-o-space-W-o-r-l-d-d-enter
代码

print("starting")
import keyboard                 # imports the keyboard-module
file = open("log.txt","w")     # creates a file
nZ = 0
keyboard.unhook_all()           # clears all keys that were typed in

def on_key(key):
    file.write(str(key.name) + "\n")    # writes the output to a file
    file.flush()                        # saves the written stuff

while True:
    keyboard.hook(on_key)       # waits for a key typed in
    keyboard.unhook_all()       # again clears all keys that were typed in
    nZ += 1

file.close()

所以它们有时是重复的,有时不是。这是为什么?是我的电脑变慢了,是模块本身还是我使用了错误功能或类似的东西?

编辑 - 正确的代码

print("starting")
import keyboard                 # imports the keyboard-module
file = open("log.txt","w")     # creates a file
keyboard.unhook_all()           # clears all keys that were typed in

def on_key(key):
    file.write(str(key.name) + "\n")    # writes the output to a file
    file.flush()                        # saves the written stuff

while True:
    keyboard.on_release(on_key)       # waits for a key typed in
    keyboard.unhook_all()              # again clears all keys that were typed in

file.close() 

但是,如果您想确切地查看用户输入的内容,可以用三行来完成:

import keyboard
rk = keyboard.record(until="Esc")
keyboard.play(rk)
 

解决方法

问题是钩子开火,松开,弹回并立即再次开火,因为仍然按下键。

您可以通过使用 // Get List_of_Packages where there is no match in List_sub_Activities after // it is joined to Activities with the agency_id set. const agencyId = 2; const packages = await List_of_Packages.findAll({ attributes: ['list_name'],include: { model: List_sub_Activities,// we don't need to actually fetch the list_id attributes: [],include: { model: Activities,where: { agency_id: agencyId,},// uses a LEFT JOIN required: false,// only return results where the List_sub_Activities.list_id is null where: sequelize.where(sequelize.col('List_sub_Activities.list_id'),'IS',null),}); 而不是 keyboard.on_release(on_key) 来防止这种情况发生。这会导致钩子只有在释放钥匙时才会触发。这样当钩子回弹时按键不再被按下。