layout: post
title: python实现键盘控制
description: python实现键盘控制,屏蔽了虚拟按键检测
categories:

  • python
    tags:
  • python

python实现键盘控制

一、代码实现

import ctypes
import time

#键码表
VK_LBUTTON = 0x01        # 鼠标左键
VK_RBUTTON = 0x02        # 鼠标右键
VK_CANCEL = 0x03         # Control-break processing
VK_MBUTTON = 0x04        # 鼠标中键
VK_XBUTTON1 = 0x05       # 鼠标第一个扩展按钮
VK_XBUTTON2 = 0x06       # 鼠标第二个扩展按钮
VK_BACK = 0x08           # Backspace键
VK_TAB = 0x09            # Tab键
VK_CLEAR = 0x0C          # Clear键
VK_RETURN = 0x0D         # Enter键
VK_SHIFT = 0x10          # Shift键
VK_CONTROL = 0x11        # Ctrl键
VK_MENU = 0x12           # Alt键
VK_PAUSE = 0x13          # Pause键
VK_CAPITAL = 0x14        # Caps Lock键
VK_ESCAPE = 0x1B         # Esc键
VK_SPACE = 0x20          # Space键
VK_PRIOR = 0x21          # Page Up键
VK_NEXT = 0x22           # Page Down键
VK_END = 0x23            # End键
VK_HOME = 0x24           # Home键
VK_LEFT = 0x25           # 左箭头键
VK_UP = 0x26             # 上箭头键
VK_RIGHT = 0x27          # 右箭头键
VK_DOWN = 0x28           # 下箭头键
VK_SELECT = 0x29         # Select键
VK_PRINT = 0x2A          # Print键
VK_EXECUTE = 0x2B        # Execute键
VK_SNAPSHOT = 0x2C       # Print Screen键
VK_INSERT = 0x2D         # Ins键
VK_DELETE = 0x2E         # Del键
VK_HELP = 0x2F           # Help键
VK_0 = 0x30              # 0键
VK_1 = 0x31              # 1键
VK_2 = 0x32              # 2键
VK_3 = 0x33              # 3键
VK_4 = 0x34              # 4键
VK_5 = 0x35              # 5键
VK_6 = 0x36              # 6键
VK_7 = 0x37              # 7键
VK_8 = 0x38              # 8键
VK_9 = 0x39              # 9键
VK_A = 0x41              # A键
VK_B = 0x42              # B键
VK_C = 0x43              # C键
VK_D = 0x44              # D键
VK_E = 0x45              # E键
VK_F = 0x46              # F键
VK_G = 0x47              # G键
VK_H = 0x48              # H键
VK_I = 0x49              # I键
VK_J = 0x4A              # J键
VK_K = 0x4B              # K键
VK_L = 0x4C              # L


# 定义键盘输入事件结构体
class INPUT(ctypes.Structure):
    _fields_ = [
        ("type", ctypes.c_ulong),
        ("ki", ctypes.c_void_p)
    ]

# 定义键盘输入数据结构体
class KEYBDINPUT(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))
    ]

# 定义键盘输入标志
KEYEVENTF_KEYDOWN = 0x0
KEYEVENTF_KEYUP = 0x2

# 定义虚拟键码
VK_A = 0x41
VK_B = 0x42

# 发送键盘输入事件
def send_input_event(virtual_keycode, key_event_flag):
    # 构造键盘输入数据结构体
    keyboard_input = KEYBDINPUT()
    keyboard_input.wVk = virtual_keycode
    keyboard_input.dwFlags = key_event_flag

    # 构造键盘输入事件结构体
    input_event = INPUT()
    input_event.type = 1
    input_event.ki = ctypes.pointer(keyboard_input)

    # 发送键盘输入事件
    ctypes.windll.user32.SendInput(1, ctypes.pointer(input_event), ctypes.sizeof(input_event))

# 模拟按下键
def press_key(virtual_keycode):
    send_input_event(virtual_keycode, KEYEVENTF_KEYDOWN)

# 模拟弹起键
def release_key(virtual_keycode):
    send_input_event(virtual_keycode, KEYEVENTF_KEYUP)

# 模拟按键操作
def simulate_key_press(virtual_keycode):
    press_key(virtual_keycode)
    time.sleep(0.1) # 模拟按键操作时间
    release_key(virtual_keycode)

# 测试程序
def main():
    simulate_key_press(VK_A) # 模拟按下和弹起A键
    simulate_key_press(VK_B) # 模拟按下和弹起B键

if __name__ == '__main__':
    main()