GetAsyncKeyState (user32)
Last changed: -


// Somebody wiped this out for no good reason so here is a snippet of it

// Documentation for this API is available here

[DllImport("user32.dll", SetLastError = true)]

public static extern short GetAsyncKeyState(int vKey);

// An example

const int VK_PRINT = 0x2A;

const int VK_LWIN = 0x5B;

// Takes in a constant int that determines which key scan code to return

// The constants are available here

// You can also use Keys enums i.e. Keys.Left, but they may not match exactly.

// If the key is currently down, the value is non zero

if(GetAsyncKeyState(VK_LWIN) != 0)

// If the key has just been pressed, the first bit will be set to 1 so you can "AND" the rest of the bits away and check if the value equals

// A more detailed explanation is available here

if((GetAsyncKeyState(VK_PRINT) & 1) == 1)

// Another Example

[DllImport("user32.dll", SetLastError = true)]

public static extern bool GetAsyncKeyState(int vKey);

if(GetAsyncKeyState((int)Keys.A) == true)