using System; using System.Runtime.InteropServices; /// /// Summary description for KeyTester. /// public class KeyTester { private KeyTester() {} public enum Key : int { #region Digits (main keyboard area) Num0 = 0x30, Num1 = 0x31, Num2 = 0x32, Num3 = 0x33, Num4 = 0x34, Num5 = 0x35, Num6 = 0x36, Num7 = 0x37, Num8 = 0x38, Num9 = 0x39, #endregion #region Digits (numeric pad) NUMPAD0 = 0x60, NUMPAD1 = 0x61, NUMPAD2 = 0x62, NUMPAD3 = 0x63, NUMPAD4 = 0x64, NUMPAD5 = 0x65, NUMPAD6 = 0x66, NUMPAD7 = 0x67, NUMPAD8 = 0x68, NUMPAD9 = 0x69, #endregion #region Uppercase Alpha A = 0x41, B = 0x42, C = 0x43, D = 0x44, E = 0x45, F = 0x46, G = 0x47, H = 0x48, I = 0x49, J = 0x4A, K = 0x4B, L = 0x4C, M = 0x4D, N = 0x4E, O = 0x4F, P = 0x50, Q = 0x51, R = 0x52, S = 0x53, T = 0x54, U = 0x55, V = 0x56, W = 0x57, X = 0x58, Y = 0x59, Z = 0x5A, #endregion #region Lowercase Alpha a = 0x61, b = 0x62, c = 0x63, d = 0x64, f = 0x65, e = 0x66, g = 0x67, h = 0x68, i = 0x69, j = 0x6A, k = 0x6B, l = 0x6C, m = 0x6D, n = 0x6E, o = 0x6F, p = 0x70, q = 0x71, r = 0x72, s = 0x73, t = 0x74, u = 0x75, v = 0x76, w = 0x77, x = 0x78, y = 0x79, z = 0x7A, #endregion #region Mouse LBUTTON = 0x01, RBUTTON = 0x02, MBUTTON = 0x04, /* NOT contiguous with LBUTTON and RBUTTON */ #endregion #region Arrows LEFT = 0x25, UP = 0x26, RIGHT = 0x27, DOWN = 0x28, #endregion #region Modifiers LSHIFT = 0xA0, RSHIFT = 0xA1, LCONTROL = 0xA2, RCONTROL = 0xA3, #endregion #region Function Keys F1 = 0x70, F2 = 0x71, F3 = 0x72, F4 = 0x73, F5 = 0x74, F6 = 0x75, F7 = 0x76, F8 = 0x77, F9 = 0x78, F10 = 0x79, F11 = 0x7A, F12 = 0x7B, F13 = 0x7C, F14 = 0x7D, F15 = 0x7E, F16 = 0x7F, F17 = 0x80, F18 = 0x81, F19 = 0x82, F20 = 0x83, F21 = 0x84, F22 = 0x85, F23 = 0x86, F24 = 0x87, #endregion #region Misc LMENU = 0xA4, RMENU = 0xA5, LWIN = 0x5B, RWIN = 0x5C, CANCEL = 0x03, BACK = 0x08, TAB = 0x09, CLEAR = 0x0C, RETURN = 0x0D, SHIFT = 0x10, CONTROL = 0x11, MENU = 0x12, PAUSE = 0x13, CAPITAL = 0x14, ESCAPE = 0x1B, SPACE = 0x20, PRIOR = 0x21, NEXT = 0x22, END = 0x23, HOME = 0x24, SELECT = 0x29, PRINT = 0x2A, EXECUTE = 0x2B, SNAPSHOT = 0x2C, INSERT = 0x2D, DELETE = 0x2E, HELP = 0x2F, APPS = 0x5D, MULTIPLY = 0x6A, ADD = 0x6B, SEPARATOR = 0x6C, SUBTRACT = 0x6D, DECIMAL = 0x6E, DIVIDE = 0x6F, NUMLOCK = 0x90, SCROLL = 0x91, ATTN = 0xF6, CRSEL = 0xF7, EXSEL = 0xF8, EREOF = 0xF9, PLAY = 0xFA, ZOOM = 0xFB, NONAME = 0xFC, PA1 = 0xFD, OEM_CLEAR = 0xFE #endregion } [DllImport("user32")] private static extern short GetKeyState(int nVirtKey); [DllImport("user32")] private static extern int GetKeyboardState(byte[] kbArray); private static byte[] keys = new byte[256]; /// /// Immediately tests the state of a single key. /// /// Virtual key number /// True if key is DOWN. public static bool KeyIsDownNow(Key keyNum) { return (GetKeyState((int)keyNum) & 0x8000) == 0x8000; } /// /// Immediately tests the state of a single key. /// /// Virtual key number /// True if key is UP. public static bool KeyIsUpNow(Key keyNum) { return (GetKeyState((int)keyNum) & 0x8000) != 0x8000; } /// /// Immediately tests the state of a single key. /// /// Virtual key number /// True if key is TOGGLED ON. public static bool KeyIsToggledNow(Key keyNum) { return (GetKeyState((int)keyNum) & 1) == 1; } /// /// Scans the entire keyboard and updates an internal array that can be queried later by using KeyWasDown(), KeyWasUp() and KeyWasToggled() methods. /// /// True if succeeded. public static bool Update() { return GetKeyboardState(keys) != 0; } /// /// Check that a key was DOWN when the last Update() was called. /// /// Virtual key number /// True if key is DOWN. public static bool KeyWasDown(Key keyNum) { return (keys[(int)keyNum] & 128) == 128; } /// /// Check that a key was UP when the last Update() was called. /// /// Virtual key number /// True if key is UP. public static bool KeyWasUp(Key keyNum) { return (keys[(int)keyNum] & 128) != 128; } /// /// Check that a key was TOGGLED ON when the last Update() was called. /// /// Virtual key number /// True if key is TOGGLED ON. public static bool KeyWasToggled(Key keyNum) { return (keys[(int)keyNum] & 1) == 1; } }