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

2023-09-02 01:33:19 作者:别靠近我心

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

How can I find the state of NumLock, CapsLock and ScrollLock keys in .net ?

推荐答案

导入WinAPI的功能函数GetKeyState

Import the WinAPI function GetKeyState

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

,然后你可以使用它像

and then you can use it like that

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 +你可以使用

the above is for framework 1.1, for framework 2.0 + you can use

Control.IsKeyLocked