GetAsyncKeyState多个字符

时间:2014-12-01 18:28:06

标签: c# winapi

我遇到GetAsyncKeyState问题。

public static void GetKey()
{
    string keyBuffer = "";

    foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
    {
        if (Enum.GetName(typeof(Keys), i) != "LButton")
        {
            int x = GetAsyncKeyState(i);
            if ((x == 1) || (x == -32767) || (x == -32768))
            {
                keyBuffer += Enum.GetName(typeof(Keys), i) + " ";
                Object ki = Enum.ToObject(typeof(Keys), i);

                if (keyBuffer == "LButton")
                {
                }
                if (keyBuffer == "RButton")
                {
                }
                if (keyBuffer == "LShiftKey")
                {
                }
                if (keyBuffer == "RShiftKey")
                {
                }
                if (keyBuffer == "RMouse")
                {
                }
                if (keyBuffer == "LMouse")
                {
                }
                else
                {
                    Console.WriteLine(ki);
                }
            }
        }
    }

问题是我在将代码更改为:

时获得了多个密钥
if ((x == 1) || (x == -32767))

我只得到了一个,但我没有从excel获得密钥。

1 个答案:

答案 0 :(得分:0)

看一下你的代码,我看到了一些问题。

GetAsyncKeyState返回已签名的16位short。如果你没有这样定义,你应该改变你的定义。我这样说是因为我注意到你使用int来保存返回值并想知道你是否正确地声明了这个函数。

我在您的代码中看到的另一个问题是您测试的密钥是否错误。测试返回值是否为负,以检查密钥是否已关闭:

if (GetAsyncKeyState(...) < 0)
{
    // key is down
}

除此之外,我希望您在使用GetAsyncKeyState时犯了错误。从您的评论来看,您正在尝试阅读文本输入,GetAsyncKeyState是错误的功能。