基础类封装-键盘类操作库封装

  1 import win32api
  2 import win32con
  3 import win32clipboard as w
  4 
  5 class KeyboardKeys():
  6     """Analog keyboard keys class"""
  7     VK_CODE = {
  8         'backspace': 0x08,
  9         'tab': 0x09,
 10         'clear': 0x0c,
 11         'enter': 0x0D,
 12         'shift': 0x10,
 13         'ctrl': 0x11,
 14         'alt': 0x12,
 15         'pause': 0x13,
 16         'caps_lock': 0x14,
 17         'esc': 0x1B,
 18         'spacebar': 0x20,
 19         'page_up': 0x21,
 20         'page_down': 0x22,
 21         'end': 0x23,
 22         'home': 0x24,
 23         'left_arrow': 0x25,
 24         'up_arrow': 0x26,
 25         'right_arrow': 0x27,
 26         'down_arrow': 0x28,
 27         'select': 0x29,
 28         'print': 0x2A,
 29         'execute': 0x2B,
 30         'print_screen': 0x2C,
 31         'ins': 0x2D,
 32         'del': 0x2E,
 33         'help': 0x2F,
 34         '0': 0x30,
 35         '1': 0x31,
 36         '2': 0x32,
 37         '3': 0x33,
 38         '4': 0x34,
 39         '5': 0x35,
 40         '6': 0x36,
 41         '7': 0x37,
 42         '8': 0x38,
 43         '9': 0x39,
 44         'a': 0x41,
 45         'b': 0x42,
 46         'c': 0x43,
 47         'd': 0x44,
 48         'e': 0x45,
 49         'f': 0x46,
 50         'g': 0x47,
 51         'h': 0x48,
 52         'i': 0x49,
 53         'j': 0x4A,
 54         'k': 0x4B,
 55         'l': 0x4C,
 56         'm': 0x4D,
 57         'n': 0x4E,
 58         'o': 0x4F,
 59         'p': 0x50,
 60         'q': 0x51,
 61         'r': 0x52,
 62         's': 0x53,
 63         't': 0x54,
 64         'u': 0x55,
 65         'v': 0x56,
 66         'w': 0x57,
 67         'x': 0x58,
 68         'y': 0x59,
 69         'z': 0x5A,
 70         'numpad_0': 0x60,
 71         'numpad_1': 0x61,
 72         'numpad_2': 0x62,
 73         'numpad_3': 0x63,
 74         'numpad_4': 0x64,
 75         'numpad_5': 0x65,
 76         'numpad_6': 0x66,
 77         'numpad_7': 0x67,
 78         'numpad_8': 0x68,
 79         'numpad_9': 0x69,
 80         'multiply_key': 0x6A,
 81         'add_key': 0x6B,
 82         'separator_key': 0x6C,
 83         'subtract_key': 0x6D,
 84         'decimal_key': 0x6E,
 85         'divide_key': 0x6F,
 86         'F1': 0X70,
 87         'F2': 0X71,
 88         'F3': 0X72,
 89         'F4': 0X73,
 90         'F5': 0X74,
 91         'F6': 0X75,
 92         'F7': 0X76,
 93         'F8': 0X77,
 94         'F9': 0X78,
 95         'F10': 0X79,
 96         'F11': 0X7A,
 97         'F12': 0X7B,
 98         'F13': 0X7C,
 99         'F14': 0X7D,
100         'F15': 0X7E,
101         'F16': 0X7F,
102         'F17': 0X80,
103         'F18': 0X81,
104         'F19': 0X82,
105         'F20': 0X83,
106         'F21': 0X84,
107         'F22': 0X85,
108         'F23': 0X86,
109         'F24': 0X87,
110         'num_lock': 0x90,
111         'scroll_lock': 0x91,
112         'left_shift': 0xA0,
113         'right_shift': 0xA1,
114         'left_control': 0xA2,
115         'right_control':0xA3,
116         'left_menu': 0xA4,
117         'right_menu': 0xA5,
118         'browser_back':0xA6,
119         'browser_forward': 0xA7,
120         'browser_refresh': 0xA8,
121         'browser_stop': 0xA9,
122         'browser_search': 0xAA,
123         'browser_favorites': 0xAB,
124         'browser_start_and_home': 0xAC,
125         'voleme_mute': 0xAD,
126         'voleme_down': 0xAE,
127         'voleme_up': 0xAF,
128         'next_track': 0xB1,
129         'stop_media': 0xB2,
130         'play/pause_media': 0xB3,
131         'start_mail': 0xB4,
132         'select_media': 0xB5,
133         'start_application_1': 0xB6,
134         'start_application_2': 0xB7,
135         'attn_key': 0xF6,
136         'crsel_key': 0xF7,
137         'exsel_key': 0xF8,
138         'play_key': 0xFA,
139         'zoom_key': 0xFB,
140         'ckear_key': 0xFE,
141         '+': 0xBB,
142         ',': 0xBC,
143         '-': 0xBD,
144         '.': 0xBE,
145         '/': 0xBF,
146         '`': 0xC0,
147         ';': 0xBA,
148         '[': 0xDB,
149         '\\': 0xDC,
150         ']': 0xDD,
151         "'": 0xDE
152     }
153 
154     @staticmethod
155     def keyDown(keyName):
156         """按下按键"""
157         win32api.keybd_event(KeyboardKeys.VK_CODE[keyName], 0, 0, 0)
158 
159     @staticmethod
160     def keyUp(keyName):
161         """释放按键"""
162         win32api.keybd_event(KeyboardKeys.VK_CODE[keyName], 0,win32con.KEYEVENTF_KEYUP, 0)
163 
164     @staticmethod
165     def oneKey(key):
166         """模拟单个按键,按下并释放"""
167         KeyboardKeys.keyDown(key)
168         KeyboardKeys.keyUp(key)
169 
170     @staticmethod
171     def twoKeys(key1,key2):
172         """模拟两个按键"""
173         KeyboardKeys.keyDown(key1)
174         KeyboardKeys.keyDown(key2)
175         KeyboardKeys.keyUp(key2)
176         KeyboardKeys.keyUp(key1)
177 
178 
179 
180 
181 if __name__ == "__main__":
182     test = KeyboardKeys()
183     test.setText(u'my name is tyw')
184     print(test.getText().decode('gbk').encode('utf-8'))

 

posted @ 2020-04-26 20:22  此去经年归来仍是少年  阅读(282)  评论(0编辑  收藏  举报