from pynput import mouse
from pynput.keyboard import Key, Controller
"""
PyCharm需要用管理员方式启动,否则游戏内输入无效!
"""
keyboard = Controller()
def on_click(x, y, button, pressed):
if button == mouse.Button.x2: # 当鼠标前进键按下时,按下w
keyboard.press('w')
if button == mouse.Button.x1: # 当鼠标后退键按下时, 释放w
keyboard.release('w')
# Collect events until released
with mouse.Listener(on_click=on_click) as listener:
listener.join()