如何仅在按住W键时运行游戏移动速度修改函数?
The core issue with your current code is that the while(true) loop constantly writes your custom speed value to the memory address, regardless of whether the W key is held down. This overrides the game's attempt to set the value back to 0 when you release W, which is why your character won't stop moving.
Here's how to fix it, with step-by-step explanations and updated code:
1. Add Keyboard State Detection
We need to check if the W key is actively held down before writing to the memory address. We'll use the Windows API function GetAsyncKeyState (from user32.dll) to detect real-time key presses—it returns a non-zero value if the specified key is currently pressed.
First, add this DllImport declaration at the top of your class (outside the SpeedSpeed method):
[DllImport("user32.dll")] static extern short GetAsyncKeyState(int vKey);
The virtual key code for the W key is 0x57, which we'll use to check its state.
2. Modify the Loop Logic
Update your loop to only write the custom speed when W is held. When W is released, we stop writing to the memory address, letting the game set the value back to 0 (standing still) or 1 (normal movement) as intended. We'll also add a small sleep to the loop to avoid hogging CPU resources.
Updated Full Code
using System; using System.Runtime.InteropServices; using System.Threading; // Ensure you have the VAMemory reference included in your project class GameSpeedModifier { // Import Windows API for key state detection [DllImport("user32.dll")] static extern short GetAsyncKeyState(int vKey); // Replace these with your actual memory addresses/offsets const int PlayerBase = 0x00509B74; const int SpeedOffset = 0x1010; static void SpeedSpeed() { VAMemory vam = new VAMemory("ac_client"); // Prompt user for valid speed input Console.WriteLine("Enter speed value (0-10):"); string input = Console.ReadLine(); if (!int.TryParse(input, out int NumberSpeed) || NumberSpeed < 0 || NumberSpeed > 10) { Console.WriteLine("Invalid input! Please enter a number between 0 and 10."); return; } int LocalPlayer = vam.ReadInt32((IntPtr)PlayerBase); int address1010 = LocalPlayer + SpeedOffset; // Main loop to check W key and apply speed while (true) { // Check if W is currently held down bool isWHeld = GetAsyncKeyState(0x57) != 0; if (isWHeld) { // Only write custom speed when W is pressed vam.WriteInt32((IntPtr)address1010, NumberSpeed); } // Small delay to reduce CPU usage Thread.Sleep(10); } } // Example main thread to launch the speed modifier static void Main() { Thread speedThread = new Thread(SpeedSpeed); speedThread.Start(); Console.WriteLine("Speed modifier running. Press Ctrl+C to exit."); Console.ReadLine(); } }
How This Works
- When you hold W,
GetAsyncKeyState(0x57)returns a non-zero value, so the code writes your custom speed toaddress1010every 10ms to maintain the boosted speed. - When you release W, the code stops overriding the memory address. The game can then set the value back to 0 (for standing still) or 1 (normal movement) on its own.
- The input validation step ensures users can't enter invalid values that might break the game or modifier.
Testing the Expected Flow
- Run the program, which launches the
SpeedSpeedthread via the main thread. - Enter
10when prompted. - In-game, hold W: your character moves at 10x speed.
- Release W: the modifier stops writing to memory, and your character stops moving as the game resets the value to 0.
内容的提问来源于stack exchange,提问作者MeatCheetos

