WPF 操作键盘

 1         #region 打开键盘的键
 2         const uint KEYEVENTF_EXTENDEDKEY = 0x1;
 3         const uint KEYEVENTF_KEYUP = 0x2;
 4 
 5         [DllImport("user32.dll")]
 6         static extern short GetKeyState(int nVirtKey);
 7         [DllImport("user32.dll")]
 8         static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
 9 
10         public enum VirtualKeys : byte
11         {
12             VK_NUMLOCK = 0x90, //数字锁定键
13             VK_SCROLL = 0x91,  //滚动锁定
14             VK_CAPITAL = 0x14, //大小写锁定
15             VK_A = 62
16         }
17 
18         //获取key
19         public static bool GetState(VirtualKeys Key)
20         {
21             return (GetKeyState((int)Key) == 1);
22         }
23         //设置key
24         public static void SetState(VirtualKeys Key, bool State)
25         {
26             if (State != GetState(Key))
27             {
28                 keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
29                 keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
30             }
31         }
32         #endregion

      //打开大写键盘
调用:   SetState(VirtualKeys.VK_CAPITAL, true);

 

posted @ 2014-07-07 11:21  b̶i̶n̶g̶.̶  阅读(438)  评论(0编辑  收藏  举报