Search code examples
c#pinvokec#-2.0

GetAsyncKeyState on windows 7 x64


I'm trying to use GetAsyncKeyState(i) on windows7 x64 with C# to get pressed keys. It works perfect on x86. Here are my codes:

[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(long vKey);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode); 

        search = false;
        int key_my;
        for (i = 0; i < 255; i++)
        {
            key_my = GetAsyncKeyState(i); // this should return -3.... but it does 46...........
            if ( key_my == (System.Int16.MinValue + 1))
            { search = true; break; }
        }
        if ( search == true)
        {
           ...//using if to keys here.
        }

any IDEA?


Solution

  • The GetAsyncKeyState function should have a return type of short not int and it's parameter should be typed to int not long

    [DllImport("user32.dll")]
    public static extern short GetAsyncKeyState(int vKey);