最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用pynput實(shí)現(xiàn)劃詞復(fù)制功能

 更新時(shí)間:2022年05月05日 16:27:48   作者:Toblerone_Wind  
這篇文章主要為大家想詳細(xì)介紹了Python如何利用pynput實(shí)現(xiàn)劃詞復(fù)制功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

前言

本文參考了以下代碼

Windows系統(tǒng)環(huán)境下Python腳本實(shí)現(xiàn)全局“劃詞復(fù)制”功能

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庫(kù)的官方文檔

實(shí)現(xiàn)代碼

參考的博客實(shí)現(xiàn)了劃詞復(fù)制,但是看了下他的代碼寫(xiě)的有點(diǎn)復(fù)制混亂,監(jiān)聽(tīng)準(zhǔn)確率并不高且不太容易理解。

實(shí)際監(jiān)聽(tīng)鼠標(biāo)的劃詞操作邏輯很簡(jiǎn)單:

記錄下鼠標(biāo)左鍵按下時(shí)的位置,當(dāng)鼠標(biāo)左鍵松開(kāi)時(shí),記錄下鼠標(biāo)左鍵松開(kāi)的位置,如果按下的位置和松開(kāi)的位置不一致,說(shuō)明鼠標(biāo)正在劃詞。

from pynput.mouse import Listener, Button
from pynput.keyboard import Key, Controller
 
class AutoCopier():
    __press_xy = (0, 0)  # 私有變量 鼠標(biāo)左鍵按下時(shí)的位置
 
    def __init__(self):
        self.keyboard = Controller()                     # 初始化鍵盤(pán)控制器
        self.listener = Listener(on_click=self.on_click) # 初始化鼠標(biāo)監(jiān)聽(tīng)器
        self.listener.start()                            # 開(kāi)啟鼠標(biāo)監(jiān)聽(tīng)器線程
 
    # 點(diǎn)擊函數(shù)
    def on_click(self, x, y, button, pressed):           
        if button == Button.left:             # 左鍵點(diǎn)擊
            if pressed:                       # 左鍵按下
                self.__press_xy = (x, y)      # 記錄當(dāng)前鼠標(biāo)位置
            else:                             # 左鍵松開(kāi)           
                if self.__press_xy != (x, y): # 按下位置和松開(kāi)位置不一致
                    self.copy()               # 說(shuō)明操作是劃詞,執(zhí)行復(fù)制函數(shù)
    
    # 復(fù)制函數(shù)
    def copy(self): 
        with self.keyboard.pressed(Key.ctrl): # 按下ctrl,with語(yǔ)句結(jié)束后自動(dòng)松開(kāi)
            self.keyboard.press('c')          # 按下c
            self.keyboard.release('c')        # 松開(kāi)c
 
    # 等待線程終止
    def wait_to_stop(self):
        self.listener.join()
 
# for test
if __name__ == '__main__':
    at = AutoCopier()
    at.wait_to_stop()

知識(shí)點(diǎn)補(bǔ)充

1.pynput是什么

官方說(shuō)法:“他可以控制和監(jiān)聽(tīng)我們的輸入設(shè)備,目前支持鼠標(biāo)和鍵盤(pán)的控制與監(jiān)聽(tīng);因?yàn)槲抑皇褂昧嗽O(shè)備的控制 至于監(jiān)聽(tīng)并沒(méi)作深入了解 文章也不設(shè)計(jì)”

2.使用步驟

安裝pynput模塊

pip install pynput
#使用ctrl+v 快捷粘貼時(shí)候用到
pip install pyperclip

鍵盤(pán)控制

如下:

from pynput.keyboard import Key, Controller as c_keyboard
from pynput.mouse import Button, Controller as c_mouse

keyboard = c_keyboard()
#字符與數(shù)字
keyboard.press('a')
keyboard.release('a')

keyboard.press('A')
keyboard.release('A')

keyboard.press('1')
keyboard.release('1')
#非數(shù)字與字母鍵 詳情: 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')
##復(fù)制
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)
##回車(chē)
keyboard.press(Key.enter);
keyboard.release(Key.enter);

鼠標(biāo)控制

如下:

from pynput.keyboard import Key, Controller as c_keyboard
from pynput.mouse import Button, Controller as c_mouse

mouse= c_mouse()
#點(diǎn)擊
##雙擊
mouse.click(Button.left, 2)
##按下右鍵
mouse.press(Button.right)
##釋放右鍵
mouse.release(Button.right)
#鼠標(biāo)坐標(biāo)
print(mouse.position) 
##x軸坐標(biāo)
print(mouse.position[0]) 
##y軸坐標(biāo)
print(mouse.position[1])
#移動(dòng)
##移動(dòng)到絕對(duì)坐標(biāo)
mouse.position = (400, 38)
##相對(duì)當(dāng)前坐標(biāo)移動(dòng)
mouse.move(300, 2)
#滑動(dòng)
mouse.press(Button.left)
mouse.move(300, 2)
mouse.release(Button.left)

