ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用Python中的win32api检测键按

2019-09-29 09:55:52  阅读:575  来源: 互联网

标签:python winapi key-bindings


我正在尝试使用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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有