當前位置: 首頁>>代碼示例>>Python>>正文


Python Listener.add_listener方法代碼示例

本文整理匯總了Python中listener.Listener.add_listener方法的典型用法代碼示例。如果您正苦於以下問題:Python Listener.add_listener方法的具體用法?Python Listener.add_listener怎麽用?Python Listener.add_listener使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在listener.Listener的用法示例。


在下文中一共展示了Listener.add_listener方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: KMLoger

# 需要導入模塊: from listener import Listener [as 別名]
# 或者: from listener.Listener import add_listener [as 別名]
class KMLoger(threading.Thread):
    """ Classe principal """
    def __init__(self):
        threading.Thread.__init__(self)
        self.devices = get_devices()
        self.keyfile = ".keymap"    # Arquivo padrão para o mapa de caracteres
        self.keymap = KeyMap(self.devices)
        self.listener = Listener(self.devices)
        self.history = History()
        self.lock = False
        self.listen()
        self.copy = False

    def record(self):
        if self.copy:
            self.copy = False
            print("\rComandos gravados" + " "*80)
            self.listener.del_key_sniffer("rec")
            # self.listener.del_mouse_sniffer("mouse")
        else:
            self.copy = True
            print("\rIniciando gravação" + " "*80)
            self.history.reset()
            self.listener.add_key_sniffer("rec", [self.keymap.get_key_by_func("rec")], self.history.append_event)
            # self.listener.add_mouse_sniffer("mouse", self.history.append_event)

    def pprint(self, *args):
        if not self.copy:
            print(args)

    def listen(self):
        """ Inicializando todas as fun """
        self.listener.add_listener("redefine", ecodes.EV_KEY, self.keymap.get_key_by_func("redefine"), self.change_keys)
        
        self.listener.add_listener("quit", ecodes.EV_KEY, self.keymap.get_key_by_func("quit"), self.exit)
        self.listener.add_listener("copy", ecodes.EV_KEY, self.keymap.get_key_by_func("rec"), self.record)
        self.listener.add_listener("run", ecodes.EV_KEY, self.keymap.get_key_by_func("run"), self.play)
        # self.listener.add_listener("save", ecodes.EV_KEY, self.keymap.get_key_by_func("save"), self.history.save)
        # self.listener.add_listener("load", ecodes.EV_KEY, self.keymap.get_key_by_func("load"), self.history.load)
        # self.listener.add_listener("pause", ecodes.EV_KEY, self.keymap.get_key_by_func("pause"), self.history.sleep)
        # self.listener.add_listener("stop", ecodes.EV_KEY, self.keymap.get_key_by_func("stop"), self.history.stop)
        # self.listener.add_listener("run_forever", ecodes.EV_KEY, self.keymap.get_key_by_func("run_forever"), self.play_forever)
        # self.listener.add_key_sniffer("rec", [self.keymap.get_key_by_func("rec")], self.history.append_event)

        
        # self.listener.add_listener("copy", ecodes.EV_KEY, self.keymap.get_key_by_func("rec"), pprint, "record")
        self.listener.add_listener("save", ecodes.EV_KEY, self.keymap.get_key_by_func("save"), self.pprint, "save")
        self.listener.add_listener("load", ecodes.EV_KEY, self.keymap.get_key_by_func("load"), self.pprint, "load")
        self.listener.add_listener("pause", ecodes.EV_KEY, self.keymap.get_key_by_func("pause"), self.pprint, "sleep")
        self.listener.add_listener("stop", ecodes.EV_KEY, self.keymap.get_key_by_func("stop"), self.pprint, "stop")
        # self.listener.add_listener("run", ecodes.EV_KEY, self.keymap.get_key_by_func("run"), self.pprint, "play")
        self.listener.add_listener("run_forever", ecodes.EV_KEY, self.keymap.get_key_by_func("run_forever"), self.pprint, "play_forever")
        

    def play(self):
    	if not self.copy:
    		print("\rplay" + " "*80)
	        self.history.set_n(1)
	        self.history.play()

    def play_forever(self):
    	if not self.copy:
	        self.history.set_n(0)
	        self.history.play()

    def run(self):
        self.history.start()
        self.listener.run()
        self.history.join()

    def save_key_map(self):
        """ Salva o mapa de caracteres no arquivo padrão. """
        pickle.dump( self.keymap, open(self.keyfile, 'wb'), -1)
    
    def exit(self):
        if not self.copy:
            self.history.exit()
            self.listener.exit()
            flush_input()
            print("\nBye!")

    def load_key_map(self):
        """ Carrega o mapa de caracteres do arquivo padrão. """
        try:
            self.keymap = pickle.load(open(self.keyfile, 'rb'))
        except IOError:
            self.save_key_map()
        self.listen()

    def change_keys(self):
        if not self.copy:
            self.keymap.setup()
            print("\rNovo mapa de teclas salvo.")
            self.save_key_map()
            print("\rPrograma precisa ser reiniciado")
            self.exit()

    def print_keys(self):
        self.keymap.print_keymap()
開發者ID:andredalton,項目名稱:bcc,代碼行數:101,代碼來源:kmlogger.py


注:本文中的listener.Listener.add_listener方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。