#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Contact : liuyuqi.gov@msn.cn @Time : 2022/10/30 19:14:43 @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved. @Desc : ''' from ctypes import windll, Structure, c_long, byref import socket import threading import time from keymap import KEYMAP class Mouse(Structure): '''鼠标光标的坐标''' _fields_ = [("x", c_long), ("y", c_long)] class Server(object): def __init__(self, port): server = socket.socket() server.bind(('0.0.0.0', port)) server.listen(5) client = None # client, addr = server.accept() ENABLE = True PREV_X, PREV_Y = -1, -1 PREV_MOUSE_LEFT = windll.user32.GetKeyState(0x01) & 0x8000 PREV_MOUSE_RIGHT = windll.user32.GetKeyState(0x02) & 0x8000 def start(self): '''start server''' global server global client global PREV_X global PREV_Y global PREV_MOUSE_LEFT global PREV_MOUSE_RIGHT global ENABLE while True: try: if Server.getKeyDown(0x78): ENABLE = not ENABLE time.sleep(1) if not ENABLE: continue mouse_left = windll.user32.GetKeyState(0x01) mouse_left = mouse_left & 0x8000 mouse_right = windll.user32.GetKeyState(0x02) mouse_right = mouse_right & 0x8000 x, y = Server.getMousePos() for keycode, val in KEYMAP.items(): if Server.getKeyDown(keycode): print(val) if mouse_left != PREV_MOUSE_LEFT: PREV_MOUSE_LEFT = mouse_left if mouse_left != 0: # Left Pressed print('LP') else: # Left Released print('LR') if mouse_right != PREV_MOUSE_RIGHT: PREV_MOUSE_RIGHT = mouse_right if mouse_right != 0: # Right Pressed print('RP') else: # Right Pressed print('RR') if x != PREV_X or y != PREV_Y: PREV_X, PREV_Y = x, y print(x, y) pass except: pass time.sleep(0.025) def stop(self): '''stop server''' self.server.close() @staticmethod def getKeyDown(keycode): '''按键是否按下''' state = windll.user32.GetKeyState(keycode) if (state != 0) and (state != 1): return True return False @staticmethod def getMousePos(): '''''' pt = Mouse() windll.user32.GetCursorPos(byref(pt)) return (pt.x, pt.y) if __name__ == "__main__": th = threading.Thread(target=Server().start()) th.start() th.join()