在没有Tk的情况下按Python键
我从Windows7通过SSH使用树莓Pi,并构建了一个机器人。如果按箭头,它将移动。我用TkInter模块检测钥匙,但它需要一个图形环境。因此,如果我只在SSH终端中,它就不能运行。是否有一些模块可以检测钥匙,而不需要窗口?我没有尝试过,但快速搜索显示:在没有Tk的情况下按Python键,python,tkinter,raspberry-pi,keypress,Python,Tkinter,Raspberry Pi,Keypress,我从Windows7通过SSH使用树莓Pi,并构建了一个机器人。如果按箭头,它将移动。我用TkInter模块检测钥匙,但它需要一个图形环境。因此,如果我只在SSH终端中,它就不能运行。是否有一些模块可以检测钥匙,而不需要窗口?我没有尝试过,但快速搜索显示: 它本质上是pyhook的linux实现(仅限windows) 因此,要使用它: import pyxhook import time #This function is called every time a key is presss
它本质上是pyhook的linux实现(仅限windows) 因此,要使用它:
import pyxhook
import time
#This function is called every time a key is presssed
def kbevent( event ):
#print key info
print event
#If the ascii value matches spacebar, terminate the while loop
if event.Ascii == 32:
global running
running = False
#Create hookmanager
hookman = pyxhook.HookManager()
#Define our callback to fire when a key is pressed down
hookman.KeyDown = kbevent
#Hook the keyboard
hookman.HookKeyboard()
#Start our listener
hookman.start()
#Create a loop to keep the application running
running = True
while running:
time.sleep(0.1)
#Close the listener when we are done
hookman.cancel()