Exemplo n.º 1
0
 def __init__(self, conn, addr):
     self.mouse = MouseController()
     self.keyboard = KeyoardController()
     threading.Thread.__init__(self)
     self.conn = conn
     self.addr = addr
     print("Accepting connection from: {0}:{1} ".format(*self.addr))
Exemplo n.º 2
0
def click_left_button():
    mouse_controller = MouseController()
    while True:
        if Shared.is_pressed_f3:
            mouse_controller.press(Button.left)
            mouse_controller.release(Button.left)
        time.sleep(0.1)
Exemplo n.º 3
0
 def __init__(self):
     global main_client
     print("server has started")
     user32 = ctypes.windll.user32
     user32.SetProcessDPIAware()
     self.WIDTH = user32.GetSystemMetrics(0)
     self.HEIGHT = user32.GetSystemMetrics(1)
     self.max_bytes = max_bytes
     self.mouse = MouseController()
     self.keyboard = KeyboardController()
     self.client_ip = "0.0.0.0"
     self.port = secondary_port
     self.control = True
     self.stop_running = False
     self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     self.client_socket.bind((self.client_ip, self.port))
     data, self.address = self.client_socket.recvfrom(self.max_bytes)
     if data.decode() == "close_connection":
         self.client_socket.close()
         print("server closed")
         return
     main_client.stop_waitingpage()
     main_client.close_main_page()
     main_client.add_conn(data.decode())
     self.client_socket.sendto(main_client.name.encode(), self.address)
     thread = threading.Thread(target=self.send_screen)
     control_thread = threading.Thread(target=self.main_control)
     stop_thread = threading.Thread(target=self.stop)
     thread.start()
     control_thread.start()
     stop_thread.start()
Exemplo n.º 4
0
def highlight_region(region: Region):
    (offset_x, offset_y, diamater) = region
    radius = diamater / 2
    terminated = False
    print(color_header("Highlighting Region"))
    print("Press ESC at any time to cancel.")

    def detect(key):
        nonlocal terminated
        if key == Key.esc:
            terminated = True
            return False

    with KeyboardListener(on_press=detect):
        mouse = MouseController()
        for deg in range(0, 360):
            if terminated:
                break
            rad = radians(deg)
            mouse.position = (offset_x + radius + sin(rad) * radius,
                              offset_y + radius + cos(rad) * radius)
            sleep(0.01)
    if terminated:
        print("Highlighting canceled.")
    else:
        print("Highlighting finished.")
Exemplo n.º 5
0
 def start(self):
     self._listener_keyboard = KeyboardListener(on_release=self.on_keyboard_release)
     self._listener_keyboard.start()
     if input("please input \"MOUSE\" to start mouse function:\n") == "MOUSE":
         self._listener_mouse = MouseListener(on_click=self.on_click)
         self._listener_mouse.start()
         self.ctl_mouse = MouseController()
Exemplo n.º 6
0
 def run(self):
     while self.execute_count > 0:
         with open(self.file_name, 'r', encoding='utf-8') as file:
             mouse_exec = MouseController()
             line = file.readline()
             while line:
                 obj = json.loads(line)
                 if obj['name'] == 'mouse':
                     if obj['event'] == 'move':
                         mouse_exec.position = (obj['location']['x'], obj['location']['y'])
                         time.sleep(0.01)
                     elif obj['event'] == 'click':
                         if obj['action']:
                             if obj['target'] == 'left':
                                 mouse_exec.press(Button.left)
                             else:
                                 mouse_exec.press(Button.right)
                         else:
                             if obj['target'] == 'left':
                                 mouse_exec.release(Button.left)
                             else:
                                 mouse_exec.release(Button.right)
                         time.sleep(0.01)
                     elif obj['event'] == 'scroll':
                         mouse_exec.scroll(obj['location']['x'], obj['location']['y'])
                         time.sleep(0.01)
                 line = file.readline()
Exemplo n.º 7
0
 def close_add_image_menu():
     """
     Close menu for import images in new product menu
     """
     mouse = MouseController()
     mouse.press(Button.left)
     mouse.release(Button.left)
