【问题标题】:How to convert values in Entry to win32api Virtual-Key Codes?如何将 Entry 中的值转换为 win32api 虚拟键代码?
【发布时间】:2022-09-23 15:24:30
【问题描述】:

win32api检测按键的代码基于0x01,0x02,0x03等

见:https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

例如 :

if win32api.GetKeyState(0x02) < 0: #will wait for the right mouse button to be pressed to click the left mouse button 

    pyautogui.PAUSE = 0.1
    pyautogui.click() 

我的问题是如何从我们输入密钥的条目中,例如 a,alt,shift 我们如何将这个密钥转换为代码 0x0... 以便它被 win32api 接受?之后能够单击此键

编辑:这是下面组合框中的一个键,但想象一下这是一个条目,我如何将其转换为 0x0... 表单,因此它被接受 赢32api?

  • 我的意思是通常有密钥代码,然后是真正的密钥。就在此处的条目中有真正的钥匙,我们将把它转换成一个钥匙代码,它将被当作真正的钥匙来阅读。这就是它背后的想法
  • 因为如果我写 win32api.GetKeyState(\'a\') 它将不起作用,这就是我想要密钥代码的原因。否则我可以简单地在 StringVar 中使用 get() 检索输入的值,然后将其分配给 GetKeyState 函数,但这显然行不通
  • GetKeyState 接受一个 Int,因此我可以将每个条目值转换为其特定的键码,该键码将在 GetKeyState 中返回所需的键

标签: python pywin32


【解决方案1】:

根据[MS.Docs]: GetKeyState function (winuser.h)

检索指定虚拟键的状态。状态指定键是向上、向下还是切换(打开、关闭——每次按下键时交替)。

...

一个虚拟钥匙。如果所需的虚拟键是字母或数字(A 到 Z、a 到 z 或 0 到 9),nVirtKey必须设置为该字符的 ASCII 值。对于其他键,它必须是虚拟键码。

我不确定我是否得到了问题(左上角带有小东西的大图像对我没有多大帮助),但一切都按预期工作获取密钥状态的一边。如果你想要一个字符-> 键码映射您可以:

需要注意的重要一点是一个以上的键可以产生相同的虚拟键码(例如 1数字键盘 1)

代码00.py

#!/usr/bin/env python

import msvcrt
import sys
import time

import win32api as wapi
import win32con as wcon


def test_gks(key, exit_key=0x1B):
    print("GetKeyState")
    while True:
        res = wapi.GetKeyState(key)
        down = (res >> 8) < 0
        toggled = res & 0x01
        print("Key 0x{:02X} is: down: {:d}, toggled {:d}".format(key, down, toggled))
        if msvcrt.kbhit():
            k = ord(msvcrt.getch())
            if k == exit_key:
                break
        time.sleep(0.5)


def test_vks(exit_key=0x1B):
    print("VkKeyScan")
    while True:
        if msvcrt.kbhit():
            kc = msvcrt.getch()
            ki = ord(kc)
            if ki == exit_key:
                break
            print("Key {:} (ASCII {:d}) VK: 0x{:04X}".format(kc, ki, wapi.VkKeyScan(kc)))
        time.sleep(0.5)


def main(*argv):
    print("Press <Esc> to exit...")
    if argv:
        key = wcon.VK_RBUTTON
        #key = 0x41  # "A"
        test_gks(key)
    else:
        test_vks()


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

输出

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q073807251]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py dummy
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32

Press <Esc> to exit...
GetKeyState
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 1, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 0, toggled 0
Key 0x02 is: down: 1, toggled 1
Key 0x02 is: down: 1, toggled 1
Key 0x02 is: down: 1, toggled 1
Key 0x02 is: down: 1, toggled 1
Key 0x02 is: down: 1, toggled 1
Key 0x02 is: down: 1, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1
Key 0x02 is: down: 0, toggled 1

Done.


[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32

Press <Esc> to exit...
VkKeyScan
Key b'a' (ASCII 97) VK: 0x0041
Key b'A' (ASCII 65) VK: 0x0141
Key b'1' (ASCII 49) VK: 0x0031
Key b'!' (ASCII 33) VK: 0x0131
Key b' ' (ASCII 32) VK: 0x0020
Key b'1' (ASCII 49) VK: 0x0031
Key b' ' (ASCII 32) VK: 0x0020
Key b'\xe0' (ASCII 224) VK: 0x-001
Key b'H' (ASCII 72) VK: 0x0148
Key b' ' (ASCII 32) VK: 0x0020
Key b'\x00' (ASCII 0) VK: 0x0332
Key b';' (ASCII 59) VK: 0x00BA
Key b' ' (ASCII 32) VK: 0x0020
Key b'\t' (ASCII 9) VK: 0x0009
Key b'\x08' (ASCII 8) VK: 0x0008
Key b' ' (ASCII 32) VK: 0x0020
Key b'\r' (ASCII 13) VK: 0x000D

Done.

运行:

  • 1英石:鼠标右键监控。
    我单击它一次(切换),然后再次(切换回)然后按住它几秒钟(连续下降)。请注意,有时在按下按钮后,由于某种原因,它会不断生成事件,所以按下Esc键不会工作(Ctrl+C必需的)

  • 2nd: 键 (仅键盘)。
    序列:一个,转移+一个,1,转移+1,空间,数字键盘 1,空间,向上箭头,空间,F1,空间,标签,Bk空间,空间,进入,Esc键

【讨论】:

  • 感谢您的示例,但您正在使用自己的案例对其进行测试?难道我不必把所有这些都只是为了将条目中输入的值转换为它的虚拟代码(来自win32api)吗?因此,当我使用 get() 获取条目的值时,我得到的是虚拟代码而不是字母,尽管虚拟代码描述了字母。但是您不会要求用户输入例如 0x48 而是在条目中输入 h。 H 将被检索为 0X48,GetKeyState 函数中不会出现错误。
  • 嗯,显然我不明白这个问题(我不确定我现在是否明白了)。所以你需要从A转换为0x41?你有你需要的钥匙列表吗?
猜你喜欢
  • 2018-09-20
  • 1970-01-01
  • 2010-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
相关资源
最近更新 更多