- //命名空间
- using System.Runtime.InteropServices;
- //导入方法
- [DllImport("user32.dll", EntryPoint = "GetKeyboardState")]
- public static extern int GetKeyboardState(byte[] pbKeyState);
- //大小写状态
- public static bool CapsLockStatus
- {
- get
- {
- byte[] bs = new byte[256];
- GetKeyboardState(bs);
- return (bs[0x14] == 1);
- }
- }
- //引用,此部分根据你的需要来修改
- private void button2_Click(object sender, EventArgs e)
- {
- if (CapsLockStatus == true)
- MessageBox.Show("键盘处于大写锁定状态!");
- else
- MessageBox.Show("键盘处于小写状态!");
- }
- //该片段来自于http://www.codesnippet.cn/detail/240620134245.html
来源: http://www.codesnippet.cn/detail/240620134245.html