如何在 C# 中以编程方式检查和切换 Num-Lock?

发布于 2024-08-03 17:05:59 字数 96 浏览 6 评论 0原文

我想以编程方式检查 的值,并能够切换数字锁定。在 C# 中执行此操作的最简单方法是什么?

原因是我想在程序启动时验证数字锁定是否为“ON”。

谢谢

I would like to programmatically check the value of, and be able to toggle num-lock. What's the simplest way to do that in C#?

The reason is that I want to verify num-lock is "ON" at program start.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2024-08-10 17:05:59

检查 如何以编程方式打开 Numlock 键

using System;
using System.Runtime.InteropServices;

class SetNumlockKeyOn
{
    [StructLayout(LayoutKind.Sequential)]
    public struct INPUT
    {
        internal int type;
        internal short wVk;
        internal short wScan;
        internal int dwFlags;
        internal int time;
        internal IntPtr dwExtraInfo;
        int dummy1;
        int dummy2;
        internal int type1;
        internal short wVk1;
        internal short wScan1;
        internal int dwFlags1;
        internal int time1;
        internal IntPtr dwExtraInfo1;
        int dummy3;
        int dummy4;
}
[DllImport(“user32.dll”)]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);

public static void SetNumlockOn()
{
    const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
    INPUT input = new INPUT();
    input.type = 0x01; //INPUT_KEYBOARD
    input.wVk = 0x90; //VK_NUMLOCK
    input.wScan = 0;
    input.dwFlags = 0; //key-down
    input.time = 0;
    input.dwExtraInfo = IntPtr.Zero;

    input.type1 = 0x01;
    input.wVk1 = 0x90;
    input.wScan1 = 0;
    input.dwFlags1 = 2; //key-up
    input.time1 = 0;
    input.dwExtraInfo1 = IntPtr.Zero;

    IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
    Marshal.StructureToPtr(input, pI, false);
    int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the MOUSEINPUT tag !!!

    //if (result == 0 || Marshal.GetLastWin32Error() != 0)
    // Console.WriteLine(Marshal.GetLastWin32Error());
    Marshal.FreeHGlobal(pI);
}

Check How to programmatically turn on the Numlock Key

using System;
using System.Runtime.InteropServices;

class SetNumlockKeyOn
{
    [StructLayout(LayoutKind.Sequential)]
    public struct INPUT
    {
        internal int type;
        internal short wVk;
        internal short wScan;
        internal int dwFlags;
        internal int time;
        internal IntPtr dwExtraInfo;
        int dummy1;
        int dummy2;
        internal int type1;
        internal short wVk1;
        internal short wScan1;
        internal int dwFlags1;
        internal int time1;
        internal IntPtr dwExtraInfo1;
        int dummy3;
        int dummy4;
}
[DllImport(“user32.dll”)]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);

public static void SetNumlockOn()
{
    const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
    INPUT input = new INPUT();
    input.type = 0x01; //INPUT_KEYBOARD
    input.wVk = 0x90; //VK_NUMLOCK
    input.wScan = 0;
    input.dwFlags = 0; //key-down
    input.time = 0;
    input.dwExtraInfo = IntPtr.Zero;

    input.type1 = 0x01;
    input.wVk1 = 0x90;
    input.wScan1 = 0;
    input.dwFlags1 = 2; //key-up
    input.time1 = 0;
    input.dwExtraInfo1 = IntPtr.Zero;

    IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
    Marshal.StructureToPtr(input, pI, false);
    int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the MOUSEINPUT tag !!!

    //if (result == 0 || Marshal.GetLastWin32Error() != 0)
    // Console.WriteLine(Marshal.GetLastWin32Error());
    Marshal.FreeHGlobal(pI);
}
甜心小果奶 2024-08-10 17:05:59

您可以通过 P/Invoke 使用 GetKeyboardState 和 keybd_event 来执行此操作

keybd_event 的 MSDN 页面准确显示了如何切换数字锁定,以及获取其状态(在 C++ 中)。

pinvoke.net 上有针对 keybd_eventGetKeyboardState

You can do this via P/Invoke with GetKeyboardState and keybd_event.

The MSDN page for keybd_event shows exactly how to toggle num-lock, as well as get it's state (in C++).

There are P/Invoke signitures available on pinvoke.net for keybd_event and GetKeyboardState.

兔小萌 2024-08-10 17:05:59

除了 Arsen 给出的答案:

There are issues with heap crashing in 64-bit builds。使用此代码的程序可能随时崩溃。要查看这一点,请启用调试选项“启用 Windows 调试堆分配器”。调试器在调用 FreeHGlobal 时停止。

它有助于计算 INPUT 结构的大小,如下所示。

int mouseInpSize = Marshal.SizeOf(input);
IntPtr pI = Marshal.AllocHGlobal(mouseInpSize);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize);
Marshal.FreeHGlobal(pI);

In addition to the answer given by Arsen:

There are problems with heap corruption in 64-bit builds. Programs using this code may crash at any point. To see this, enable the debug option "Enable Windows debug heap allocator". The debugger stops on calling FreeHGlobal.

It helps to calculate the size of the INPUT structure as follows.

int mouseInpSize = Marshal.SizeOf(input);
IntPtr pI = Marshal.AllocHGlobal(mouseInpSize);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize);
Marshal.FreeHGlobal(pI);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文