server.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2022/10/30 19:14:43
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc :
  8. '''
  9. from ctypes import windll, Structure, c_long, byref
  10. import socket
  11. import threading
  12. import time
  13. from keymap import KEYMAP
  14. class Mouse(Structure):
  15. '''鼠标光标的坐标'''
  16. _fields_ = [("x", c_long), ("y", c_long)]
  17. class Server(object):
  18. def __init__(self, port):
  19. server = socket.socket()
  20. server.bind(('0.0.0.0', port))
  21. server.listen(5)
  22. client = None
  23. # client, addr = server.accept()
  24. ENABLE = True
  25. PREV_X, PREV_Y = -1, -1
  26. PREV_MOUSE_LEFT = windll.user32.GetKeyState(0x01) & 0x8000
  27. PREV_MOUSE_RIGHT = windll.user32.GetKeyState(0x02) & 0x8000
  28. def start(self):
  29. '''start server'''
  30. global server
  31. global client
  32. global PREV_X
  33. global PREV_Y
  34. global PREV_MOUSE_LEFT
  35. global PREV_MOUSE_RIGHT
  36. global ENABLE
  37. while True:
  38. try:
  39. if Server.getKeyDown(0x78):
  40. ENABLE = not ENABLE
  41. time.sleep(1)
  42. if not ENABLE:
  43. continue
  44. mouse_left = windll.user32.GetKeyState(0x01)
  45. mouse_left = mouse_left & 0x8000
  46. mouse_right = windll.user32.GetKeyState(0x02)
  47. mouse_right = mouse_right & 0x8000
  48. x, y = Server.getMousePos()
  49. for keycode, val in KEYMAP.items():
  50. if Server.getKeyDown(keycode):
  51. print(val)
  52. if mouse_left != PREV_MOUSE_LEFT:
  53. PREV_MOUSE_LEFT = mouse_left
  54. if mouse_left != 0:
  55. # Left Pressed
  56. print('LP')
  57. else:
  58. # Left Released
  59. print('LR')
  60. if mouse_right != PREV_MOUSE_RIGHT:
  61. PREV_MOUSE_RIGHT = mouse_right
  62. if mouse_right != 0:
  63. # Right Pressed
  64. print('RP')
  65. else:
  66. # Right Pressed
  67. print('RR')
  68. if x != PREV_X or y != PREV_Y:
  69. PREV_X, PREV_Y = x, y
  70. print(x, y)
  71. pass
  72. except:
  73. pass
  74. time.sleep(0.025)
  75. def stop(self):
  76. '''stop server'''
  77. self.server.close()
  78. @staticmethod
  79. def getKeyDown(keycode):
  80. '''按键是否按下'''
  81. state = windll.user32.GetKeyState(keycode)
  82. if (state != 0) and (state != 1):
  83. return True
  84. return False
  85. @staticmethod
  86. def getMousePos():
  87. ''''''
  88. pt = Mouse()
  89. windll.user32.GetCursorPos(byref(pt))
  90. return (pt.x, pt.y)
  91. if __name__ == "__main__":
  92. th = threading.Thread(target=Server().start())
  93. th.start()
  94. th.join()