Exemplo n.º 8
0
 def __init__(self, do_reset=False):
     if do_reset:
         self.set_mouse_positions()
     else:
         self.position_dict = self.get_mouse_positions()
     self.mouse = MouseController()
     self.keyboard = KeyController()
     self.threads = []
Exemplo n.º 9
0
 def __init__(self,PixelConversion=1):
     """Init a MovementController instance.
     
     Keyword Arguments:
     PixelConversion -- 1 for PC, 2 for Retina Display
     """
     self.pixelConversion = PixelConversion
     self.keyboard = KeyboardController()
     self.mouse = MouseController()
Exemplo n.º 10
0
def getWindowFocus():
    print("Getting the focus of the full screen window on the first display")
    mouse = MouseController()

    mouse.position = (400, 200)
    mouse.press(Button.left)
    time.sleep(0.2)
    mouse.release(Button.left)
    time.sleep(0.5)
Exemplo n.º 11
0
    def __init__(self):
        self.replay_file = self.LOG_FILE_NAME
        self.keyboard = KeyboardController()
        self.mouse = MouseController()

        self.INPUT_LIST = [
            self.KEY_PRESS,
            self.KEY_RELEASE,
            self.MOUSE_CLICK,
            self.MOUSE_SCROLL,
        ]
Exemplo n.º 12
0
class FastMouse:
    random_delay = 100
    controller = MouseController()

    def get_pos(self):
        return self.controller.position

    def click(self, location):
        try:
            self.controller.click(location, 0)
        except:
            print("Error clicking location")
        time.sleep(0.1)

    def click_cur_pos(self):
        self.controller.click(Button.left, 1)
        time.sleep(0.1)

    def click(self, location, wait):
        self.move_mouse(location)
        time.sleep(0.05 + wait)
        self.controller.click(Button.left, 1)
        time.sleep(0.05)
        time.sleep(random.randint(0, self.random_delay) * 0.0005)

    def move_mouse(self, location):
        randint = random.randint(0, 3)
        randomized_location = (location[0] + float(randint),
                               location[1] + float(randint))
        curve = self.get_curve_list(self.controller.position,
                                    randomized_location)
        speed_counter = 0
        while self.controller.position != randomized_location:
            self.controller.position = curve.pop(0)
            if (len(curve) > 100):
                curve.pop(100)
            else:
                curve.pop(len(curve) - 1)
            randint = random.randint(0, 30)
            if randint == 1:
                time.sleep(0.0000000001 + speed_counter)
            # speed_counter += 0.000000001

    def mid_point(self, point_a, point_b):
        return int(point_a + ((point_b - point_a) / 2))

    def get_curve_list(self, start, end):
        curvature = 1
        mid_point = (self.mid_point(start[0], end[0]) +
                     random.randint(7, 15) * curvature,
                     self.mid_point(start[1], end[1]) +
                     random.randint(7, 15) * curvature)
        return get_curve_points([start, start, mid_point, end, end, end, end])
Exemplo n.º 13
0
def keystroke_action(inject):
  keyboard = KeyboardController()
  mouse = MouseController()

  for command in inject:
    command = command.split(' ')
    c_type, c_data = command[0], command[1]

    if c_type == 'press': 
      try:
        c_data = eval(f'Key.{c_data}')
      except:
        pass
      finally:
        keyboard.press(c_data)
        keyboard.release(c_data)
    elif c_type == 'hold':
      try:
        c_data = eval(f'Key.{c_data}')
      except:
        pass
      finally:
        keyboard.press(c_data)
    elif c_type == 'release':
      try:
        c_data = eval(f'Key.{c_data}')
      except:
        pass
      finally:
        keyboard.release(c_data)
    elif c_type == 'type':
      keyboard.type(' '.join(command[1:]))
    elif c_type == 'position':
      mouse.position = [int(position) for position in c_data.split(',')]
    elif c_type == 'move':
      move_positions = [int(position) for position in c_data.split(',')]
      mouse.move(move_positions[0], move_positions[1])
    elif c_type == 'mhold':
      mouse.press(eval(f'Button.{c_data}'))
    elif c_type == 'mrelease':
      mouse.release(eval(f'Button.{c_data}'))
    elif c_type == 'click':
      mouse.press(eval(f'Button.{c_data}'))
      mouse.release(eval(f'Button.{c_data}'))
    elif c_type == 'dclick':
      mouse.click(eval(f'Button.{c_data}'), 2)
    elif c_type == 'scroll':
      scroll_positions = [int(position) for position in c_data.split(',')]
      mouse.scroll(scroll_positions[0], scroll_positions[1])
    elif c_type == 'sleep':
      time.sleep(float(c_data))
    else:
      raise Exception('Invalid keystroke command')
