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

Python使用Keyboard模块实现游戏特技脚本时出现键盘卡顿问题求助

Fixing Keyboard Lag in Your Python Game Stunt Script

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 pynput for mouse controls (since it's great for that), but stick to keyboard for all keyboard actions to avoid conflicts.
  • Removed redundant code: Got rid of duplicate keyboard.press('w') calls and the unnecessary final keyboard.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 of time.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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 20:52:29