<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                在之前寫的一篇[《Python:監控鍵盤輸入、鼠標操作,并將捕獲到的信息記錄到文件中》](http://blog.csdn.net/dyx1024/article/details/7311013)文章中,有個讀者留言如下: ![](https://box.kancloud.cn/2016-06-08_575793590ab34.gif) 這看似一個很平常的需求,但實現起來并不容易,如果用快捷鍵來控制一個程序干些別的事情那是非常容易的,但關鍵是本程序剛好是用hook來監控鍵盤,所以必須使用PumpMessages(),而此函數使用當前程序進入消息循環,它抓取每個鼠標和鍵盤事件。當我們的程序跑起來后,按下停止的熱鍵時,也被此函數捕獲,所以定義的任何熱鍵均不能生效,具體實現及測試在文章[《Python:通過自定義系統級快捷鍵來控制程序運行》](http://blog.csdn.net/dyx1024/article/details/7335085)中有所描述。 現在,我們換一個思路,既然已經監控到了按鍵,那就判斷當前的按鍵是不是預先定義的熱鍵,如果是,則可調用自己的處理函數,這樣就找到了一個控制的入口,可以通過它實現我們想要的功能,注意此時不能讓程序調用os.exit(0)讓程序退出,否則再次按啟動熱鍵時就沒法玩了。具體實現如下,代碼中有詳細注釋,不再一一解釋。 ### 一、代碼: ~~~ #!/usr/bin/env python # -*- coding: utf-8 -*- import pythoncom import pyHook import time import pyhk import os import sys import ctypes from ctypes import wintypes import win32con import win32api class CInspectKeyAndMouseEvent: ''' Function:鍵盤和鼠標監控類 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-09 ''' def __init__(self, filename): '初始化' self.filename = filename def open_file(self): '打開文件' self.fobj = open(self.filename, 'w') def close_file(self): '關閉文件' self.fobj.close() def IsNotWriteLog(self): '是否記錄日志' return self.bFlag def IsExitCommand(self, event): ''' 是否當前按下了程序定義的熱鍵' 如果按下了ALT+F2,將記錄日志的狀態位置為True,不記錄日志, 如果按下了ALT+F1,將記錄日志狀態位置為False,表示記錄日志 ''' if event.Alt == 32 and str(event.Key) == 'F2': self.bFlag = True print time.strftime('[%Y-%m-%d %H:%M:%S]: ',time.localtime(time.time()))+ ' stop write log' elif event.Alt == 32 and str(event.Key) == 'F1': self.bFlag = False print time.strftime('[%Y-%m-%d %H:%M:%S]: ',time.localtime(time.time()))+ ' start write log' def onMouseEvent(self, event): "處理鼠標事件" #判斷是否要記錄日志 if self.IsNotWriteLog(): return True self.fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n') self.fobj.writelines("Current Time:%s\n" % time.strftime('[%Y-%m-%d %H:%M:%S]: ',time.localtime(time.time()))) self.fobj.writelines("MessageName:%s\n" % str(event.MessageName)) self.fobj.writelines("Message:%d\n" % event.Message) self.fobj.writelines("Time_sec:%d\n" % event.Time) self.fobj.writelines("Window:%s\n" % str(event.Window)) self.fobj.writelines("WindowName:%s\n" % str(event.WindowName)) self.fobj.writelines("Position:%s\n" % str(event.Position)) self.fobj.writelines('-' * 20 + 'MouseEvent End' + '-' * 20 + '\n') return True def onKeyboardEvent(self, event): #處理按下的熱鍵 self.IsExitCommand(event) #判斷是否要記錄日志 if self.IsNotWriteLog(): return True self.fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + '\n') self.fobj.writelines("Current Time:%s\n" % time.strftime('[%Y-%m-%d %H:%M:%S]: ',time.localtime(time.time()))) self.fobj.writelines("MessageName:%s\n" % str(event.MessageName)) self.fobj.writelines("Message:%d\n" % event.Message) self.fobj.writelines("Time:%d\n" % event.Time) self.fobj.writelines("Window:%s\n" % str(event.Window)) self.fobj.writelines("WindowName:%s\n" % str(event.WindowName)) self.fobj.writelines("Ascii_code: %d\n" % event.Ascii) self.fobj.writelines("Ascii_char:%s\n" % chr(event.Ascii)) self.fobj.writelines("Key:%s\n" % str(event.Key)) self.fobj.writelines('-' * 20 + 'Keyboard End' + '-' * 20 + '\n') return True #默認記錄 bFlag = False def InspectKeyAndMouseEvent(): "啟動監控" my_event = CInspectKeyAndMouseEvent("D:\\hook_log.txt") my_event.open_file() #創建hook句柄 hm = pyHook.HookManager() #監控鍵盤 hm.KeyDown = my_event.onKeyboardEvent hm.HookKeyboard() #監控鼠標 hm.MouseAll = my_event.onMouseEvent hm.HookMouse() #循環獲取消息 pythoncom.PumpMessages() my_event.close_file() def handle_start_InspecEvent(): "開始監控(按下Ctrl + F1)" print time.strftime('[%Y-%m-%d %H:%M:%S]: ',time.localtime(time.time()))+ ' start write log' InspectKeyAndMouseEvent() #def handle_stop_InspecEvent(): # "停止監控 (按下Ctrl + F2)" # InspectKeyAndMouseEvent(False) if __name__ == "__main__": ''' Function:通過快捷鍵控制程序運行 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-09 ''' byref = ctypes.byref user32 = ctypes.windll.user32 #定義快捷鍵 HOTKEYS = { 1 : (win32con.VK_F1, win32con.MOD_ALT) # 2 : (win32con.VK_F2, win32con.MOD_ALT) } #快捷鍵對應的驅動函數 HOTKEY_ACTIONS = { 1 : handle_start_InspecEvent, # 2 : handle_stop_InspecEvent } #注冊快捷鍵 for id, (vk, modifiers) in HOTKEYS.items (): if not user32.RegisterHotKey (None, id, modifiers, vk): print "Unable to register id", id #啟動監聽 try: msg = wintypes.MSG () while user32.GetMessageA (byref (msg), None, 0, 0) != 0: if msg.message == win32con.WM_HOTKEY: action_to_take = HOTKEY_ACTIONS.get (msg.wParam) if action_to_take: action_to_take () user32.TranslateMessage (byref (msg)) user32.DispatchMessageA (byref (msg)) finally: for id in HOTKEYS.keys (): user32.UnregisterHotKey (None, id) ~~~ ### 二、測試: 1、以下打印是按下熱鍵時控制臺輸出(支持當前程序不是非激活窗口下按下熱鍵) ![](https://box.kancloud.cn/2016-06-08_575793592ba94.gif) 2、日志內容: 可以看到,在23:16:58停止記錄日志后,至23:17:05重新開始記錄之前,所有的鍵盤和鼠標輸入均沒有記錄,達到預期效果。 ~~~ --------------------MouseEvent Begin-------------------- Current Time:[2012-03-09 23:16:57]:? MessageName:mouse move Message:512 Time_sec:12542031 Window:328916 WindowName:FolderView Position:(737, 438) --------------------MouseEvent End-------------------- --------------------Keyboard Begin-------------------- Current Time:[2012-03-09 23:16:58]:? MessageName:key sys down Message:260 Time:12542890 Window:1639322 WindowName:本地磁盤 (D:) Ascii_code: 0 Ascii_char:? Key:Lmenu --------------------Keyboard End-------------------- --------------------Keyboard Begin-------------------- Current Time:[2012-03-09 23:17:05]:? MessageName:key sys down Message:260 Time:12550015 Window:1639322 WindowName:本地磁盤 (D:) Ascii_code: 0 Ascii_char:? Key:F1 --------------------Keyboard End-------------------- --------------------MouseEvent Begin-------------------- Current Time:[2012-03-09 23:17:06]:? MessageName:mouse move Message:512 Time_sec:12551000 Window:328916 WindowName:FolderView Position:(720, 420) --------------------MouseEvent End-------------------- --------------------MouseEvent Begin-------------------- Current Time:[2012-03-09 23:17:06]:? MessageName:mouse move Message:512 Time_sec:12551015 Window:328916 WindowName:FolderView Position:(719, 420) --------------------MouseEvent End-------------------- ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看