Exemplo n.º 14
0
def move_mouse():
    """
    :return: None, moves the mouse relative to current position
    """
    mouse = MouseController()

    # choose random x and y position to move mouse to
    x = randint(-30, 30)
    y = randint(-30, 30)

    mouse.move(x, y)
    logging.info(f'Moved mouse relative to current position {x, y}')
Exemplo n.º 15
0
def main():
    """
    Fishing with simple model (color channels are untouched).
    """
    model = tf.keras.models.load_model("train_model/Model/trained_model")
    classes = ["cast", "pull", "wait"]

    # mss is fast
    sct = mss.mss()
    # set correct monitor location!
    monitor = {"top": 550, "left": 430, "width": 200, "height": 450}

    mouse = MouseController()

    while True:
        t_1 = time.time()
        # using little sleep to keep reaction time close to human
        time.sleep(0.1)

        # python-mss.readthedocs.io/examples.html
        sct_img = sct.grab(monitor)
        pil_img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw",
                                  "BGRX")
        keras_frame = keras.preprocessing.image.img_to_array(pil_img)
        frame_batch = tf.expand_dims(keras_frame, 0)
        prediction = model.predict(frame_batch)
        pred_class = classes[np.argmax(prediction)]

        print()
        print(pred_class)

        frame = np.array(pil_img)
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = cv2.putText(frame, pred_class, (5, 25),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2,
                            cv2.LINE_AA)

        cv2.imshow('monitor', frame)
        cv2.waitKey(1)
        print("fps:", 1 // (time.time() - t_1))

        if pred_class == "cast":
            mouse.click(Button.right, 1)
            time.sleep(2.5)  # sleep while line is setting in game

        elif pred_class == "pull":
            mouse.click(Button.right, 1)
            time.sleep(2.5)  # sleep while line is setting in game

        else:
            # no need for action, just wait
            pass
Exemplo n.º 16
0
    def auto_roll(self, area):  # 自动滚动模式
        x, y, w, h = area
        self.rollermask.tips.setText("单击停止")
        QApplication.processEvents()
        speed = round(1 / self.roll_speed, 2)
        screen = QApplication.primaryScreen()
        winid = QApplication.desktop().winId()

        def onclick(x, y, button, pressed):  # 点击退出的函数句柄
            if pressed:
                print("click to stop")
                self.in_rolling = False
                listener.stop()

        controler = MouseController()  # 鼠标控制器
        listener = MouseListenner(on_click=onclick)  # 鼠标监听器
        self.match_thread = Commen_Thread(
            self.match_and_merge)  # 把match_and_merge放入一个线程中
        self.in_rolling = True
        i = 0
        controler.position = (area[0] + int(area[2] / 2),
                              area[1] + int(area[3] / 2))  # 控制鼠标点击到滚动区域中心
        oldimg = Image.new("RGB", (128, 128), "#FF0f00")
        listener.start()
        while self.in_rolling:
            st = time.time()
            pix = screen.grabWindow(winid, x, y, w, h)  # 截屏
            newimg = Image.fromqpixmap(pix)  # 转化为pil的格式
            img = cv2.cvtColor(asarray(newimg), cv2.COLOR_RGB2BGR)
            self.img_list.append(img)  # 图片数据存入self.img_list中被后台的拼接线程使用

            # cv2.imshow("FSd", img)
            # cv2.waitKey(0)
            if i >= 1:
                if i == 1:
                    self.match_thread.start()  # 当截第二张图片时拼接线程才启动
                if self.is_same(oldimg, newimg):  # 每帧检查是否停止
                    self.in_rolling = False
                    i += 1
                    break
            oldimg = newimg
            controler.scroll(dx=0, dy=-3)  # 控制鼠标滚动
            time.sleep(speed)  # 通过sleep控制自动滚动速度
            # cv2.imwrite('j_temp/{0}.png'.format(i), img)
            i += 1
        print("结束滚动,共截屏{}张".format(i))
        listener.stop()
        # self.showrect.hide()
        self.match_thread.wait()
Exemplo n.º 17
0
    def auto_roll(self, area):
        x, y, w, h = area
        self.tips.setText("单击停止")
        QApplication.processEvents()
        speed = round(1 / self.roll_speed, 2)
        screen = QApplication.primaryScreen()
        winid = QApplication.desktop().winId()

        def onclick(x, y, button, pressed):
            if pressed:
                print("click to stop")
                self.in_rolling = False
                listener.stop()

        controler = MouseController()
        listener = MouseListenner(on_click=onclick)
        self.match_thread = Commen_Thread(self.match_and_merge)
        self.in_rolling = True
        i = 0
        controler.position = (area[0] + int(area[2] / 2), area[1] + int(area[3] / 2))
        oldimg = Image.new("RGB", (128, 128), "#FF0f00")
        listener.start()
        while self.in_rolling:
            st = time.time()
            pix = screen.grabWindow(winid, x, y, w, h)
            newimg = Image.fromqpixmap(pix)
            img = cv2.cvtColor(np.asarray(newimg), cv2.COLOR_RGB2BGR)
            self.img_list.append(img)

            # cv2.imshow("FSd", img)
            # cv2.waitKey(0)
            if i >= 1:
                if i == 1:
                    self.match_thread.start()
                if self.is_same(oldimg, newimg):  # 每帧检查是否停止
                    self.in_rolling = False
                    i += 1
                    break
            oldimg = newimg
            controler.scroll(dx=0, dy=-3)
            time.sleep(speed)
            # cv2.imwrite('j_temp/{0}.png'.format(i), img)
            i += 1
        print("结束滚动,共截屏{}张".format(i))
        listener.stop()
        self.showrect.hide()
        self.match_thread.wait()
        self.showm_signal.emit("长截图完成")
Exemplo n.º 18
0
def gaming(ip):
    port = 5000
    game_machine = socket.socket()
    game_machine.connect((ip, port))
    data = game_machine.recv(4096)
    print("Message received: ", data.decode())

    mouse = MouseController()
    mouse_listener = MouseListener(on_click=on_click)
    mouse_listener.start()

    keyboard = KeyController()
    keyboard_listener = KeyListener(on_press=on_press, on_release=on_release)
    keyboard_listener.start()

    width, height = send_resolution(game_machine)

    # Streaming video
    while True:
        # Retrieves frame
        frame = stream(game_machine)

        # Displays frame
        cv2.namedWindow("Streaming", cv2.WND_PROP_FULLSCREEN)
        cv2.setWindowProperty("Streaming", cv2.WND_PROP_FULLSCREEN,
                              cv2.WINDOW_FULLSCREEN)
        cv2.imshow("Streaming", frame)
        if cv2.waitKey(1) == 27:
            break
        game_machine.send("Received".encode())
        print("Received")

        # Send mouse position
        send_mouse_pos(game_machine, mouse, width, height)

        # Send mouse clicks
        send_mouse_clicks(game_machine, CLICKS)
        CLICKS.clear()

        # Send keyboard input
        send_keyboard_input(game_machine, KEYPRESS)
        KEYPRESS.clear()
        CURRENT_KEY.clear()

    keyboard_listener.stop()
    mouse_listener.stop()
    cv2.destroyAllWindows()
    game_machine.close()
Exemplo n.º 19
0
 def __init__(self, parent_ui=None, config=Config()):
     super(AutoClickerThread, self).__init__()
     self.ui = parent_ui
     self.config = config
     self.thread = None
     self._stop_event = Event()
     self.mouse_button = None
     self.current_keys = set()
     self.accepted_keys = set()
     self.reload_config()
     self.activated = False
     self.mouse_controller = MouseController()
     self.keyboard_controller = KeyboardController()
     self.last_state = False
     self.sequence_index = 0
     self.sequence_length = len(self.config.output_sequence)
Exemplo n.º 20
0
    def run(self):
        if not os.path.exists(self.file_name):
            os.mkdir(self.file_name)
        with open(self.file_name, 'r', encoding='utf-8') as file:
            mouse_exec = MouseController()
            line = file.readline()
            # if not line:
            #     print('no mouse')
            #     return
            while line:
                obj = json.loads(line)
                # print(obj)
                if obj['name'] == 'mouse':
                    delay = obj['time'] / self.speed
                    if delay < 0.1:
                        high_precision_delay(delay)
                    elif 1 > delay > 0.1:
                        time.sleep(delay)
                    else:
                        self.listening_sleep(delay)

                    if obj['event'] == 'move':
                        mouse_exec.position = (obj['location']['x'],
                                               obj['location']['y'])
                        # print(1)
                    elif obj['event'] == 'click':
                        if obj['action']:
                            # time.sleep(obj['time'])
                            if obj['target'] == 'left':
                                mouse_exec.press(Button.left)
                            else:
                                mouse_exec.press(Button.right)
                        else:
                            if obj['target'] == 'left':
                                mouse_exec.release(Button.left)
                            else:
                                mouse_exec.release(Button.right)
                    elif obj['event'] == 'scroll':
                        # time.sleep(obj['time'])
                        mouse_exec.scroll(20, obj['location']['y'])
                    if self.stoper:
                        print('鼠标stoper')
                        self.stoper = False
                        break
                line = file.readline()
Exemplo n.º 21
0
def playScript(waitingTime, type, action, details):
    eventCount = 0
    mouse = MouseController()
    keyboard = KeyController()
    for t in waitingTime:
        time.sleep(int(t) / 1000)
        if type[eventCount] == 'Mouse':
            if action[eventCount] == 'Scrolled':
                # Move mouse to the right position
                arguments = details[eventCount].split(" ", 1)
                coordinates = arguments[0].split(",", 1)
                xpos = coordinates[0][1:]
                ypos = coordinates[1][:-1]
                mouse.position = (coordinates[0][1:], coordinates[1][:-1])

                # Get the right scroll arguments and perform scroll
                scrollArgs = arguments[1].split(",", 1)
                stArg = scrollArgs[0][1:]
                ndArg = scrollArgs[1][:-1]
                mouse.scroll(int(stArg), int(ndArg))
            else:
                # Move mouse to the right position
                coordinates = details[eventCount].split(",", 1)
                xpos = coordinates[0][1:]
                ypos = coordinates[1][:-1]
                mouse.position = (xpos, ypos)
                # Press and release
                mouse.press(stringToButton(str(action)))
                mouse.release(stringToButton(str(action)))
        else:
            # Remove quotation marks
            event = action[eventCount].replace("'", "")
            if details[eventCount] == 'Press':
                if len(event) == 1:
                    keyboard.press(event)
                else:
                    keyboard.press(stringToKey(event))
            else:
                if len(event) == 1:
                    keyboard.release(event)
                else:
                    keyboard.release(stringToKey(event))

        eventCount += 1
Exemplo n.º 22
0
    def auto_roll(self, area):
        """自动滚动截屏,总函数"""
        x, y, w, h = area
        self.img_width = w
        self.img_height = h
        speed = round(1 / self.roll_speed, 2)
        screen = QApplication.primaryScreen()
        controler = MouseController()
        find_pos = Commen_Thread(self.efind_the_pos)
        self.in_rolling = True
        i = 0
        controler.position = (area[0] + int(area[2] / 2),
                              area[1] + int(area[3] / 2))
        while self.in_rolling:
            pix = screen.grabWindow(QApplication.desktop().winId(), x, y, w,
                                    h)  # 区域截屏获取图像pixmap
            img = self.qpix_to_cv2(pix)  # 将qt的pixmap转为pil模块的img对象
            self.img_list.append(img)  # 储存图片的列表
            if i >= 1:
                if self.is_same(self.img_list[i - 1],
                                self.img_list[i]):  # 每帧检查是否停止(图片是否相同)
                    self.in_rolling = False
                    i += 1
                    break
                if i == 5:  # 图片大于5张才开始寻找拼接点
                    find_pos.start()
            controler.scroll(dx=0, dy=-3)  # 滚动屏幕
            time.sleep(0.1)  # 速度控制
            cv2.imwrite('j_temp/{0}.png'.format(i), img)
            i += 1
        print('图片数', i)
        self.max_arrange = i - 1  # 获取图片序列用于控制寻找边界点的结束
        if i <= 2:
            print('过短!一张图还不如直接截呐')
            self.clear_timer.start(0)
            return
        else:
            find_pos.wait()  # 等待拼接点寻找完成
        print('found_pos_done')
        self.merge_all()  # 调用图片拼接函数

        self.clear_timer.start(10000)  # 10s后初始化内存
Exemplo n.º 23
0
class KeyPresser(Thread):
    gcd = 1
    kc = KeyboardController()
    mc = MouseController()
    c = RGB(255, 0, 0)
    dc = GetDC(0)
    macro = ""

    def __init__(self, m):
        print(macros[m])
        self.macro = m
        Thread.__init__(self)
        self.daemon = True
        self.start()

    def indicator(self):
        width = int(GetSystemMetrics(0) / 2)
        for i in range(width - 12, width + 12):
            for j in range(24):
                SetPixel(self.dc, i, j, self.c)

    def run(self):
        while True:
            global run, pause, macros

            if running:
                self.indicator()

                if not pause:
                    for macro in macros[self.macro]:
                        if isinstance(macro, tuple):
                            self.mc.scroll(macro[0], macro[1])

                        else:
                            self.kc.press(macro)
                            self.kc.release(macro)

                        sleep(0.1)
                sleep(self.gcd)
            else:
                sleep(2)
Exemplo n.º 24
0
def streaming():
    port = 5000
    stream_machine = socket.socket()
    host_name = socket.gethostbyname(socket.gethostname())
    stream_machine.bind((host_name, port))
    stream_machine.listen()
    print(host_name)

    while True:
        # Initializations
        conn, addr = stream_machine.accept()
        conn.send("Testing connection . . .".encode())
        image = ImageGrab.grab()

        mouse = MouseController()
        keyboard = KeyController()

        width, height = calc_resolution(conn)
        # Streaming video
        while True:
            convert(image, conn)

            #Wait for receive message, then captures screen again
            conn.recv(1096).decode()
            image = ImageGrab.grab()
            print("New Image")

            # Move mouse
            move_mouse(conn, mouse, width, height)

            # Click mouse
            click_mouse(conn, mouse)

            # Keyboard
            press_keys(conn, keyboard)

        conn.close()
Exemplo n.º 25
0
import argparse
import time
from datetime import datetime
from pynput.mouse import Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController

mouse = MouseController()
keyboard = KeyboardController()

MOVE_MOUSE = False
PRESS_SHIFT_KEY = False
PIXELS_TO_MOVE = 1

move_mouse_every_seconds = 300


def define_custom_seconds():
    global move_mouse_every_seconds, PIXELS_TO_MOVE, PRESS_SHIFT_KEY, MOVE_MOUSE

    parser = argparse.ArgumentParser(
        description=
        "This program moves the mouse or press a key when it detects that you are away. "
        "It won't do anything if you are using your computer. "
        "Useful to trick your machine to think you are still working with it.")

    parser.add_argument(
        "-s",
        "--seconds",
        type=int,
        help=
        "Define in seconds how long to wait after a user is considered idle. Default 300."
Exemplo n.º 26
0
 def __init__(self):
     self.mouse = MouseController()
Exemplo n.º 27
0
import os
import subprocess

from motee.net import get_address, get_mac, hostname, PORT
from motee.scope import ScopeHandler
from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController
from Xlib import X
from Xlib.display import Display
from Xlib.ext.xtest import fake_input
import Xlib.XK

KEYBOARD = KeyboardController()
MOUSE = MouseController()

DISPLAY = Display()
Xlib.XK.load_keysym_group("xf86")


def _keycode(name):
    keysym_num = Xlib.XK.string_to_keysym(name)
    if keysym_num == Xlib.XK.NoSymbol:
        raise ValueError(name)
    keycode = DISPLAY.keysym_to_keycode(keysym_num)
    if not keycode:
        raise RuntimeError(f'Unable to map key "{name}" to a keycode')
    return keycode


VOLUME_UP = _keycode("XF86_AudioRaiseVolume")
VOLUME_DOWN = _keycode("XF86_AudioLowerVolume")
Exemplo n.º 28
0
from pynput.mouse import Button
from pynput.mouse import Controller as MouseController
from pynput import keyboard, mouse
from pynput.keyboard import Key
from pynput.keyboard import Controller as KeyBoardController
import threading

moc = MouseController()
keyc = KeyBoardController()


def moveClick(x, y, btn):
    moc.position = (x, y)
    moc.press(btn)
    moc.release(btn)


def doubleClick(x, y, btn, num=2):
    moc.position = (x, y)
    moc.click(btn, num)


def on_move(x, y):
    print('Pointer moved to {0}'.format((x, y)))


def mouseListener():
    def on_click(x, y, button, pressed):
        print('{0} at {1}'.format('Pressed' if pressed else 'Released',
                                  (x, y)))
        if not pressed:
Exemplo n.º 29
0
def read_tablet(args):
    """Loop forever and map evdev events to mouse"""

    last_x = last_y = None
    start_x = start_y = x = y = 0
    moves = 0

    monitor = get_monitors()[0]
    log.debug('Chose monitor: {}'.format(monitor))

    stdout = open_remote_device(args, file='/dev/input/event1')

    mouse = MouseController()
    keyboard = KeyboardController()
    # table of image coordinates and actions
    mapping = KeyboardMapping(mouse, keyboard)

    while True:
        _, _, e_type, e_code, e_value = struct.unpack('2IHHi', stdout.read(16))

        if e_type == e_type_abs:

            # handle x direction (rotated)
            if e_code == e_code_finger_ypos:
                log.debug(e_value)
                x = e_value
                monitor_x, monitor_y = map_finger(x, y, finger_width,
                                                  finger_height, monitor.width,
                                                  monitor.height)
                # use relative movement instead of absolute position
                # (like a trackpad)
                if last_x is not None:
                    mouse.move(monitor_x - last_x, 0)
                    moves += 1
                else:
                    start_x = x
                last_x = monitor_x

            # handle y direction (rotated)
            elif e_code == e_code_finger_xpos:
                log.debug('\t{}'.format(e_value))
                y = e_value
                monitor_x, monitor_y = map_finger(x, y, finger_width,
                                                  finger_height, monitor.width,
                                                  monitor.height)
                # use relative movement instead of absolute position
                # (like a trackpad)
                if last_y is not None:
                    mouse.move(0, monitor_y - last_y)
                    moves += 1
                else:
                    start_y = y
                last_y = monitor_y

            # handle pressure
            elif e_code == e_code_finger_pressure:
                log.debug('\t\t{}'.format(e_value))
                pass

            # handle finger lift
            elif e_code == e_code_finger_touch:
                if e_value == e_value_finger_up:
                    log.debug('\t\t\tfinger up')
                    last_x = None
                    last_y = None
                    log.debug('start position:{}  {}'.format(start_x, start_y))
                    # FIXME: I have no freaking idea why, but the keyboard lags slightly unless
                    # we print something to console here
                    print(' \r', end='')
                    # if the mouse hasn't moved much since the last finger lift, assume it was a touch
                    if moves < 8:
                        log.debug('touch, moves:{}'.format(moves))
                        image_x, image_y = map_finger(start_x, start_y,
                                                      finger_width,
                                                      finger_height,
                                                      image_width,
                                                      image_height)
                        mapping.call_action(image_x, image_y)
                    else:
                        log.debug('drag, moves:{}'.format(moves))

                    moves = 0
Exemplo n.º 30
0
    def __init__(self):
        self.keyboard = KeyboardController()
        self.cursor = MouseController()

        self.loadedTemplates = {}
        self.currentPressedButtons = []