我有在C#中相当一个问题,现在我试图代码时按F8键被按下检测功能,并设置布尔F8Pushed为true,那么当它再次按下设定F8Pushed为false。如何当一个键被按下一个布尔变量设置为true和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());
}
}
现在,我有和无法解决的问题是,我不能找到一个办法让变量自身复制到另一个变量,并如果有意义的话,能够回到错误。我可以得到一个布尔值设置为true,如果IsKeyPressed() == true
但我无法弄清楚如何得到它回false时,它再次返回true。
在此先感谢!
编辑: 感谢您的帮助家伙但是我仍然有一些麻烦,下面有一个更新。
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);
}
}
我设法得到它的工作有点不过,它是非常错误和很多次关键中风没有检测到任何想法?
看起来你已经写了一个无限循环与你而(真)。该变量永远不会改变,因为线程永远不会离开这个循环。 – Mishax
但我可以调用其他功能,可以改变它的权利? – user3885393