没有 GUI 的 Python 键盘事件(非阻塞) [英] Python keyboard events without GUI (non-blocking)

查看:22
本文介绍了没有 GUI 的 Python 键盘事件(非阻塞)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种在 Python 中获取 KeyDown 和 KeyUp 事件的方法.使用 Pygame,这可以通过 pygame.KEYDOWNpygame.KEYUP 轻松完成,但我试图找到一种不使用 GUI 的方法.

I am trying to find a way to get KeyDown and KeyUp events in Python. With Pygame, this is easily done with pygame.KEYDOWN and pygame.KEYUP, but I am trying to find a way to do it without using a GUI.

理想情况下,我希望能够定义一个函数 isKeyDown(key),如果 key 当前正在返回 True按住,或者如果不是,则返回 False,但无论哪种方式,它都会继续执行代码(它将是非阻塞的).例如:

Ideally, I would like to be able to define a function isKeyDown(key), which would return True if key is currently being held down, or return False if it isn't, but either way, it would move on with the code (it would be non-blocking). For example:

while True:
    if not isKeyDown("a"): #If 'a' isn't currently being pressed
         print ("You are not pressing 'a'")
    elif isKeyDown("a"): #If 'a' is being held down when this is run
         print ("You are pressing 'a'")
         print ("This is the end of this program")
         break

我试过 msvcrt.getch(),但有两个问题:

I've tried msvcrt.getch(), but that has two problems:

1) 它停止程序,直到按下某些东西,并且

1) It stops the program until something is pressed, and

2) 它并不能真正告诉您按键是否被按下;如果当时正在按下键,它只会返回.这有时可能会重合,因为 Windows 会在按下按键时重复按键,但在 while 循环以最大速度运行时不太可能发生这种情况.

2) It doesn't really tell you if the key is being held down; it simply returns if the key is being pressed at the time. This can sometimes coincide since Windows repeats key presses when they are held down, but it is unlikely to happen when the while loop is being run at maximum speed.

我也使用过 msvcrt.kbhit(),但如果有任何键被按下而 msvcrt.getch 未读取,它会将自身设置为 1(),所以仍然会遇到 msvcrt.getch() 的问题,并没有真正返回当前是否真的按下了键.

I've also used msvcrt.kbhit(), but it sets itself to 1 if any key has been pressed and not read by msvcrt.getch(), and so that still runs into the problems of msvcrt.getch() and doesn't really return whether a key is actually currently being held down.

我使用的是 Windows,因此许多人用来回答类似问题的 curses 模块不可用.

I am using Windows, so the curses module that many people have used to answer similar questions isn't available.

有没有办法在没有 GUI 的 Windows 中定义这样的 isKeyDown() 函数,最好只使用内置模块?如果内置 Python 模块无法做到这一点,第三方模块也可以,只要它们不需要 GUI.

Is there a way to define such a isKeyDown() function in Windows without a GUI, preferably only using built-in modules? If this is not possible with the built-in Python modules, third-party modules would be okay, as long as they don't require a GUI.

即使使用 Pygame 或 Tkinter 也可以,只要一种关闭其 GUI 的方法即可.

Even using Pygame or Tkinter would be fine, as long as a way to turn off their GUIs.

如果这不可能,请告诉我原因,这样我就可以停止寻找解决方案(过去几天我一直在尝试做其他事情).

If this is not possible, please tell me why, so I can stop looking for a solution (I have been trying to do little else for the past few days).

我在 64 位 Windows 10 笔记本电脑上使用 Python 3.5.2.

I am using Python 3.5.2 on a 64-bit Windows 10 laptop.

推荐答案

看起来你想要做的是获得 钩子.我相信这取决于 pywin32 库.

It looks like what you want to do is get pyhook. I believe it depends on the pywin32 library.

基本上你想要做的是寻找创建一个键盘记录器.这将为您提供非阻塞键盘事件.当然,您可能希望使用 win32 api 来确保按下键时 的窗口处于焦点,如果这对您很重要的话.

Basically what you want to do is look for creating a keylogger. That will give you non-blocking keyboard events. Of course you may want to use the win32 api to make sure that when the key is pressed it's your window that's in focus, if that matters to you.

我无法访问我的 windows 框,或者我可以为您提供更多代码示例,但这至少应该为您指明正确的方向.

I don't have my windows box accessible, or I'd be able to give you more code samples, but that should at least point you in the right direction.

这篇关于没有 GUI 的 Python 键盘事件(非阻塞)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