文中键盘监听部分借鉴了cnblog的‘十一月_寒风’大佬的文章python监听键盘和鼠标事件
from win32gui import*
from time import sleep
from random import randint
from threading import Thread
from math import*
from commctrl import*
from ctypes import *
from ctypes import wintypes
import win32con
SetWindowsHookEx=windll.user32.SetWindowsHookExA
UnhookWindowsHookEx=windll.user32.UnhookWindowsHookEx
CallNextHookEx=windll.user32.CallNextHookEx
GetMessage=windll.user32.GetMessageA
GetModuleHandle=windll.kernel32.GetModuleHandleW
#保存键盘钩子函数句柄
keyboard_hd = None
#保存按键
key = None
class KBDLLHOOKSTRUCT(Structure):
_fields_ = [
('vkCode',c_int),
('scanCode', c_int),
('flags', c_int),
('time', c_int),
('dwExtraInfo', c_uint),
('',c_void_p)
]
def wait_for_msg():
msg = wintypes.MSG()
GetMessage(msg, 0, 0, 0)
def keyboard_pro(nCode, wParam, lParam):
"""
函数功能:键盘钩子函数,当有按键按下时此函数被回调
"""
global key
if nCode == win32con.HC_ACTION:
KBDLLHOOKSTRUCT_p = POINTER(KBDLLHOOKSTRUCT)
param=cast(lParam,KBDLLHOOKSTRUCT_p)