zoukankan      html  css  js  c++  java
  • 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'}
  • 相关阅读:
    Oracle11g新建用户及用户表空间
    PLSQL连接oracle12c
    Zabbix微信报警脚本及写触发记录
    使用document.select(Jquery Css selector) selector:看jsoup文档来敲案例学习 selector选择器
    仙女打卡day1
    XML的解析(读取),附带源码与运行效果图 (day01)
    [Luogu] P7077 函数调用
    [Luogu] P5815 [CQOI2010]扑克牌
    [Luogu] CF364D Ghd
    [Luogu] P4823 [TJOI2013]拯救小矮人
  • 原文地址:https://www.cnblogs.com/gexbooks/p/12768230.html
Copyright © 2011-2022 走看看