pynput 监听多组合键
def listener_mouse(self):
self.keyoard_listener = pynput.keyboard.Listener(on_press=self.keyboard_input_press, on_release=self.keyboard_input_release) self.mouse_listener.start() self.keyoard_listener.start() self.mouse_listener.join() self.keyoard_listener.join() return def keyboard_input_release(self, key): # 按键释放 if self.press_list: print(self.press_list) if len(self.press_list) == 1: if self.release_flag: self.mouse_keyboard_move.append(str(self.press_list)) else: self.release_flag = True self.press_list.remove(str(key)) elif len(self.press_list) == 2: self.mouse_keyboard_move.append(str(self.press_list)) if key in self.press_list: self.press_list.remove(str(key)) self.release_flag = False else: self.mouse_keyboard_move.append(str(self.press_list)) self.press_list.clear() self.release_flag = False if key == pynput.keyboard.Key.esc: self.stop_listener_keyoard() with open('aa.txt', "w") as f: for i in self.mouse_keyboard_move: f.write(str(i) + ';') f.close() def keyboard_input_press(self, key): # 按键按下 self.press_list.add(str(key))
直接上代码了,组合键用集合保存,写入txt中,但后来发现个问题,由于大量逻辑在按键释放时判断,对造成程序在此处相对的处理速度很慢,如果打字快点的话就会出现顺序颠倒的情况,难办。
部分结果(woshishui?nizhidaoma?):
"'w'"};{"'o'"};{'Key.space'};{"'s'", "'h'"};{"'s'", "'i'", "'h'"};{"'s'"};{"'h'"};{"'i'", "'u'"};{"'i'", "'u'"};{'Key.space', "'i'", "'u'"};{"'a'", 'Key.shift'};{"'a'", 'Key.shift'};{'Key.shift_r', "'a'", "'/'", 'Key.shift'};{"'i'", "'n'"};{"'i'", "'n'"};{'Key.space', "'i'", "'n'"};{"'s'", "'h'"};{"'s'", "'i'", "'h'"};{"'d'"};{"'a'"};{"'o'"};{'Key.space'};{"'m'"};{"'a'"};{'Key.space'};{'Key.shift_r', "'/'"};{'Key.shift_r', "'/'"};{'Key.shift_r', "'/'", 'Key.esc'}
以前-好记性不如烂笔头
现在-好记性不如烂键盘