发布时间:2018-08-14编辑:佚名阅读(2206)
[DllImport("user32.dll", EntryPoint = "GetKeyboardState")] public static extern int GetKeyboardState(byte[] pbKeyState); //定义键盘变量及函数 [DllImport("user32")] static extern void keybd_event( byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo ); const uint KEYEVENTF_EXTENDEDKEY = 0x1; const uint KEYEVENTF_KEYUP = 0x2; /// <summary> /// 模拟按键 /// </summary> /// <param name="key"></param> public static void KeyBoardDo(int key) { keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } /// <summary> /// 模拟按键 /// </summary> /// <param name="key"></param> public static void KeyBoardDo(int[] key) { foreach (int k in key) { keybd_event((byte)k, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); } foreach (int k in key) { keybd_event((byte)k, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } /// <summary> /// 获取大小写状态 True为大写 False为小写 /// </summary> public static bool CapsLockStatus { get { byte[] bs = new byte[256]; GetKeyboardState(bs); return (bs[0x14] == 1); } } /// <summary> /// 设置大小写状态 /// </summary> /// <param name="result"></param> public static void SetCapsLock(bool result) { if (CapsLockStatus != result) { KeyBoardDo(20); } }
关键字: C#获取和修改大小写状态
0人
0人
0人
0人