14Dec/091
GetAsyncKeyState not working? Look here!
Especially when running on a 64-bit system you may run into problems. To ensure your application works on 32-bit and 64-bit you have to make some small changes to the API declaration.
To make something clear: Even though it says "user32.dll" it is actually a dll with 64-bit functions. The 32-bit version of it is in the WOW64 folder.
wrap-lines: true
This is the code I am using:
using System.Runtime.InteropServices;
static class VKeyboard
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetAsyncKeyState(IntPtr vKey);
public static bool CheckKeyDown(System.Windows.Forms.Keys vKey)
{
return 0 != ((Int64)GetAsyncKeyState((IntPtr)vKey) & 0x8000);
}
}To call this just use:
if (VKeyboard.CheckKeyDown(Keys.ControlKey))
{
//do something
}The integer types were changed to IntPtr because IntPtr is a wrapper that changes depending on the system (32-bit or 64-bit). You can change vKey as well to IntPtr to save the typecast but I like the "Keys." calling method.
Enjoy.