到此這篇關(guān)于Python利用pynput實(shí)現(xiàn)劃詞復(fù)制功能的文章就介紹到這了,更多相關(guān)Python pynput劃詞復(fù)制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PyTorch詳解經(jīng)典網(wǎng)絡(luò)種含并行連結(jié)的網(wǎng)絡(luò)GoogLeNet實(shí)現(xiàn)流程

    PyTorch詳解經(jīng)典網(wǎng)絡(luò)種含并行連結(jié)的網(wǎng)絡(luò)GoogLeNet實(shí)現(xiàn)流程

    今天小編就為大家分享一篇Pytorch實(shí)現(xiàn)GoogLeNet的方法,GoogLeNet提出了一個(gè)名為“Inception”的深度卷積神經(jīng)網(wǎng)結(jié)構(gòu),其目標(biāo)是將分類、識(shí)別ILSVRC14數(shù)據(jù)集的技術(shù)水平提高一個(gè)層次。這一結(jié)構(gòu)的主要特征是對(duì)網(wǎng)絡(luò)內(nèi)部計(jì)算資源的利用進(jìn)行了優(yōu)化
    2022-05-05
  • Python數(shù)據(jù)序列化技術(shù)總結(jié)

    Python數(shù)據(jù)序列化技術(shù)總結(jié)

    在現(xiàn)代軟件開(kāi)發(fā)中,數(shù)據(jù)序列化是一個(gè)關(guān)鍵環(huán)節(jié),它允許我們將復(fù)雜的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為可存儲(chǔ)或可傳輸?shù)母袷?,Python提供了多種數(shù)據(jù)序列化技術(shù),每種技術(shù)都有其獨(dú)特的性能優(yōu)勢(shì)和適用場(chǎng)景,本文將詳細(xì)介紹幾種強(qiáng)大的Python數(shù)據(jù)序列化技術(shù),需要的朋友可以參考下
    2025-03-03
  • scratch3.0二次開(kāi)發(fā)之用blocks生成python代碼

    scratch3.0二次開(kāi)發(fā)之用blocks生成python代碼

    python是blockl.generator的一個(gè)實(shí)例,會(huì)調(diào)用generator里的方法,這篇文章主要介紹了scratch3.0二次開(kāi)發(fā)之用blocks生成python代碼,需要的朋友可以參考下
    2021-08-08
  • Python文檔生成工具pydoc使用介紹

    Python文檔生成工具pydoc使用介紹

    這篇文章主要介紹了Python文檔生成工具pydoc使用介紹,本文講解了基本用法、獲取幫助的方法、生成的文檔效果圖等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • Python系統(tǒng)監(jiān)控模塊psutil功能與經(jīng)典用法分析

    Python系統(tǒng)監(jiān)控模塊psutil功能與經(jīng)典用法分析

    這篇文章主要介紹了Python系統(tǒng)監(jiān)控模塊psutil功能與經(jīng)典用法,簡(jiǎn)單講述了psutil模塊的功能、原理并結(jié)合具體實(shí)例形式分析了Python使用psutil模塊針對(duì)CPU、內(nèi)存、磁盤(pán)、網(wǎng)絡(luò)等信息的讀取相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • python制作微博圖片爬取工具

    python制作微博圖片爬取工具

    這篇文章主要介紹了python如何制作微博圖片爬取工具,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python中json庫(kù)的操作指南

    Python中json庫(kù)的操作指南

    JSON是存儲(chǔ)和交換文本信息的語(yǔ)法,類似XML,JSON比XML更小、更快,更易解析,且易于人閱讀和編寫(xiě),下面這篇文章主要給大家介紹了關(guān)于Python中json庫(kù)的操作指南,需要的朋友可以參考下
    2023-04-04
  • 簡(jiǎn)單了解python元組tuple相關(guān)原理

    簡(jiǎn)單了解python元組tuple相關(guān)原理

    這篇文章主要介紹了簡(jiǎn)單了解python元組tuple相關(guān)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • python 爬取免費(fèi)簡(jiǎn)歷模板網(wǎng)站的示例

    python 爬取免費(fèi)簡(jiǎn)歷模板網(wǎng)站的示例

    這篇文章主要介紹了python 爬取免費(fèi)簡(jiǎn)歷模板網(wǎng)站的示例,幫助大家更好的理解和使用python 爬蟲(chóng),感興趣的朋友可以了解下
    2020-09-09
  • 給Python學(xué)習(xí)者的文件讀寫(xiě)指南(含基礎(chǔ)與進(jìn)階)

    給Python學(xué)習(xí)者的文件讀寫(xiě)指南(含基礎(chǔ)與進(jìn)階)

    今天,貓貓跟大家一起,好好學(xué)習(xí)Python文件讀寫(xiě)的內(nèi)容,這部分內(nèi)容特別常用,掌握后對(duì)工作和實(shí)戰(zhàn)都大有益處,學(xué)習(xí)是循序漸進(jìn)的過(guò)程,欲速則不達(dá)
    2020-01-01

最新評(píng)論

商城县| 五家渠市| 凤凰县| 宝清县| 胶州市| 咸丰县| 寻乌县| 平湖市| 松原市| 图木舒克市| 从化市| 潮州市| 曲阜市| 通渭县| 彭州市| 和田县| 西峡县| 城固县| 香格里拉县| 托克托县| 洛南县| 曲麻莱县| 芦溪县| 油尖旺区| 湖北省| 象州县| 关岭| 前郭尔| 桓台县| 禹州市| 静海县| 达拉特旗| 临汾市| 高唐县| 青州市| 沾化县| 南平市| 密山市| 广宗县| 托克逊县| 武定县|