在 Python 中使用 win32api 检测按键

发布于 2024-10-31 13:20:09 字数 440 浏览 3 评论 0原文

我正在尝试使用 win32api 通过特定按键来打破 Python 中的循环。人们会怎样做呢?

以下代码中 win32api.KeyPress('H') 的实际版本是什么?

修订:

import win32api

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

我希望能够通过按h键来中断循环。

编辑:

我正在尝试制作一个重复报告鼠标位置的程序,我需要一种退出所述程序的机制。

请参阅修改后的代码。

I'm trying to break a loop in Python with a specific key press using win32api. How would one go about this?

What is the actual version of win32api.KeyPress('H'), in the following code?

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟─花易冷 2024-11-07 13:20:09

win32api 只是底层 Windows 低级库的接口。
请参阅 GetAsyncKeyState 函数

确定调用该函数时某个键是按下还是按下,以及该键是否在上次调用 GetAsyncKeyState 后按下。

语法

SHORT WINAPI GetAsyncKeyState(
__in  int vKey
);

返回值

类型:简短

如果函数成功,返回值指定自上次调用 GetAsyncKeyState 以来是否按下了该键,以及该键当前是按下还是按下。如果设置了最高有效位,则按键按下,如果设置了最低有效位,则在上次调用 GetAsyncKeyState 后按下按键。

请注意,返回值是位编码的(不是 boolean)。
要获取 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

win32api is just an interface to the underlying windows low-level library.
See the 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.

Note that the return value is bit-encoded (not a boolean).
To get at vKey values, an application can use the virtual-key code constants in the win32con module.

For example, testing the "CAPS LOCK" key:

>>> 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:

>>> win32api.GetAsyncKeyState(ord('H'))
1
有深☉意 2024-11-07 13:20:09

这不是 GUI 编程的工作方式。您不会调用方法来检查按键情况。相反,当按下按键时您会收到发送的消息。假设您有一个正在接收输入的窗口,那么您需要响应到达窗口过程的 WM_KEYDOWN 消息,或者 Python win32api 术语中的消息映射。


您的编辑表明您没有使用消息队列,这是相当不寻常的。您可以通过调用 GetAsyncKeyState()

This isn't how it works in GUI programming. 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.


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().

小傻瓜 2024-11-07 13:20:09

检查 github 上的 python tiler,即使您只是想查找要发送的关键代码,它也非常有用。此外,如果您在后台运行代码并希望从窗口外部中断循环,这也会很有用。

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

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

Check the python tiler on github, very useful even if you are trying to just find key codes to send. 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

code with windows keys:
https://code.google.com/p/python-windows-tiler/source/browse/pwt/hotkey.py?r=df41af2a42b6304047a5f6f1f2903b601b22eb39

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文