温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python模拟点击中怎么实现区域的不同按键

发布时间:2020-11-30 10:13:12 来源:亿速云 阅读:228 作者:小新 栏目:编程语言

小编给大家分享一下python模拟点击中怎么实现区域的不同按键,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

假如想要实现鼠标左键双击时根据所在的不同区域实现不同的自动按键。

思路:监控鼠标事件,判断按键类型,如果是判断双击保留上一次的点击时间,自动按键最好新建线程,不然会卡在主线程。

#coding=utf-8
 
from pymouse import PyMouse, PyMouseEvent
from pykeyboard import PyKeyboard, PyKeyboardEvent
import time, threading
import math
 
## 初始化参数区(全局变量)
stop = False
interval = 1
is_running = False
times = 10
keys_mapping = {
    0 : ['1', '2', '3', '4'],
    10 : ['a', 'b'],
    1 : ['c', 'd'],
    11 : ['e', 'f'],
}   # 左上:0 右上:10 左下:1 右下:11
mouse = PyMouse()
keyboard = PyKeyboard()
x_dim, y_dim = mouse.screen_size()
 
## 循环按键
def loop(key):
    global stop
    global is_running
    global keyboard
    is_running = True
    for i in range(times):
        for k in keys_mapping[key]:
            if stop:
                print('stop')
                is_running = False
                return
            print(key, k)
            #keyboard.tap_key(k)
            time.sleep(interval)
    is_running = False
 
## 监控鼠标
class Clickonacci(PyMouseEvent):
    last_ts = None
    last_x = None
    last_y = None
    last_button = None
 
    def __init__(self):
        PyMouseEvent.__init__(self)
 
    ## hori:1-上,-1-下
    def scroll(self, x, y, hori, press):
        print(x, y, hori)
 
    def click(self, x, y, button, press):
        if press:
            return
        global stop
        global x_dim
        global y_dim
        ts = time.time()
        # button:1-左键,2-右键,3-中键
        # press: True-按下,False-释放
        if button == 1:
            ## 判断双击
            if self.last_ts and ts-self.last_ts<0.3 and self.last_button==button:
                print('double click')
                if is_running == True:
                    stop = True
                    time.sleep(interval)
                stop = False
                ## 计算类型
                key = 10*math.floor(2.0*x/x_dim) + math.floor(2.0*y/y_dim)
                t = threading.Thread(target=loop, name='LoopThread', args=(key,))
                t.start()
            self.last_ts = ts
            self.last_x = x
            self.last_y = y
            self.last_button = button
        else:
            stop = True
 
if __name__ == '__main__':
    #main()
    C = Clickonacci()
    C.run()

以上是“python模拟点击中怎么实现区域的不同按键”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI