Python模拟键盘输入

Python笔记 Python 2020-04-18 阅读:170

一、用Python实现模拟键盘输入

代码中用到的键值码,参考文末表格

1、随机输入字符

1-1、随机输入a~z

def random_abc():
    key = random.randint(69, 86)
    win32api.keybd_event(key, 0, 0, 0)
    win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放

1-2、随机输入0~9

def random_123():
    key = random.randint(96, 105)
    win32api.keybd_event(key, 0, 0, 0)
    win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放

2、有序输入字符

2-1、有序输出数字

例如:需要输入次数为100次,就模拟键盘输入1~100的数字。代码中 times = 次数

DIC = dict(zip(list(range(0, 10)), list(range(96, 106))))


def order_123(times=None):
    global DIC
    for i in range(1, times+1):
        single = list(str(i))
        for j in single:
            key = DIC[int(j)]
            win32api.keybd_event(key, 0, 0, 0)
            win32api.keybd_event(key, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放
        win32api.keybd_event(13, 0, 0, 0)
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放

二、常见的键值码和键

表格转载自:https://blog.csdn.net/u010620152/article/details/55258350

常数名称十六进制值十进制值对应按键
VK_LBUTTON011鼠标的左键
VK_RBUTTON022鼠标的右键
VK-CANCEL033Ctrl+Break(通常不需要处理)
VK_MBUTTON044鼠标的中键(三按键鼠标)
VK_BACK088Backspace键
VK_TAB099Tab键
VK_CLEAR0C12Clear键(Num Lock关闭时的数字键盘5)
VK_RETURN0D13Enter键
VK_SHIFT1016Shift键
VK_CONTROL1117Ctrl键
VK_MENU1218Alt键
VK_PAUSE1319Pause键
VK_CAPITAL1420Caps Lock键
VK_ESCAPE1B27Ese键
VK_SPACE2032Spacebar键
VK_PRIOR2133Page Up键
VK_NEXT2234Page Domw键
VK_END2335End键
VK_HOME2436Home键
VK_LEFT2537LEFT ARROW 键(←)
VK_UP2638UP ARROW键(↑)
VK_RIGHT2739RIGHT ARROW键(→)
VK_DOWN2840DOWN ARROW键(↓)
VK_Select2941Select键
VK_PRINT2A42Print键
VK_EXECUTE2B43EXECUTE键
VK_SNAPSHOT2C44Print Screen键(抓屏)
VK_Insert2D45Ins键(Num Lock关闭时的数字键盘0)
VK_Delete2E46Del键(Num Lock关闭时的数字键盘.)
VK_HELP2F47Help键
VK_030480键
VK_131491键
VK_232502键
VK_333513键
VK_434524键
VK_535535键
VK_636546键
VK_737557键
VK_838568键
VK_939579键
VK_A4165A键
VK_B4266B键
VK_C4367C键
VK_D4468D键
VK_E4569E键
VK_F4670F键
VK_G4771G键
VK_H4872H键
VK_I4973I键
VK_J4A74J键
VK_K4B75K键
VK_L4C76L键
VK_M4D77M键
VK_N4E78N键
VK_O4F79O键
VK_P5080P键
VK_Q5181Q键
VK_R5282R键
VK_S5383S键
VK_T5484T键
VK_U5585U键
VK_V5686V键
VK_W5787W键
VK_X5888X键
VK_Y5989Y键
VK_Z5A90Z键
VK_NUMPAD06096数字键0键
VK_NUMPAD16197数字键1键
VK_NUMPAD26298数字键2键
VK_NUMPAD36299数字键3键
VK_NUMPAD464100数字键4键
VK_NUMPAD565101数字键5键
VK_NUMPAD666102数字键6键
VK_NUMPAD767103数字键7键
VK_NUMPAD868104数字键8键
VK_NUMPAD969105数字键9键
VK_MULTIPLY6A106数字键盘上的*键
VK_ADD6B107数字键盘上的+键
VK_SEPARATOR6C108Separator键
VK_SUBTRACT6D109数字键盘上的-键
VK_DECIMAL6E110数字键盘上的.键
VK_DIVIDE6F111数字键盘上的/键
VK_F170112F1键
VK_F271113F2键
VK_F372114F3键
VK_F473115F4键
VK_F574116F5键
VK_F675117F6键
VK_F776118F7键
VK_F877119F8键
VK_F978120F9键
VK_F1079121F10键
VK_F117A122F11键
VK_F127B123F12键
VK_NUMLOCK90144Num Lock 键
VK_SCROLL91145Scroll Lock键
上面没有提到的:(都在大键盘)
VK_LWIN 91左win键
VK_RWIN 92右win键
VK_APPS 93右Ctrl左边键,点击相当于点击鼠标右键,会弹出快捷菜单
186;(分号)
187=键
188,键(逗号)
189-键(减号)
190.键(句号)
191/键
192`键(Esc下面)
219[键
220
221]键
222‘键(引号)
版权声明

本文基于《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
文章链接:https://tooler.club/archives/python-simulated-keyboard-input.html (转载时请注明本文出处及文章链接)

如果觉得我的文章对你有用,请随意赞赏

0条评论