main.py 744 B

123456789101112131415161718192021222324252627282930
  1. # This is a sample Python script.
  2. from pynput import mouse,keyboard
  3. # Press Shift+F10 to execute it or replace it with your code.
  4. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
  5. import win32con
  6. def on_press(key):
  7. try:
  8. print(f'Key {key.char} pressed')
  9. except AttributeError:
  10. print(f'Special key {key} pressed')
  11. def on_release(key):
  12. if key == keyboard.Key.esc:
  13. # Stop listener
  14. return False
  15. def on_click(x, y, button, pressed):
  16. if pressed:
  17. print(f'Mouse clicked at ({x}, {y}) with {button}')
  18. # 鼠标监听
  19. mouse_listener = mouse.Listener(on_click=on_click)
  20. mouse_listener.start()
  21. # 键盘监听
  22. # 保持监听
  23. mouse_listener.join()