-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathbutton.py
More file actions
67 lines (55 loc) · 1.74 KB
/
button.py
File metadata and controls
67 lines (55 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# coding=gbk
import ctypes
import time
class KeyboardInputKi(ctypes.Structure):
_fields_ = [
("wVk", ctypes.c_ushort),
("wScan", ctypes.c_ushort),
("dwFlags", ctypes.c_ulong),
("time", ctypes.c_ulong),
("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong)),
('unused', ctypes.c_ubyte * 8)
]
class KeyboardInput(ctypes.Structure):
_fields_ = [
("type", ctypes.c_ulong),
("ki", KeyboardInputKi)
]
def drive_button(vk_code: int, send_type: int, func_type: bool):
"""
驱动按键
按键码
按键方式 0按下+抬起 1按下 2抬起
功能键方式 true为长按
:param vk_code: int
:param send_type:int
:param func_type: bool
:return:
"""
ki = KeyboardInputKi()
ki.wVk = vk_code
ki.wScan = ctypes.windll.user32.MapVirtualKeyW(vk_code, 0)
ki.dwExtraInfo = ctypes.pointer(ctypes.c_ulong(0))
if send_type == 0 or send_type == 1:
ki.dwFlags = 1 if func_type else 0
ki.time = int(time.time() * 1000)
inp = KeyboardInput(type=1, ki=ki)
ctypes.windll.user32.SendInput(1, ctypes.pointer(inp), ctypes.sizeof(inp))
time.sleep(0.01)
if send_type == 0 or send_type == 2:
ki.dwFlags = 3 if func_type else 2
ki.time = int(time.time() * 1000)
inp = KeyboardInput(type=1, ki=ki)
ctypes.windll.user32.SendInput(1, ctypes.pointer(inp), ctypes.sizeof(inp))
time.sleep(0.01)
def get_key_state(key_code: int) -> bool:
"""
按键状态
:param key_code:
:return:
"""
get_key_state_api = ctypes.windll.user32.GetKeyState
get_key_state_api.argtypes = [ctypes.c_int]
get_key_state_api.restype = ctypes.c_short
state = get_key_state_api(key_code)
return state & 0x8000 != 0