没有Tk的Python键被按下

时间:2015-07-08 12:23:58

标签: python tkinter raspberry-pi keypress

我在Windows 7中通过SSH使用Raspberry Pi,我构建了一个机器人。如果按箭头,它将移动。我用TkInter模块检测到密钥,但它需要一个图形环境。所以如果我只是在SSH终端,它就无法运行。是否有一些模块可以检测到密钥并且不需要窗口?

1 个答案:

答案 0 :(得分:-1)

我没有尝试过,但快速搜索已经显示出来了:
example
github source

本质上是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()