检测键盘输入
Detecting keyboard input
我是 Python 的新手,我正在尝试使用键盘库检测何时按下 f 键。这是我正在尝试的代码 运行
import keyboard
keyboard.on_press_key('f',here())
def here():
print('a')
然而,当将 here() 指定为回调时,我在构建时收到名称未定义错误
当你调用here()
时,它还没有定义,所以把here()
的声明移到你的代码上面。
另外,因为here
应该是回调,你需要把它作为函数引用传递给on_press_key
。
import keyboard
def here():
print('a')
keyboard.on_press_key('f', here)
只需向上移动函数 here()
声明,如下所示:
import keyboard
def here():
print('a')
keyboard.on_press_key('f', here())
否则 here()
尚未声明,因此您的错误。
NameError: global name '---' is not defined Python knows the purposes
of certain names (such as names of built-in functions like print).
Other names are defined within the program (such as variables). If
Python encounters a name that it doesn't recognize, you'll probably
get this error. Some common causes of this error include:
Forgetting to give a variable a value before using it in another
statement Misspelling the name of a built-in function (e.g., typing
"inpit" instead of "input")
对于 python 你的情况下在线时的解释器:
keyboard.on_press_key('f',here())
它不知道 here()
是什么,因为它还不在内存中。
示例:
$ cat test.py
dummy_call()
def dummy_call():
print("Foo bar")
$ python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
dummy_call()
NameError: name 'dummy_call' is not defined
$ cat test.py
def dummy_call():
print("Foo bar")
dummy_call()
$ python test.py
Foo bar
import keyboard
IsPressed = False
# once you press the button('f') the function happens once
# when you release the button('f') the loop resets
def here():
print('a')
while True:
if not keyboard.is_pressed('f'):
IsPressed = False
while not IsPressed:
if keyboard.is_pressed('f'):
here()
IsPressed = True
# or if you want to detect every frame it loops then:
def here():
print('a')
while True:
if keyboard.is_pressed('f'):
here()
我是 Python 的新手,我正在尝试使用键盘库检测何时按下 f 键。这是我正在尝试的代码 运行
import keyboard
keyboard.on_press_key('f',here())
def here():
print('a')
然而,当将 here() 指定为回调时,我在构建时收到名称未定义错误
当你调用here()
时,它还没有定义,所以把here()
的声明移到你的代码上面。
另外,因为here
应该是回调,你需要把它作为函数引用传递给on_press_key
。
import keyboard
def here():
print('a')
keyboard.on_press_key('f', here)
只需向上移动函数 here()
声明,如下所示:
import keyboard
def here():
print('a')
keyboard.on_press_key('f', here())
否则 here()
尚未声明,因此您的错误。
NameError: global name '---' is not defined Python knows the purposes of certain names (such as names of built-in functions like print). Other names are defined within the program (such as variables). If Python encounters a name that it doesn't recognize, you'll probably get this error. Some common causes of this error include:
Forgetting to give a variable a value before using it in another statement Misspelling the name of a built-in function (e.g., typing "inpit" instead of "input")
对于 python 你的情况下在线时的解释器:
keyboard.on_press_key('f',here())
它不知道 here()
是什么,因为它还不在内存中。
示例:
$ cat test.py
dummy_call()
def dummy_call():
print("Foo bar")
$ python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
dummy_call()
NameError: name 'dummy_call' is not defined
$ cat test.py
def dummy_call():
print("Foo bar")
dummy_call()
$ python test.py
Foo bar
import keyboard
IsPressed = False
# once you press the button('f') the function happens once
# when you release the button('f') the loop resets
def here():
print('a')
while True:
if not keyboard.is_pressed('f'):
IsPressed = False
while not IsPressed:
if keyboard.is_pressed('f'):
here()
IsPressed = True
# or if you want to detect every frame it loops then:
def here():
print('a')
while True:
if keyboard.is_pressed('f'):
here()