仅在按住键时如何运行功能?

时间:2019-08-30 08:34:21

标签: c#

0 =静止不动,1 =移动

该地址的默认值为0,但在移动(按住W)时更改为1(默认速度)。

当我将地址更改为10时,它使我以速度10向前移动,但是当我点击W时,它重置为1。

当我不断将地址更改为10时,它不会让您停止移动,因为0表示不移动,并且它永远也不会使您到达0。

程序会更改游戏中的移动速度。

程序运行后,您不能停止移动。

我不确定如何按住W键时如何阅读,因为我想使程序仅在按住W键时运行。

尽管可能是由于我自己的错误,我几乎都尝试了所有方法。

这是一个不断将速度设置为输入数字的程序。

    static void SpeedSpeed()
    {
        VAMemory vam = new VAMemory("ac_client");

        string StringSpeed = "";
        StringSpeed = Console.ReadLine();
        int NumberSpeed = Int32.Parse(StringSpeed);

        int LocalPlayer = vam.ReadInt32((IntPtr)PlayerBase);

        int address1010 = LocalPlayer + Speed;

        //code to run when W is held.
        while (true)
        {
            vam.WriteInt32((IntPtr)address1010, NumberSpeed);
        }
    }
}

}

这里我有一个目标,那就是将速度(address1010)不断更改为用户输入的数字(NumberSpeed),但只有在按住W键时才能更改。

预期-        1.通过另一个线程(MainThread)启动线程(SpeedSpeed)。

   2. It asks you in the console to input a number from 0 to 10 from MainThread.

   3. I input 10.

   4. I go in-game and hold down the W key, which activates the program

   5. I move forward, as the program makes me do.

   6. I let go of the W key.

   7. The program stops until the W key is held again.

实际-        1.通过另一个线程(MainThread)启动线程(SpeedSpeed)。

   2. It asks you in the console to input a number from 0 to 10 from MainThread.

   3. I input 10.

   4. I go in-game and I am constantly moving forward and can't stop.

2 个答案:

答案 0 :(得分:0)

您可以使用keyPressed尝试

 bool mIsPress = false;

 yourControl.KeyPress += new KeyPressEventHandler(keypressed);

 private void keypressed(Object o, KeyPressEventArgs e)
 {
    if (e.KeyChar == (char)Keys.W)
    {
         mIspress= true;
        e.Handled = true;
    }else{
         mIspress= false;
     }
 }

此后,您可以创建一个线程

if(mIsPress){
   //your code to run
 }else{
   //your code to stop
   }

答案 1 :(得分:0)

使用DLL导入。 它使一切变得简单

这是我用来解决类似问题的链接

https://www.pinvoke.net/default.aspx/user32/GetAsyncKeyState.html

我确定此链接会为您提供帮助

使用此功能时,您必须输入using System.Runtime.InteropServices