hel преди 1 година
родител
ревизия
d73c9c274e
променени са 4 файла, в които са добавени 90 реда и са изтрити 48 реда
  1. 12 12
      listenTest.py
  2. 30 6
      monitorTest.py
  3. 0 30
      queueFunc.py
  4. 48 0
      screen.py

+ 12 - 12
listenTest.py

@@ -1,12 +1,11 @@
-import time
-
-from pynput import mouse
-from pynput.mouse import Controller, Button
 import asyncio
 
-import win32Test
 import pika
+from pynput import mouse
+from pynput.mouse import Controller
+
 import messageClass
+import screen
 
 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
 channel = connection.channel()
@@ -15,12 +14,12 @@ channel.exchange_declare("sync", "fanout", durable=True)
 
 mouse_controller = Controller()
 
-middle_point, base_width, base_height = win32Test.arrange_windows()
+screen.arrange_master()
 
 
 def in_monitorZone():
     now_position = mouse_controller.position
-    if now_position < (base_width, base_height):
+    if now_position < (500, 600):
         return True
     else:
         return False
@@ -38,9 +37,9 @@ def on_click(x, y, button, pressed):
     # y,
     # dx,
     # dy,
-
-    message = messageClass.ClickMessage(x, y, button).getMessage()
-    channel.basic_publish(exchange='sync', routing_key='', body=message)
+    if in_monitorZone():
+        message = messageClass.ClickMessage(x, y, button).getMessage()
+        channel.basic_publish(exchange='sync', routing_key='', body=message)
     # if not pressed:
     #     # Stop listener
     #     return False
@@ -55,8 +54,9 @@ async def scroll_to_next_point(middle_point, mouse_controller, dx, dy):
 
 
 def on_scroll(x, y, dx, dy):
-    message = messageClass.ScrollMessage(x, y, dx, dy).getMessage()
-    channel.basic_publish(exchange='sync', routing_key='', body=message)
+    if in_monitorZone():
+        message = messageClass.ScrollMessage(x, y, dx, dy).getMessage()
+        channel.basic_publish(exchange='sync', routing_key='', body=message)
 
     # print('Scrolled {0} at {1}'.format(
     #     'down' if dy < 0 else 'up',

+ 30 - 6
monitorTest.py

@@ -1,17 +1,19 @@
 #!/usr/bin/env python
+import json
 import os
 import signal
 import sys
 import time
 
 import pika
+from pynput.mouse import Controller, Button
 
-import queueFunc
-import win32Test
+import screen
 
-middle_point, base_width, base_height = win32Test.arrange_windows()
+screenNumber = screen.arrange_slave()
 connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.0.13', port=5672))
 channel = connection.channel()
+mouseController = Controller()
 
 
 def callback(ch, method, properties, body):
@@ -20,7 +22,7 @@ def callback(ch, method, properties, body):
     print(time.time())
 
     # 执行其他操作,例如记录日志、更新数据库等
-    queueFunc.handleMessage(properties, body)
+    handleMessage(properties, body)
     # 确认收到消息
     # ch.basic_ack(delivery_tag=method.delivery_tag)
 
@@ -28,7 +30,7 @@ def callback(ch, method, properties, body):
 def main():
     # 定义一个交换机(exchange)
     exchange_name = 'sync'
-    channel.exchange_declare(exchange=exchange_name, exchange_type='fanout',durable=True)
+    channel.exchange_declare(exchange=exchange_name, exchange_type='fanout', durable=True)
 
     # 定义一个队列
     queue_name = 'hello'
@@ -53,9 +55,31 @@ def signal_handler(sig, frame):
     sys.exit(0)
 
 
+def handleMessage(properties, body):
+    jsonObject = json.loads(body)
+    if jsonObject['function'] == 0:
+        on_click(jsonObject['x'], jsonObject['y'], jsonObject['button'])
+    else:
+        on_scroll(jsonObject['x'], jsonObject['y'], jsonObject['dx'], jsonObject['dy'])
+
+
+def on_click(x, y, button):
+    for i in range(0, screenNumber):
+        mouseController.position = (x + i * 800, y)
+        if 'left' in button:
+            mouseController.click(Button.left, 1)
+        else:
+            mouseController.click(Button.right, 1)
+
+
+def on_scroll(x, y, dx, dy):
+    for i in range(0, screenNumber):
+        mouseController.position = (x +i * 800, y)
+        mouseController.scroll(dx, dy)
+
+
 signal.signal(signal.SIGINT, signal_handler)
 signal.signal(signal.SIGTERM, signal_handler)
-
 if __name__ == '__main__':
     try:
         main()

+ 0 - 30
queueFunc.py

@@ -1,30 +0,0 @@
-from pynput.mouse import Controller, Button
-import json
-import time
-
-mouseController = Controller()
-
-
-def handleMessage(properties, body):
-    jsonObject = json.loads(body)
-    if jsonObject['function'] == 0:
-        on_click(jsonObject['x'], jsonObject['y'], jsonObject['button'])
-    else:
-        on_scroll(jsonObject['x'], jsonObject['y'], jsonObject['dx'], jsonObject['dy'])
-
-
-def on_click(x, y, button):
-    mouseController.position = (x, y)
-    if 'left' in button:
-        print('w')
-        mouseController.click(Button.left, 1)
-        time.sleep(0.1)
-    else:
-        print('2')
-        mouseController.click(Button.right, 1)
-        time.sleep(0.1)
-
-
-def on_scroll(x, y, dx, dy):
-    mouseController.position = (x, y)
-    mouseController.scroll(dx, dy)

+ 48 - 0
screen.py

@@ -0,0 +1,48 @@
+import math
+
+import win32con
+import win32gui
+
+
+def get_all_windows():
+    windows = []
+    win32gui.EnumWindows(lambda hwnd, windows: windows.append(hwnd), windows)
+    return windows
+
+
+def get_window_title(hwnd):
+    return win32gui.GetWindowText(hwnd)
+
+
+def find_chrome():
+    windows = get_all_windows()
+    target = []
+    for hwnd in windows:
+        title = get_window_title(hwnd)
+        if "Google Chrome" in title:
+            target.append(hwnd)
+    return target
+
+
+def arrange_master():
+    chrome = find_chrome()
+    hwnd = chrome[0]
+    win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 0, 0, 500, 600, 0)
+    win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
+    win32gui.SetForegroundWindow(hwnd)
+
+
+def arrange_slave():
+    chrome = find_chrome()
+    for i, hwnd in enumerate(chrome):
+        if i > 5:
+            return
+        # width = math.ceil(i / 3)
+        # height = math.floor(i / 2)
+        win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, i * 500, 0, 500, 600, 0)
+        win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
+        win32gui.SetForegroundWindow(hwnd)
+    return len(chrome)
+
+
+arrange_master()