Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
GetKeyState (user32)
.
C# Signature:
[DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
User-Defined Types:
None.
Notes:
Here are some sample values for nVirtKey:
const int VK_SHIFT = 0x10;
const int VK_CONTROL = 0x11;
const int VK_MENU = 0x12;
Tips & Tricks:
Please add some!
Sample Code:
using System.Windows.Forms;
public static bool IsKeyLocked(Keys keyVal) {
switch( keyVal ) {
case Keys.NumLock :
case Keys.Capital :
case Keys.Scroll :
case Keys.Insert :
return (GetKeyState((int)keyVal) & 0x8001) != 0;
}
return false;
}
public static bool IsKeyPressed(Keys keyVal) {
return GetKeyState((int)keyVal) < 0;
}
- Added by Kasper -
/// <summary>
/// This method tests whether a key is down or not.
/// </summary>
/// <param name="k">The key to test.</param>
/// <returns>True if the keys is down.</returns>
public static bool IsKeyDown(System.Windows.Forms.Keys k)
{
short state = GetKeyState((int)k);
if((state & 0x8000) == 0x8000) //It's down
return true;
return false;
}
/// <summary>
/// This method tests whether a key is on or not.
/// </summary>
/// <param name="k">The key to test.</param>
/// <returns>True if the keys is on.</returns>
public static bool IsKeyOn(System.Windows.Forms.Keys k)
{
short state = GetKeyState((int)k);
if((state & 0x0001) == 0x0001) //It's on
return true;
return false;
}
Alternative Managed API:
Do you know one? Please contribute it!
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).