键盘代码部份转自: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 = ,
Alt = ,
Control = ,
Shift = ,
Windows =
} /// <summary>
/// 切换输入法
/// </summary>
const int hotKeyId_SwitchIME = ; /// <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) > )
{
//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, , KEYEVENTF_EXTENDEDKEY, );
keybd_event(VK_space, , KEYEVENTF_EXTENDEDKEY, );
keybd_event(VK_space, , KEYEVENTF_KEYUP, );
keybd_event(VK_LWIN, , KEYEVENTF_KEYUP, );
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 码相同:
值 描述
A 键
B 键
C 键
D 键
E 键
F 键
G 键
H 键
I 键
J 键
K 键
L 键
M 键
N 键
O 键
P 键
Q 键
R 键
S 键
T 键
U 键
V 键
W 键
X 键
Y 键
Z 键 至 键与数字 - 的 ASCII 码相同:
值 描述
键
键
键
键
键
键
键
键
键
键 下列常数代表数字键盘上的键:
值 描述
0x60 键
0x61 键
0x62 键
0x63 键
0x64 键
0x65 键
0x66 键
0x67 键
0x68 键
0x69 键
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 键