網站首頁 編程語言 正文
前言
本文參考了以下代碼
Windows系統環境下Python腳本實現全局“劃詞復制”功能
from pynput import mouse
import time
import threading
__DEBUG = False
def log(msg,debug=False):
if __DEBUG or debug:
print(msg)
class MouseMonitor():
__press_time = 0
__press_double_state = False
__move = (0,0)
def __init__(self,on_selected=None):
if on_selected:
self.on_selected = on_selected
else:
self.on_selected = self.on_selected
self.listener = mouse.Listener(on_move=self.on_move,on_click=self.on_click)
self.listener.start()
self.listener.join()
def on_selected(msg):
print('selected "%s" has been copied.' % (msg,))
def on_move(self,x,y):
if self.__press_time == 0:
self.__move = (x,y)
# log(self.__press_time,time.time())
# log('Pointer moved to {0}'.format((x,y)))
def on_click(self,x,y,button,pressed):
if str(button) == 'Button.left':
if pressed:
self.on_pressed(x,y)
else:
self.on_released(x,y)
def on_pressed(self,x,y):
if self.__press_double_state:
# double click
# self.__press_double_state = False
if not self.check_not_time_out(self.__press_time, time.time(),0.4): # miss double click
log('double1 click timeout and reset then')
self.reset()
self.__press_time = time.time()
else:
# single click
self.__press_time = time.time()
# self.__press_double_state = False
def on_released(self,x,y):
if self.__press_double_state:
# double click
if self.check_not_time_out(self.__press_time, time.time(),0.8):
log('double click: %s' % (self.get_copy()))
self.on_selected(self.get_copy())
self.__press_double_state = False
else:
log('double2 click timeout and reset then')
self.reset()
else:
if self.check_not_time_out(self.__press_time, time.time()):
log('double click maybe')
self.__press_double_state = True
threading.Timer(0.5, self.timeout_handler).start() # wait timeout to reset
elif not self.check_not_time_out(self.__press_time, time.time(),1):
if self.__move != (0,0):
self.on_selected(self.get_copy())
log('selected: %s' % (self.get_copy(),))
self.reset()
else:
log('reset state')
self.reset()
def get_copy(self):
import win32clipboard as wc
import win32con
def trigger_copy():
from pynput.keyboard import Key,Controller
key = Controller()
with key.pressed(Key.ctrl):
key.press('c')
key.release('c')
time.sleep(0.1) # wait for ctrl+c valid
trigger_copy()
msg = ''
try:
wc.OpenClipboard()
msg = wc.GetClipboardData(win32con.CF_UNICODETEXT)
wc.CloseClipboard()
except TypeError:
log('Clipboard Content is TypeError.')
return msg
def reset(self):
self.__press_time = 0
self.__press_double_state = False
self.__move = (0,0)
def timeout_handler(self):
self.reset()
log('timeout reset state')
def check_not_time_out(self,old,new,delta=0.2):
if(new - old > delta): # time delta > 0.2s
return False
else:
return True
def printf(msg):
log('copy content:'+msg,True)
# log('x = {0} , y = {1}'.format(x,y))
if __name__ == '__main__':
mm = MouseMonitor(printf)
pynput庫的官方文檔
實現代碼
參考的博客實現了劃詞復制,但是看了下他的代碼寫的有點復制混亂,監聽準確率并不高且不太容易理解。
實際監聽鼠標的劃詞操作邏輯很簡單:
記錄下鼠標左鍵按下時的位置,當鼠標左鍵松開時,記錄下鼠標左鍵松開的位置,如果按下的位置和松開的位置不一致,說明鼠標正在劃詞。
from pynput.mouse import Listener, Button
from pynput.keyboard import Key, Controller
class AutoCopier():
__press_xy = (0, 0) # 私有變量 鼠標左鍵按下時的位置
def __init__(self):
self.keyboard = Controller() # 初始化鍵盤控制器
self.listener = Listener(on_click=self.on_click) # 初始化鼠標監聽器
self.listener.start() # 開啟鼠標監聽器線程
# 點擊函數
def on_click(self, x, y, button, pressed):
if button == Button.left: # 左鍵點擊
if pressed: # 左鍵按下
self.__press_xy = (x, y) # 記錄當前鼠標位置
else: # 左鍵松開
if self.__press_xy != (x, y): # 按下位置和松開位置不一致
self.copy() # 說明操作是劃詞,執行復制函數
# 復制函數
def copy(self):
with self.keyboard.pressed(Key.ctrl): # 按下ctrl,with語句結束后自動松開
self.keyboard.press('c') # 按下c
self.keyboard.release('c') # 松開c
# 等待線程終止
def wait_to_stop(self):
self.listener.join()
# for test
if __name__ == '__main__':
at = AutoCopier()
at.wait_to_stop()
知識點補充
1.pynput是什么
官方說法:“他可以控制和監聽我們的輸入設備,目前支持鼠標和鍵盤的控制與監聽;因為我只使用了設備的控制 至于監聽并沒作深入了解 文章也不設計”
2.使用步驟
安裝pynput模塊
pip install pynput
#使用ctrl+v 快捷粘貼時候用到
pip install pyperclip
鍵盤控制
如下:
from pynput.keyboard import Key, Controller as c_keyboard
from pynput.mouse import Button, Controller as c_mouse
keyboard = c_keyboard()
#字符與數字
keyboard.press('a')
keyboard.release('a')
keyboard.press('A')
keyboard.release('A')
keyboard.press('1')
keyboard.release('1')
#非數字與字母鍵 詳情: https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key
keyboard.press(Key.enter);
keyboard.release(Key.enter);
#組合
##全選
keyboard.press(Key.ctrl)
keyboard.press('a')
time.sleep(2)
keyboard.release('a')
keyboard.release(Key.ctrl)
###或者
with keyboard .pressed(Key.ctrl):
keyboard.press('a')
keyboard.release('a')
##復制
seller_nick = 'www.baidu.com/a.php?a=a&b=2'
pyperclip.copy('https://'+seller_nick.replace('amp;',''))
##粘貼
keyboard.press(Key.ctrl)
keyboard.press('v')
time.sleep(1)
keyboard.release('v')
keyboard.release(Key.ctrl)
##回車
keyboard.press(Key.enter);
keyboard.release(Key.enter);
鼠標控制
如下:
from pynput.keyboard import Key, Controller as c_keyboard
from pynput.mouse import Button, Controller as c_mouse
mouse= c_mouse()
#點擊
##雙擊
mouse.click(Button.left, 2)
##按下右鍵
mouse.press(Button.right)
##釋放右鍵
mouse.release(Button.right)
#鼠標坐標
print(mouse.position)
##x軸坐標
print(mouse.position[0])
##y軸坐標
print(mouse.position[1])
#移動
##移動到絕對坐標
mouse.position = (400, 38)
##相對當前坐標移動
mouse.move(300, 2)
#滑動
mouse.press(Button.left)
mouse.move(300, 2)
mouse.release(Button.left)
原文鏈接:https://blog.csdn.net/qq_42276781/article/details/124528692
相關推薦
- 2021-12-10 linux系統文件共享samba配置教程_Linux
- 2022-10-10 React實時預覽react-live源碼解析_React
- 2022-10-02 Android?NTP?時間同步機制詳解_Android
- 2023-02-12 Pytorch建模過程中的DataLoader與Dataset示例詳解_python
- 2022-02-27 Error in render: “TypeError: Cannot read propertie
- 2022-07-20 react?diff?算法實現思路及原理解析_React
- 2022-04-05 C++繼承和動態內存分配_C 語言
- 2022-04-17 Android編程開發從零開始編寫一個輕量級瀏覽器_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支