You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows XP下基于Pynput实现鼠标事件映射的Python脚本开发需求

实现Windows XP下鼠标按键监听与映射的Python脚本

Hey there! Since you're new to Python and need this mouse handling script to work specifically on Windows XP, let's break this down into manageable steps that play nice with XP's limitations.

1. 先搞定兼容Windows XP的Pynput版本

Windows XP is long out of support, so newer versions of Pynput won't run on it. You'll need to install pynput 1.6.8—this is the last release that officially supports XP.

Open up Command Prompt (CMD) and run this command to install it:

pip install pynput==1.6.8

Note: If your XP system doesn't have pip installed, first grab Python 3.4.x or 2.7.x (these are the last Python versions that support XP) and set up pip from there.

2. 完整脚本代码

Here's the full script with comments explaining what each part does:

from pynput.mouse import Listener, Button
from pynput.keyboard import Controller as KeyboardController

# Set up the keyboard controller to send the ESC key
keyboard = KeyboardController()

def on_click(x, y, button, pressed):
    # Only react when the button is pressed (not released, to avoid duplicates)
    if pressed:
        if button == Button.left or button == Button.right:
            # Block left/right clicks: returning False stops the event reaching the system
            return False
        elif button == Button.middle:
            # Trigger ESC when middle mouse button is pressed
            keyboard.press('\x1b')  # '\x1b' is the ASCII code for ESC
            keyboard.release('\x1b')

# Start the mouse listener (runs until you stop the script)
with Listener(on_click=on_click) as listener:
    listener.join()

3. Key Details to Know

  • Blocking left/right clicks: When we return False for left/right button presses, Pynput intercepts the event and doesn't pass it to Windows—exactly what you need for "no operation" (blocking) those clicks.
  • Middle button → ESC: We use the KeyboardController to simulate pressing and releasing the ESC key. Using the ASCII code \x1b is more reliable on older Pynput versions than referencing Key.esc directly.
  • XP Compatibility Check: Double-check you're using Python 3.4/2.7 and pynput 1.6.8—newer versions will throw errors on XP.

4. How to Run the Script

  1. Save the code as mouse_esc.py on your XP machine.
  2. Open CMD, navigate to the folder where you saved the file, and run:
python mouse_esc.py
  1. To stop the script, press Ctrl + C in the CMD window.

内容的提问来源于stack exchange,提问作者Nico

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:50:34