Python使用Keyboard模块实现游戏特技脚本时出现键盘卡顿问题求助
Hey there! I see exactly what's causing that frustrating keyboard lag—let's break it down and fix it up.
The Root of the Lag
Your while True loop is running non-stop, constantly checking keyboard.is_pressed() in a tight loop. This eats up CPU resources like crazy, which is why all your keyboard input feels sluggish, not just the script's actions. On top of that, you're mixing two different input libraries (pynput and keyboard) which can create conflicts and extra overhead.
The Fix: Switch to Event-Based Listening
Instead of polling the keyboard non-stop, we'll use the keyboard library's event-driven system. This way, your code only runs when a specific key is pressed, leaving your CPU free for other tasks (like actually playing the game). We'll also clean up the library mix-up to keep things consistent.
Here's the revised code:
from pynput.mouse import Button, Controller as MouseController import keyboard import time print("Press 5 or 6 to execute speedflip, press Q to quit") mouse = MouseController() def speedflip_5(): print('Speedflip 5 executed') mouse.press(Button.left) # Press and hold W + A keyboard.press('w') keyboard.press('a') time.sleep(0.18) keyboard.release('w') keyboard.release('a') time.sleep(0.6) # Diagonal flip sequence keyboard.press('w') keyboard.press('e') mouse.click(Button.right, 1) time.sleep(0.1) mouse.click(Button.right, 1) time.sleep(0.1) keyboard.release('w') keyboard.release('e') # Flip cancel keyboard.press('s') keyboard.press('d') time.sleep(0.9) keyboard.release('s') keyboard.release('d') time.sleep(1) mouse.release(Button.left) def speedflip_6(): print('Speedflip 6 is executed') mouse.press(Button.left) keyboard.press('w') keyboard.press('a') time.sleep(0.05) keyboard.release('w') keyboard.release('a') time.sleep(0.6) keyboard.press('w') keyboard.press('e') mouse.click(Button.right, 1) time.sleep(0.1) mouse.click(Button.right, 1) time.sleep(0.1) keyboard.release('w') keyboard.release('e') keyboard.press('s') keyboard.press('d') time.sleep(0.9) keyboard.release('s') keyboard.release('d') time.sleep(1) mouse.release(Button.left) # Bind keys to our functions keyboard.add_hotkey('5', speedflip_5) keyboard.add_hotkey('6', speedflip_6) # Keep the script running until Q is pressed keyboard.wait('q')
Key Improvements:
- Event-driven listening: We use
keyboard.add_hotkey()to trigger our speedflip functions only when 5 or 6 are pressed—no more tight loops hogging CPU. - Cleaned up library usage: We still use
pynputfor mouse controls (since it's great for that), but stick tokeyboardfor all keyboard actions to avoid conflicts. - Removed redundant code: Got rid of duplicate
keyboard.press('w')calls and the unnecessary finalkeyboard.release('w')(since you weren't holding W at that point anyway). - Proper exit: Added a
keyboard.wait('q')to let you quit the script cleanly instead of relying on an exception break.
Extra Tips for Debugging Timing:
- If you still need to tweak timings, try using smaller sleep increments or even
time.perf_counter()for more precise timing instead oftime.sleep(). - Make sure you're running the script with admin privileges—sometimes input libraries need that to work smoothly with games.
Give this revised script a try, and your keyboard lag should be gone for good!
内容的提问来源于stack exchange,提问作者O Z

