我一直在C#中使用GetAsyncKeyState,我不明白我在這裏做錯了什麼,我希望這輸出到控制檯,每次我按空間,但它沒有。我在這個小問題上花費了很長時間,需要你的幫助!爲什麼在持有所需密鑰時不輸出我的文本?
namespace key_input
{
class Program
{
#region DLLS
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(int vKey);
#endregion
static void Main(string[] args)
{
if (true)
{
Console.ReadLine();
while (GetAsyncKeyState(32) > 0)
{
Console.WriteLine("You are pressing the spacebar!");
}
}
}
}
}
[GetAsyncKeyState文檔](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(V = vs.85)的.aspx)'備註:GetAsyncKeyState功能可與鼠標按鈕.' –
當你的代碼開始時,它會觸擊'ReadLine'方法並等待你按下Enter鍵。一旦你按下Enter鍵,代碼就會繼續進入while循環並檢查鍵的狀態。如果沒有按下,程序將運行到'Main'方法的末尾並立即退出。如果空格鍵在檢查時沒有按下,它將從'while'循環中退出。 –
@ blaze_125 - 此方法不能使用鼠標按鈕。文檔中的這種說法意味着它可以使用鼠標按鈕_also_。 –