简体   繁体   English

在Python中使用win32api检测按键

[英]Detecting Key Presses using win32api in Python

I'm trying to break a loop in Python with a specific key press using win32api. 我正在尝试使用win32api通过特定的按键来打破Python中的循环。 How would one go about this? 怎么会这样呢?

What is the actual version of win32api.KeyPress('H') , in the following code? 在以下代码中, win32api.KeyPress('H')的实际版本是什么?

Revised: 修改后:

import win32api

while True :
    cp = win32api.GetCursorPos()
    print cp
    if win32api.KeyPress('H') == True :
        break

I want to be able to break a loop by pressing the h key. 我希望能够通过按h键来中断循环。

Edit: 编辑:

I'm attempting to make a program that repeatedly reports mouse positions and I need a mechanism to exit said program. 我正在尝试制作一个重复报告鼠标位置的程序,并且需要一种退出该程序的机制。

See revised code. 请参阅修订的代码。

win32api is just an interface to the underlying windows low-level library. win32api只是基础Windows低级库的接口。 See the GetAsyncKeyState Function : 请参阅GetAsyncKeyState函数

Determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. 确定在调用函数时按键是向上还是向下,以及在上一次调用GetAsyncKeyState之后是否按下了该按键。

Syntax 句法

SHORT WINAPI GetAsyncKeyState(
__in  int vKey
);

Return Value 返回值

Type: SHORT 类型: SHORT

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. 如果函数成功,则返回值指定自上次调用GetAsyncKeyState以来是否按下了该键,以及该键当前处于向上还是向下。 If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. 如果设置了最高有效位,则该键处于按下状态;如果设置了最低有效位,则在上一次调用GetAsyncKeyState之后按下了该键。

Note that the return value is bit-encoded (not a boolean ). 请注意,返回值是位编码的(不是boolean )。 To get at vKey values, an application can use the virtual-key code constants in the win32con module. 为了获得vKey值,应用程序可以使用win32con模块中的虚拟键代码常量。

For example, testing the "CAPS LOCK" key: 例如,测试“ CAPS LOCK”键:

>>> import win32api
>>> import win32con
>>> win32con.VK_CAPITAL
20
>>> win32api.GetAsyncKeyState(win32con.VK_CAPITAL)
0
>>> win32api.GetAsyncKeyState(win32con.VK_CAPITAL)
1

The virtual-key constant for simple letters are ASCII codes, so that testing the state of the "H" key (key was pressed) will look like: 简单字母的虚拟键常量是ASCII码,因此测试“ H”键(按下键)的状态将类似于:

>>> win32api.GetAsyncKeyState(ord('H'))
1

Check the python tiler on github, very useful even if you are trying to just find key codes to send. 检查github上的python tiler,即使您只是试图查找要发送的关键代码,它也非常有用。 Also this will be useful if you are running your code in the background and want to break the loop from outside the window. 如果在后台运行代码并希望从窗口外部中断循环,这也将很有用。

git project: https://github.com/Tzbob/python-windows-tiler git项目: https : //github.com/Tzbob/python-windows-tiler

code with windows keys: https://code.google.com/p/python-windows-tiler/source/browse/pwt/hotkey.py?r=df41af2a42b6304047a5f6f1f2903b601b22eb39 使用Windows键的代码: https : //code.google.com/p/python-windows-tiler/source/browse/pwt/hotkey.py?r=df41af2a42b6304047a5f6f1f2903b601b22eb39

This isn't how it works in GUI programming. 这不是GUI编程中的工作方式。 You don't call a method to check for a key press. 您不会调用检查按键的方法。 Instead you get sent messages when keys are pressed. 相反,当您按下按键时,您会收到已发送的消息。 Assuming that you have a window that is receiving input then you need to respond to the WM_KEYDOWN message arriving in your window procedure, or message map in Python win32api terms. 假设您有一个正在接收输入的窗口,那么您需要响应窗口过程中到达的WM_KEYDOWN消息或Python win32api术语中的消息映射。


Your edit shows that you are not using the message queue which is rather unusual. 您的编辑显示您没有使用消息队列,这很不正常。 You may be able to achieve what you wish by calling GetAsyncKeyState() . 您可能可以通过调用GetAsyncKeyState()实现您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM