检索指定虚拟键的状态。状态指定键是向上、向下还是切换(打开、关闭——每次按下键时交替)。
...
一个虚拟钥匙。如果所需的虚拟键是字母或数字(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键