【发布时间】: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) 写了一个无限循环。变量永远不会改变,因为线程永远不会退出这个循环。
-
但是我可以调用其他可以改变它的函数吗?