获取哪些键正在被按下
[DllImport("user32.dll", EntryPoint = "keybd_event")] private static extern void keybd_event( byte bVk, //虚拟键值 byte bScan,// 一般为0 int dwFlags, //这里是整数类型 0 为按下,2为释放 int dwExtraInfo //这里是整数类型 一般情况下设成为 0 ); [DllImport("user32.dll", EntryPoint = "GetKeyboardState", SetLastError = true)] private static extern bool NativeGetKeyboardState([Out] byte[] keyStates); public static byte GetVirtualKeyCode(Keys key) { int value = (int)key; return (byte)(value & 0xFF); } private static List<string> GetAllPressedKey() { var array = new byte[256]; NativeGetKeyboardState(array); var keyNames = Enum.GetNames(typeof(Keys)).ToList(); var keysList = new List<string>(); foreach (var keyName in keyNames) { var key = (Keys)Enum.Parse(typeof(Keys), keyName); var code = GetVirtualKeyCode(key); if ((array[code] & 0x80) != 0) { keysList.Add(keyName); } } return keysList; } private static bool CheckAnyKeyPressedAndRelease() { var array = new byte[256]; NativeGetKeyboardState(array); var keyNames = Enum.GetNames(typeof(Keys)).ToList(); var keysList = new List<string>(); foreach (var keyName in keyNames) { var key = (Keys)Enum.Parse(typeof(Keys), keyName); keybd_event((byte)key, 0, 2, 0); } return keysList.Count > 0; }
桂棹兮兰桨,击空明兮溯流光。