Python自動(dòng)化之pynput鍵盤鼠標(biāo)操作全攻略
鍵盤操作
需要執(zhí)行 press
和 release
來輸入按鍵。 當(dāng)然,也可以單獨(dú)執(zhí)行 press
from pynput.keyboard import Key, Controller from time import sleep # 初始化鍵盤控制器 keyboard = Controller() sleep(3) # 模擬按下和釋放 A 鍵 keyboard.press('a') keyboard.release('a') # 模擬按下和釋放 Enter 鍵 keyboard.press(Key.enter) keyboard.release(Key.enter)
鼠標(biāo)操作
from pynput.mouse import Button,Controller from time import sleep # 初始化鼠標(biāo)控制器 mouse = Controller() sleep(3) # 模擬鼠標(biāo)移動(dòng)到 (100, 100) 位置 mouse.position = (100, 100) mouse.click(Button.left,1) # 模擬鼠標(biāo)右鍵點(diǎn)擊 mouse.click(Button.right, 1)
注意:這里引入的是 pynput.mouse
的 Controller,與鍵盤處的名稱相同,但是作用不同,需要區(qū)分。推薦使用別名的方式將 Controller 命名為 MouseController。下面的方法也會(huì)用別名的方式進(jìn)行區(qū)分。
from pynput.mouse import Controller as MouseController
鍵盤監(jiān)聽
from pynput.keyboard import Listener as KeyboardListener def on_press(key): print(f'Alphanumeric key pressed: {key}') def on_release(key): print(f'Alphanumeric key released: {key}') if key == Key.esc: # 停止監(jiān)聽 return False # 設(shè)置鍵盤監(jiān)聽 with KeyboardListener(on_press=on_press, on_release=on_release) as listener: listener.join()
該方法輸出用戶按下的按鍵,當(dāng)用戶按下 Esc
時(shí)停止。
全局熱鍵監(jiān)聽
def keyboard_hotkey(): keyboard_controller = Controller() def on_activate_f(): print('全局熱鍵 F 被激活') keyboard_controller.type('你按了熱鍵F') def on_activate_shift_a(): print('全局熱鍵 Shift+A 被激活') keyboard_controller.type('你按了熱鍵Shift+A') # 創(chuàng)建熱鍵 with keyboard.GlobalHotKeys({ 'f': on_activate_f, '<shift>+a': on_activate_shift_a}) as h: h.join()
鼠標(biāo)監(jiān)聽
from pynput.mouse import Listener as MouseListener def mouse_listener(): # 添加左鍵點(diǎn)擊計(jì)數(shù)器 left_click_count = 0 def on_move(x, y): print(f'Mouse moved to ({x}, {y})') def on_click(x, y, button, pressed): nonlocal left_click_count print(f'Mouse clicked at ({x}, {y}) with {button}') # 檢查是否是左鍵點(diǎn)擊且按下狀態(tài) if button == Button.left and pressed: left_click_count += 1 print(f'左鍵點(diǎn)擊次數(shù): {left_click_count}') # 如果左鍵點(diǎn)擊次數(shù)超過三次,停止監(jiān)聽 if left_click_count >= 3: print('左鍵點(diǎn)擊已達(dá)到3次,停止監(jiān)聽') return False def on_scroll(x, y, dx, dy): print(f'Scrolled at ({x}, {y})') # 設(shè)置鼠標(biāo)監(jiān)聽 with MouseListener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener: listener.join() mouse_listener()
該示例記錄鼠標(biāo)信息,當(dāng)鼠標(biāo)左鍵點(diǎn)擊三次停止。
知識(shí)補(bǔ)充
如何通過pynput與日志記錄實(shí)現(xiàn)鍵盤、鼠標(biāo)的監(jiān)聽行為?
將相關(guān)的模塊導(dǎo)入到代碼塊中。
# 鼠標(biāo)鍵盤監(jiān)控 from pynput import keyboard, mouse # 日志處理 from loguru import logger # 多線程處理 from threading import Thread
將目標(biāo)日志記錄文件加入到logger代碼處理器中。
logger.add('lister.log')
編寫鼠標(biāo)被按下時(shí)的處理函數(shù)。
def on_keyboard_press(key): ''' 按鍵時(shí)記錄所按下的鍵 :param key: :return: ''' logger.debug(f'{key} :被按下了')
編寫鼠標(biāo)抬起時(shí)的處理函數(shù)。
def on_keyboard_release(key): ''' 釋放按鍵處理函數(shù) :param key: :return: ''' if key == keyboard.Key.esc: return False
編寫鼠標(biāo)單擊、右擊以及滑輪的處理函數(shù)。
def on_mouse_click(x, y, click, pressed): if click == mouse.Button.left: logger.debug('鼠標(biāo)左鍵按下了') elif click == mouse.Button.right: logger.debug('鼠標(biāo)右鍵按下了') return False else: logger.debug('中間滾輪按下了')
將鍵盤相關(guān)的處理函數(shù)加入到鍵盤事件的監(jiān)聽中。
def func_keyboard(): ''' 鍵盤的按下/釋放的監(jiān)聽 :return: ''' with keyboard.Listener(on_press=on_keyboard_press, on_release=on_keyboard_release) as keyboard_listener: keyboard_listener.join()
將鼠標(biāo)相關(guān)的處理函數(shù)加入到鼠標(biāo)事件的監(jiān)聽中。
def func_keyboard(): ''' 鍵盤的按下/釋放的監(jiān)聽 :return: ''' with keyboard.Listener(on_press=on_keyboard_press, on_release=on_keyboard_release) as keyboard_listener: keyboard_listener.join()
在main()的處理函數(shù)中將鼠標(biāo)監(jiān)聽、鍵盤監(jiān)聽分別作為兩個(gè)線程啟動(dòng)。
if __name__ == '__main__': ''' 執(zhí)行線程 ''' # 定義鍵盤監(jiān)聽線程 thread_keyboard = Thread(target=func_keyboard) # 定義鼠標(biāo)監(jiān)聽線程 thread_mouse = Thread(target=func_mouse_click) # 分別啟動(dòng)線程 thread_keyboard.start() thread_mouse.start()
最后可以得到一份鍵盤、鼠標(biāo)歷史執(zhí)行狀態(tài)記錄的日志文件,通過NLTK語言的處理從而逆向推出電腦的操作內(nèi)容,這一部分就不演示了。
到此這篇關(guān)于Python自動(dòng)化之pynput鍵盤鼠標(biāo)操作全攻略的文章就介紹到這了,更多相關(guān)Python pynput操作鍵盤鼠標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 實(shí)現(xiàn)微信自動(dòng)回復(fù)的方法
這篇文章主要介紹了Python 實(shí)現(xiàn)微信自動(dòng)回復(fù)的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09python判斷鏈表是否有環(huán)的實(shí)例代碼
在本篇文章里小編給大家整理的是關(guān)于python判斷鏈表是否有環(huán)的知識(shí)點(diǎn)及實(shí)例代碼,需要的朋友們參考下。2020-01-01Python 多線程共享變量的實(shí)現(xiàn)示例
這篇文章主要介紹了Python 多線程共享變量的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04python實(shí)現(xiàn)Excel文件轉(zhuǎn)換為TXT文件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Excel文件轉(zhuǎn)換為TXT文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04python數(shù)據(jù)分析之用sklearn預(yù)測(cè)糖尿病
這篇文章主要介紹了python數(shù)據(jù)分析之用sklearn預(yù)測(cè)糖尿病,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python數(shù)據(jù)分析的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04Python數(shù)據(jù)解析bs4庫(kù)使用BeautifulSoup方法示例
這篇文章主要為大家介紹了Python數(shù)據(jù)解析bs4庫(kù)使用BeautifulSoup方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08