【问题标题】:How to set a bool variable to true when a key is pushed and false when it is pushed again?按下键时如何将布尔变量设置为true,再次按下键时如何设置为false?
【发布时间】:2017-05-24 13:38:37
【问题描述】:

我现在在 C# 中遇到了一个相当大的问题,我正在尝试编写一个函数来检测何时按下 F8 按钮并将 bool F8Pushed 设置为 true,然后在再次按下时将 F8Pushed 设置为 false。

这是我目前的代码:

static bool IsKeyPressed() //This Function Returns True if F8 Is Pushed and False if F8 is up
{
    bool is_pressed = (GetAsyncKeyState(119) & 0x8000) != 0;
    return is_pressed;
}

static void CheckHotKey() //This is the Function that I am calling the other function from for debugging.
{
    while (true)
    {
        Console.WriteLine(IsKeyPressed());
    }
}

现在我遇到并且无法解决的问题是,我无法找到一种方法让变量将其自身复制到另一个变量,并且如果这有意义的话能够返回 false。如果 IsKeyPressed() == true 我可以将 bool 设置为 true,但是当它再次返回 true 时,我无法弄清楚如何将其恢复为 false。

提前致谢!

编辑: 感谢你们的帮助,但是我仍然遇到一些问题,请更新。

static bool IsKeyPressed()
    {
        is_pressed = (GetAsyncKeyState(119) & 0x8000) != 0 && !is_pressed; 
        return is_pressed;
    }

    static void CheckHotKeys()
    {
        while (true)
        {
            IsKeyPressed();
            if(is_pressed)
            {
                F8Pushed = true;
            }
            Console.WriteLine(F8Pushed);
            if(IsKeyPressed())
            {
                F8Pushed = false;
            }
            Console.WriteLine(F8Pushed);
        }
    }

我设法让它工作了,但是它非常有问题,而且很多时候击键没有检测到任何想法?

【问题讨论】:

  • 看起来你用你的 while(true) 写了一个无限循环。变量永远不会改变,因为线程永远不会退出这个循环。
  • 但是我可以调用其他可以改变它的函数吗?

标签: c# boolean hotkeys


【解决方案1】:

我不确定我是否完全理解您要完成的工作。您的代码包含很多有问题的部分,我选择编写一个全新的代码:

class Program
{
  const byte VK_F8 = 0x77;
  const byte VK_ESC = 0x1b;

  static bool globalAppState = false;

  static void Main(string[] args)
  {
    bool lastState = IsKeyPressed(VK_F8);
    while (!IsKeyPressed(VK_ESC))
    {
      bool newState = IsKeyPressed(VK_F8);
      if (lastState != newState)
      {
        if (newState)
        {
          Console.WriteLine("F8: pressed");
          globalAppState = !globalAppState;
        }
        else
          Console.WriteLine("F8: released");
        lastState = newState;
      }
    }
  }

  static bool IsKeyPressed(byte keyCode)
  {
    return ((GetAsyncKeyState(keyCode) & 0x8000) != 0);
  }

  [DllImport("user32.dll")]
  static extern short GetAsyncKeyState(int vKey);
}

函数IsKeyPressed(VK_F8) 始终告诉您指定键的当前状态(按下/释放)。

当您只需要对更改执行一些操作时(从按下到释放,或从释放到按下)用您的特定任务替换控制台输出功能。

当您需要一些多线程(例如在新线程中处理事件)时,这是一个不同的问题...(超出此范围)

编辑:在每个新的按键事件上添加了变量的变化。这是肮脏的解决方案...

【讨论】:

    【解决方案2】:

    您必须将该变量 is_pressed 设为全局变量以保持当前状态。然后像这样尝试:

    static bool is_pressed;
    static bool IsKeyPressed()
    {
        is_pressed = (GetAsyncKeyState(119) & 0x8000) != 0 && !is_pressed;
        return is_pressed;
    }
    

    && !is_pressed 每次都会为您切换值

    【讨论】:

      【解决方案3】:

      假设您将按键的结果存储在布尔值中:

      bool isKeyPressed = true;
      

      然后你可以这样做:

      isKeyPressed == IsKeyPressed() && !isKeyPressed;
      

      【讨论】:

        猜你喜欢
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-27
        • 2020-06-23
        相关资源
        最近更新 更多