pygame捕获窗口未聚焦时的键盘事件 [英] pygame capture keyboard events when window not in focus

查看:555
本文介绍了pygame捕获窗口未聚焦时的键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的python脚本,该脚本可以控制光标到操纵杆. 此处中记录了我找出这种方法的方式.现在可以正常使用了,但是,一旦启动脚本使用操纵杆,鼠标就没有用了,因为每当新的操纵杆事件出现时,我的python例程都会将值设置回其原始值.

I wrote a simple python script that gives control over the cursor to a joystick. My way to find out how this works is documented here. Now that works flawlessly but, as soon as I start the script to use the joystick, the mouse is useless, because my python routine sets the value back to its original, whenever a new joystick event comes in.

因此,只要按下键盘的某个键,我就希望忽略操纵杆事件.我碰到了pygame.key.get_pressed()方法,但这似乎只有在pygame窗口处于焦点时才有效.我希望此脚本在后台运行.我应该开始使用非pygame事件来监听键盘,还是可以通过pygame跟踪类似于后台识别的操纵杆事件的键盘事件?

Thus I want my joystick events to be ignored as long as a key of the keyboard is pressed. I came across the pygame.key.get_pressed() method but this seems to work only, if the pygame window is in focus. I want this script running in background. Should I start using non-pygame events to listen to the keyboard or are there ways to keep track of the keyboard events analogue to the joystick events, which are recognized in background, via pygame?

推荐答案

我希望pygame设置自己的沙盒",因此很难从其窗口之外检测输入.您先前的问题表明您也在使用win32api模块.我们可以使用它来检测全局按键.

I expect pygame sets up its own "sandbox" so that it's hard to detect input from outside its window. Your previous question indicates that you are also using the win32api module. We can use that to detect global key presses.

在全局范围内检测按键的正确方法是使用

The correct way to detect key presses at the global scope is to set up a keyboard hook using SetWindowsHookEx. Unfortunately, win32api does not expose that method, so we'll have to use a less efficient method.

GetKeyState 方法可用于确定按键是向下还是向上.您可以连续检查键的状态,以查看用户最近是否按下或释放了该键.

The GetKeyState method can be used to determine whether a key is down or up. You can continuously check the state of a key to see if the user has pressed or released it lately.

import win32api
import time

def keyWasUnPressed():
    print "enabling joystick..."
    #enable joystick here

def keyWasPressed():
    print "disabling joystick..."
    #disable joystick here

def isKeyPressed(key):
    #"if the high-order bit is 1, the key is down; otherwise, it is up."
    return (win32api.GetKeyState(key) & (1 << 7)) != 0


key = ord('A')

wasKeyPressedTheLastTimeWeChecked = False
while True:
    keyIsPressed = isKeyPressed(key)
    if keyIsPressed and not wasKeyPressedTheLastTimeWeChecked:
        keyWasPressed()
    if not keyIsPressed and wasKeyPressedTheLastTimeWeChecked:
        keyWasUnPressed()
    wasKeyPressedTheLastTimeWeChecked = keyIsPressed
    time.sleep(0.01)

警告:与任何先真睡眠然后再检查"循环一样,此方法可能比等效的设置回调并等待"方法使用更多的CPU周期.您可以延长sleep周期的长度来改善此情况,但是按键检测将花费更长的时间.例如,如果您睡了整整一秒钟,则从按下按键到禁用操纵杆之间可能要花费一秒钟的时间.

Warning: as with any "while True sleep and then check" loop, this method may use more CPU cycles than the equivalent "set a callback and wait" method. You can extend the length of the sleep period to ameliorate this, but the key detection will take longer. For example, if you sleep for a full second, it may take up to one second between when you press a key and when the joystick is disabled.

这篇关于pygame捕获窗口未聚焦时的键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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