如何检查是否已使用 Tkinter 按下键? [英] How to check if key has been pressed with Tkinter?
本文介绍了如何检查是否已使用 Tkinter 按下键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我正在制作一个程序,它可以检测您是否点击了键盘上的某个键.我知道你可以:
So I'm making a program where it detects if you have clicked a key on your keyboard. I know you can do:
def show(key):
if key == Key.tab:
print("Click")
with Listener(on_press=show) as listener:
listener.join()
除非我想让它检测是否在 Tkinter 窗口未打开的情况下按下了某个键.就像我保持 Tkinter 窗口打开,切换到 Chrome,单击 Tab,我仍然希望程序打印单击"
Except I want it to detect if a key has been pressed if the Tkinter window isn't open. Like if I keep the Tkinter window open, switch to Chrome, click Tab, I still want the program to print "Click"
推荐答案
下载键盘模块:pip3 安装键盘
Download the keyboard module: pip3 install keyboard
import keyboard #Using module keyboard
while True:#making a loop
try: #used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed('a'): #if key 'a' is pressed
print('You Pressed A Key!')
break #finishing the loop
else:
pass
except:
break #if user pressed other than the given key the loop will break
或使用 msvcrt 模块:导入msvcrt
or use msvcrt module: import msvcrt
while True:
if msvcrt.kbhit():
print('CLİCK')
import keyboard#Keyboard module in Python
rk = keyboard.record(until ='q')#It records all the keys until escape is pressed
keyboard.play(rk, speed_factor = 1)#It replay back the all keys
这篇关于如何检查是否已使用 Tkinter 按下键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文