原文:https://blog.csdn.net/z397164725/article/details/7377815
测试确实可以!
1.//鼠标在UI移动才能获取,不建议使用
[DllImport("user32.dll", EntryPoint = "GetKeyboardState")]
public static extern int GetKeyboardState(byte[] pbKeyState);
//大小写状态
byte[] bs = new byte[256];
GetKeyboardState(bs);
if(bs[0x14] == 1);
{
MessageBox.Show("按下");
}
2.
[DllImport("USER32", SetLastError = true)]
static extern short GetKeyState(int nVirtKey);
//大小写状态
if (GetKeyState(20) == 1)
{
MessageBox.Show("按下");
}
3.
//大小写状态
if (IsKeyLocked(Keys.CapsLock))
{
MessageBox.Show("按下");
}