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

成都创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为工布江达企业提供专业的网站制作、网站建设,工布江达网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
假如想要实现鼠标左键双击时根据所在的不同区域实现不同的自动按键。
思路:监控鼠标事件,判断按键类型,如果是判断双击保留上一次的点击时间,自动按键最好新建线程,不然会卡在主线程。
#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模拟点击中怎么实现区域的不同按键”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!