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
Falsefor 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
KeyboardControllerto simulate pressing and releasing the ESC key. Using the ASCII code\x1bis more reliable on older Pynput versions than referencingKey.escdirectly. - 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
- Save the code as
mouse_esc.pyon your XP machine. - Open CMD, navigate to the folder where you saved the file, and run:
python mouse_esc.py
- To stop the script, press
Ctrl + Cin the CMD window.
内容的提问来源于stack exchange,提问作者Nico
相关产品推荐
相关产品推荐

