使用Python中的win32api检测键按
作者:互联网
我正在尝试使用win32api通过特定的按键来打破Python中的循环.怎么会这样呢?
win32api.KeyPress(‘H’)的实际版本是什么,在下面的代码中?
修订:
import win32api
while True :
cp = win32api.GetCursorPos()
print cp
if win32api.KeyPress('H') == True :
break
我希望能够通过按h键来打破循环.
编辑:
我正在尝试制作一个反复报告鼠标位置的程序,我需要一种机制来退出所述程序.
查看修订后的代码
解决方法:
win32api只是底层windows低级库的接口.
见GetAsyncKeyState
Function:
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.
Syntax
SHORT WINAPI GetAsyncKeyState(
__in int vKey
);
Return Value
Type:
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. 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.
请注意,返回值是位编码的(不是布尔值).
要获得vKey值,应用程序可以使用win32con模块中的虚拟键代码常量.
例如,测试“CAPS LOCK”键:
>>> import win32api
>>> import win32con
>>> win32con.VK_CAPITAL
20
>>> win32api.GetAsyncKeyState(win32con.VK_CAPITAL)
0
>>> win32api.GetAsyncKeyState(win32con.VK_CAPITAL)
1
简单字母的虚拟键常量是ASCII码,
所以测试“H”键(按下键)的状态将如下所示:
>>> win32api.GetAsyncKeyState(ord('H'))
1
标签:python,winapi,key-bindings 来源: https://codeday.me/bug/20190929/1831210.html