一尘不染

如何在.NET中找到NumLock,CapsLock和ScrollLock的状态?

c#

如何在.NET中找到NumLock,CapsLock和ScrollLock键的状态?


阅读 394

收藏
2020-05-19

共1个答案

一尘不染

导入WinAPI函数GetKeyState

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);

然后你可以像那样使用它

bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;

编辑:以上内容适用于框架1.1,适用于框架2.0 +

Control.IsKeyLocked

2020-05-19