Python控制鼠標鍵盤代碼實例
python控制鼠標鍵盤其實很容易,我們在寫程序的時候很多時候會用的到!
python控制鼠標鍵盤步驟及代碼
1、安裝類庫
pip install pyautogui
2、代碼:
import pyautogui,time,randompyautogui.PAUSE = 3pyautogui.FAILSAFE = Truewidth, height = pyautogui.size()#print(str(width))#print(str(height))def drawRct(): for i in range(10): pyautogui.moveTo(300, 300, duration=0.25) pyautogui.moveTo(400, 300, duration=0.25) pyautogui.moveTo(400, 400, duration=0.25) pyautogui.moveTo(300, 400, duration=0.25)def mouseScroll(): time.sleep(5) for i in range(10): pyautogui.scroll(-200) time.sleep(2)def getXy(): try: while True: x, y = pyautogui.position() print(x,y) except KeyboardInterrupt: print(’nExit.’)def playVideo(x,y): for i in range(4): pyautogui.click(x,y) x=x+240 playAndComment()def playAndComment(): time.sleep(random.randint(5,15)) pyautogui.scroll(-400) pyautogui.scroll(-400) pyautogui.click(200,300) pyautogui.typewrite(’very good!’) pyautogui.click(900,400) pyautogui.click(710,16)if __name__ == ’__main__’: time.sleep(5) pyautogui.scroll(-300) for i in range(9): print(str(i)) time.sleep(random.randint(3,5)) #pyautogui.click(360,16) pyautogui.scroll(-300) time.sleep(random.randint(1,5)) playVideo(404,330)
直接監聽用戶鍵盤操作
from pynput.keyboard import Listenerimport logging location = './' logging.basicConfig( filename=(location+'keylogger.txt'), format='%(message)s', level=logging.DEBUG) def press(key): logging.info(key) with Listener(on_press = press) as listener: print(’The program begins to watch the action of keyboard:’) listener.join()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. Android打包篇:Android Studio將代碼打包成jar包教程2. Python使用urlretrieve實現直接遠程下載圖片的示例代碼3. SpringBoot+TestNG單元測試的實現4. Springboot 全局日期格式化處理的實現5. vue實現web在線聊天功能6. 解決Android Studio 格式化 Format代碼快捷鍵問題7. 完美解決vue 中多個echarts圖表自適應的問題8. JavaScript實現頁面動態驗證碼的實現示例9. Java使用Tesseract-Ocr識別數字10. JavaEE SpringMyBatis是什么? 它和Hibernate的區別及如何配置MyBatis
