键盘代码部份转自:http://www.cnblogs.com/cpcpc/archive/2011/02/22/2123055.html
由于喜欢用CTRL+空格键切换输入法,在WIN8上有所不习惯,所以写了个小程序,当按CTRL+空格键时向系统发送WIN+空格键。
首先使用全局快捷键,监视CTRL+空格的输入,然后再发送WIN+空格键。
[DllImport("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey(IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code ); [DllImport("user32.dll", SetLastError = true)] public static extern bool UnregisterHotKey(IntPtr hWnd, // handle to window int id // hot key identifier ); [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } /// <summary> /// 切换输入法 /// </summary> const int hotKeyId_SwitchIME = 343; /// <summary> /// 发送WIN+空格 /// </summary> /// <param name="bVk"></param> /// <param name="bScan"></param> /// <param name="dwFlags"></param> /// <param name="dwExtraInfo"></param> [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, long dwFlags, long dwExtraInfo); //在Form1_Shown时注册快捷键 private void Form1_Shown(object sender, EventArgs e) { Version win7ver = new Version("6.1.9999.9999"); if (Environment.OSVersion.Version.CompareTo(win7ver) > 0) { //ctrl+空格切换输入法 bool bSucc = RegisterHotKey(Handle, hotKeyId_SwitchIME, KeyModifiers.Control, Keys.Space); } } //发送WIN+空格 protected override void WndProc(ref Message m) { try { //固定的热键消息 0x0312 const int WM_HOTKEY = 0x0312; switch (m.Msg) { case WM_HOTKEY: #region 判断哪个功能 switch (m.WParam.ToInt32()) { case hotKeyId_SwitchIME: const byte VK_LWIN = 0x5B; const byte VK_space = 0x20; const byte KEYEVENTF_KEYUP = 0x2; const byte KEYEVENTF_EXTENDEDKEY = 0x1; keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_space, 0, KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_space, 0, KEYEVENTF_KEYUP, 0); keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); break; } #endregion break; } base.WndProc(ref m); } catch { } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //关闭时解除注册 UnregisterHotKey(Handle, hotKeyId_SwitchIME); }
可在代码中的任何地方用下列值代替键盘上的键: 值 描述 0x1 鼠标左键 0x2 鼠标右键 0x3 CANCEL 键 0x4 鼠标中键 0x8 BACKSPACE 键 0x9 TAB 键 0xC CLEAR 键 0xD ENTER 键 0x10 SHIFT 键 0x11 CTRL 键 0x12 MENU 键 0x13 PAUSE 键 0x14 CAPS LOCK 键 0x1B ESC 键 0x20 SPACEBAR 键 0x21 PAGE UP 键 0x22 PAGE DOWN 键 0x23 END 键 0x24 HOME 键 0x25 LEFT ARROW 键 0x26 UP ARROW 键 0x27 RIGHT ARROW 键 0x28 DOWN ARROW 键 0x29 SELECT 键 0x2A PRINT SCREEN 键 0x2B EXECUTE 键 0x2C SNAPSHOT 键 0x2D INSERT 键 0x2E DELETE 键 0x2F HELP 键 0x90 NUM LOCK 键 A 至 Z 键与 A - Z 字母的 ASCII 码相同: 值 描述 65 A 键 66 B 键 67 C 键 68 D 键 69 E 键 70 F 键 71 G 键 72 H 键 73 I 键 74 J 键 75 K 键 76 L 键 77 M 键 78 N 键 79 O 键 80 P 键 81 Q 键 82 R 键 83 S 键 84 T 键 85 U 键 86 V 键 87 W 键 88 X 键 89 Y 键 90 Z 键 0 至 9 键与数字 0 - 9 的 ASCII 码相同: 值 描述 48 0 键 49 1 键 50 2 键 51 3 键 52 4 键 53 5 键 54 6 键 55 7 键 56 8 键 57 9 键 下列常数代表数字键盘上的键: 值 描述 0x60 0 键 0x61 1 键 0x62 2 键 0x63 3 键 0x64 4 键 0x65 5 键 0x66 6 键 0x67 7 键 0x68 8 键 0x69 9 键 0x6A MULTIPLICATION SIGN (*) 键 0x6B PLUS SIGN (+) 键 0x6C ENTER 键 0x6D MINUS SIGN (-) 键 0x6E DECIMAL POINT (.) 键 0x6F DIVISION SIGN (/) 键 下列常数代表功能键: 值 描述 0x70 F1 键 0x71 F2 键 0x72 F3 键 0x73 F4 键 0x74 F5 键 0x75 F6 键 0x76 F7 键 0x77 F8 键 0x78 F9 键 0x79 F10 键 0x7A F11 键 0x7B F12 键 0x7C F13 键 0x7D F14 键 0x7E F15 键 0x7F F16 键
时间: 2024-10-22 20:00:54