您的位置:首页 > 其它

WPF 操作键盘

2014-07-07 11:21 253 查看
#region 打开键盘的键
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;

[DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

public enum VirtualKeys : byte
{
VK_NUMLOCK = 0x90, //数字锁定键
VK_SCROLL = 0x91,  //滚动锁定
VK_CAPITAL = 0x14, //大小写锁定
VK_A = 62
}

//获取key
public static bool GetState(VirtualKeys Key)
{
return (GetKeyState((int)Key) == 1);
}
//设置key
public static void SetState(VirtualKeys Key, bool State)
{
if (State != GetState(Key))
{
keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
#endregion

      //打开大写键盘
调用:   SetState(VirtualKeys.VK_CAPITAL, true);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: