我有在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