程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python win32API simulates keyboard operation events

編輯:Python
import json
from time import sleep
import requests
import win32api
import win32con
key_map = {
"0": 49, "1": 50, "2": 51, "3": 52, "4": 53, "5": 54, "6": 55, "7": 56, "8": 57, "9": 58,
'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,
"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,
'BACKSPACE': 8, 'TAB': 9, 'TABLE': 9, 'CLEAR': 12,
'ENTER': 13, 'SHIFT': 16, 'CTRL': 17,
'CONTROL': 17, 'ALT': 18, 'ALTER': 18, 'PAUSE': 19, 'BREAK': 19, 'CAPSLK': 20, 'CAPSLOCK': 20, 'ESC': 27,
'SPACE': 32, 'SPACEBAR': 32, 'PGUP': 33, 'PAGEUP': 33, 'PGDN': 34, 'PAGEDOWN': 34, 'END': 35, 'HOME': 36,
'LEFT': 37, 'UP': 38, 'RIGHT': 39, 'DOWN': 40, 'SELECT': 41, 'PRTSC': 42, 'PRINTSCREEN': 42, 'SYSRQ': 42,
'SYSTEMREQUEST': 42, 'EXECUTE': 43, 'SNAPSHOT': 44, 'INSERT': 45, 'DELETE': 46, 'HELP': 47, 'WIN': 91,
'WINDOWS': 91, 'NMLK': 144,
'NUMLK': 144, 'NUMLOCK': 144, 'SCRLK': 145,
'[': 219, ']': 221, '+': 107, '-': 109}
combine_keys = {
('CONTROL','G'): " Create a combination ", ('CONTROL','ALT','G'): " Create as a Sketchpad ", ('CONTROL','ALT','K'): " Create components ", ('CONTROL','E'): " Path splicing ",
('CONTROL','SHIFT','G'): " Ungroup ", ('CONTROL','ALT','M'): " Create a mask ", ('CONTROL','SHIFT','O'): " Outline Stroke ",
('CONTROL',']'): " Move up one level ", ('CONTROL','['): " Move down one level ",('CONTROL','SHIFT',']'): " Move to top ", ('CONTROL','SHIFT','['): " Move to the bottom ",
('CONTROL','SHIFT','H'): " Show / Hide Layers ", ('CONTROL','SHIFT','L'): " lock / Unlock ",('SHIFT','H'): " Flip horizontal ", ('SHIFT','V'): " Flip vertically ",
('DELETE'): " Delete ", ('SHIFT','L'): " arrow ", ('CONTROL','ALT','SHIFT','V'): " Vertical equidistant distribution ", ('CONTROL','ALT','SHIFT','H'): " Horizontal equidistant distribution "
}
def release_key(key_code):
"""
The functionality : Lift the button
ginseng Count :key: Key value
"""
win32api.keybd_event(key_code, win32api.MapVirtualKey(key_code, 0), win32con.KEYEVENTF_KEYUP, 0)
def press_key(key_code):
"""
The functionality : Press the button
ginseng Count :key: Key value
"""
win32api.keybd_event(key_code, win32api.MapVirtualKey(key_code, 0), 0, 0)
def press_and_release_key(key_code):
"""
Press the button
:param key_code: Key value , Such as 91, representative WIN windows System key of the system , Pop up start menu
:return:
"""
press_key(key_code)
release_key(key_code)
def pressKey(key):
"""
Click the key ( Press and lift )
:param key: Key , Such as :F5,ENTER
:return:
"""
if isinstance(key, str):
press_and_release_key(key_map[key.upper()])
elif isinstance(key, int):
press_and_release_key(key)
def press_keys(*args):
"""
Press the key combination Multiple characters are supported , Array , A tuple type
:param args: for example : ALT,TAB
:return:
"""
for i in args:
if isinstance(i, str):
press_key(key_map.get(i))
sleep(0.3)
elif isinstance(i, list):
[press_keys(n) for n in i]
elif isinstance(i, tuple):
[press_keys(n) for n in i]
def release_keys(*args):
"""
Release the key combination Multiple characters are supported , Array , A tuple type
:param args: for example :ALT,TAB
:return:
"""
for i in args:
if isinstance(i, str):
release_key(key_map.get(i))
elif isinstance(i, list):
[release_keys(n) for n in i]
elif isinstance(i, tuple):
[release_keys(n) for n in i]
def press_release_keys(*args):
"""
Press pause for one second and release the key combination , Multiple characters are supported , Array , A tuple type
:param args: for example :ALT,TAB
:return:
"""
press_keys(*args)
sleep(2)
release_keys(*args)
if __name__ == '__main__':
# pressKey('WIN') # Press down windows System key of the system , Pop up start menu
# # Analog combination button 9:tap key ,18:alt key
# press_key(18) # Press down alt key
# press_key(9) # Press down tap key
# release_key(18) # Release alt key
# release_key(9) # Release tap key
# press_key(91)
# press_key(16)
# press_key(83)
# sleep(3)
# release_key(91)
# release_key(16)
# release_key(83)
list_g = {('CONTROL', 'G'):'eee', ('ALT', 'TAB'):'ddd'}
[press_release_keys(n) for n in list_g]
class Test(object):
def keybd_event(self, VK_CODE): # VK_CODE Code the keyboard
# @Keyboard
# input
# R:82 L:76 O:79
VK_CODE = int(VK_CODE)
print(":::VK_CODE:", VK_CODE)
win32api.keybd_event(VK_CODE, 0, 0, 0)
win32api.keybd_event(VK_CODE, 0, win32con.KEYEVENTF_KEYUP, 0)
print(":::press", str(VK_CODE), "successfully!")
def press_keys(self, *args):
for i in args:
press_key(key_map.get(i))
def release_keys(self, *args):
for i in args:
release_key(key_map.get(i))


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved