python执行windows鼠标和键盘事件

获取屏幕尺寸(非分辨率):

from ctypes import windll

width = windll.user32.GetSystemMetrics(0)
height = windll.user32.GetSystemMetrics(1)
print(width, height)

设置鼠标位置(屏幕尺寸位移的):

from ctypes import windll

windll.user32.SetCursorPos(x, y)

注意:如果想要以像素去偏移,需要计算一下,屏幕尺寸 / 分辨率尺寸 * 像素偏移值

例如:位移到1196,115像素位置

import win32api
from ctypes import windll
import time

import win32con

width = windll.user32.GetSystemMetrics(0)
height = windll.user32.GetSystemMetrics(1)
print(width, height)

x = int(width / 1920 * 1196)
y = int(height / 1080 * 115)

time.sleep(3)
windll.user32.SetCursorPos(x, y)

鼠标点击操作:

import win32api
from ctypes import windll
import time

import win32con

width = windll.user32.GetSystemMetrics(0)
height = windll.user32.GetSystemMetrics(1)
print(width, height)

x = int(width / 1920 * 1196)
y = int(height / 1080 * 115)

time.sleep(3)
# 鼠标点击,先移动鼠标到对应位置
windll.user32.SetCursorPos(x, y)
# 左键按下,0, 0(x, y)由于我是鼠标直接移动过去的,所以当前位置点击下去即可
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
time.sleep(0.05)
# 左键抬起
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

#  左键:win32con.MOUSEEVENTF_LEFTDOWN(按下),win32con.MOUSEEVENTF_LEFTUP(抬起)

#  右键:win32con.MOUSEEVENTF_RIGHTDOWN(按下),win32con.MOUSEEVENTF_RIGHTUP(抬起)

模拟windows键盘事件:

# 首先要知道按键都对应的什么值

对应值参考博客:https://blog.csdn.net/lihuarongaini/article/details/101298063?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-1-101298063.nonecase

HTML键盘事件对应(web远程控制可用的):

1 {'Backspace': 8, 'tab': 9, 'clear': 12, 'Enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, 'pause': 19, 'caps_lock': 20, 'esc': 27, 'spacebar': 32, 'page_up': 33, 'page_down': 34, 'end': 35, 'home': 36, 'left_arrow': 37, 'up_arrow': 38, 'right_arrow': 39, 'down_arrow': 40, 'select': 41, 'print': 42, 'execute': 43, 'print_screen': 44, 'ins': 45, 'del': 46, 'help': 47, '0': 48, '1': 49, '2': 50, '3': 51, '4': 52, '5': 53, '6': 54, '7': 55, '8': 56, '9': 57, 'multiply_key': 106, 'add_key': 107, 'separator_key': 108, 'subtract_key': 109, 'decimal_key': 110, 'divide_key': 111, 'F1': 112, 'F2': 113, 'F3': 114, 'F4': 115, 'F5': 116, 'F6': 117, 'F7': 118, 'F8': 119, 'F9': 120, 'F10': 121, 'F11': 122, 'F12': 123, 'F13': 124, 'F14': 125, 'F15': 126, 'F16': 127, 'F17': 128, 'F18': 129, 'F19': 130, 'F20': 131, 'F21': 132, 'F22': 133, 'F23': 134, 'F24': 135, 'num_lock': 144, 'scroll_lock': 145, 'left_shift': 160, 'right_shift ': 161, 'left_control': 162, 'right_control': 163, 'left_menu': 164, 'right_menu': 165, 'browser_back': 166, 'browser_forward': 167, 'browser_refresh': 168, 'browser_stop': 169, 'browser_search': 170, 'browser_favorites': 171, 'browser_start_and_home': 172, 'volume_mute': 173, 'volume_Down': 174, 'volume_up': 175, 'next_track': 176, 'previous_track': 177, 'stop_media': 178, 'play/pause_media': 179, 'start_mail': 180, 'select_media': 181, 'start_application_1': 182, 'start_application_2': 183, 'attn_key': 246, 'crsel_key': 247, 'exsel_key': 248, 'play_key': 250, 'zoom_key': 251, 'clear_key': 254, '+': 187, ',': 188, '-': 189, '.': 190, '/': 191, '`': 192, ';': 186, '[': 219, '\': 220, ']': 221, "'": 222, '*': 106, '=': 187, '?': 191, '~': 192, ':': 186, 'Tab': 9, 'Shift': 16, 'ArrowLeft': 37, 'ArrowRight': 39, 'ArrowUp': 38, 'ArrowDown': 40, 'CapsLock': 20, 'Control': 17, 'Escape': 27, 'Alt': 18, ' ': 32, 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, 'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F': 70, 'G': 71, 'H': 72, 'I': 73, 'J': 74, 'K': 75, 'L': 76, 'M': 77, 'N': 78, 'O': 79, 'P': 80, 'Q': 81, 'R': 82, 'S': 83, 'T': 84, 'U': 85, 'V': 86, 'W': 87, 'X': 88, 'Y': 89, 'Z': 90}
View Code

import win32api

# 按下

win32api.keybd_event(key_code, 0, 0, 0)

# 抬起

win32api.keybd_event(key_code, 0, win32con.KEYEVENTF_KEYUP, 0)

注意:

1. key_code替换成对应值即可,例如a是65,b是66,空格是32

2. 这个和ascii码不太一样,a和A都是65,由开启大小写决定的

原文地址:https://www.cnblogs.com/zezhou/p/13497193.html