在 C# 中禁用数字锁定切换?

发布于 2024-08-03 17:15:03 字数 187 浏览 12 评论 0原文

我希望只要我的应用程序正在运行,就将 num-lock 保持在 ON 状态,这样,如果用户取消切换 num-lock,它将立即重新打开。在 C# 中实现这一目标的最简单方法是什么?

需要澄清的是,当我的应用程序运行时,我“拥有”用户的计算机,因此在我的特定情况下,用户不需要取消切换数字锁定(这并不意味着我始终拥有焦点)。

谢谢

I would like to maintain num-lock ON as long as my application is running, so that if the user un-toggles num-lock, it will immediately be toggled back on. What's the simplest way to achieve that in C#?

To clarify, while my application is running I "own" the user's machine, so in my specific case there will not be a need for the user to un-toggle num-lock (that does not mean I have focus at all times).

Thanks

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

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

发布评论

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

评论(3

始终不够 2024-08-10 17:15:03

您可以通过几次 P/Invoke 调用来完成此操作。查看此页面

You can do it with a few P/Invoke calls. Check out this page

国际总奸 2024-08-10 17:15:03

在表单上启用 Form.KeyPreview,添加对 Microsoft.VisualBasic 的引用(或者您可以直接使用本机 API 来轮询数字锁定键的状态)。

public static class NativeMethods
{
    public const byte VK_NUMLOCK = 0x90;
    public const uint KEYEVENTF_EXTENDEDKEY = 1;
    public const int KEYEVENTF_KEYUP = 0x2;

    [DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

    public static void SimulateKeyPress(byte keyCode)
    {
        keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
        keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
}

public partial class Form1 : Form
{
    private bool protectKeys; // To protect from inifite keypress chain reactions

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (protectKeys)
            return;

        if (e.KeyCode == Keys.NumLock && 
            !(new Microsoft.VisualBasic.Devices.Keyboard().NumLock))
        {
            protectKeys = true;
            NativeMethods.SimulateKeyPress(NativeMethods.VK_NUMLOCK);
            protectKeys = false;
        }
    }
}

Enable Form.KeyPreview on your form, add a reference to Microsoft.VisualBasic (or you can use the native API directly to poll the state of the num lock key).

public static class NativeMethods
{
    public const byte VK_NUMLOCK = 0x90;
    public const uint KEYEVENTF_EXTENDEDKEY = 1;
    public const int KEYEVENTF_KEYUP = 0x2;

    [DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

    public static void SimulateKeyPress(byte keyCode)
    {
        keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
        keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
}

public partial class Form1 : Form
{
    private bool protectKeys; // To protect from inifite keypress chain reactions

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (protectKeys)
            return;

        if (e.KeyCode == Keys.NumLock && 
            !(new Microsoft.VisualBasic.Devices.Keyboard().NumLock))
        {
            protectKeys = true;
            NativeMethods.SimulateKeyPress(NativeMethods.VK_NUMLOCK);
            protectKeys = false;
        }
    }
}
小矜持 2024-08-10 17:15:03

您需要添加一个低级键盘挂钩来执行此操作。 Stephen Toub 在他的博客上撰写了关于设置的教程这个起来。

您的键盘挂钩可以检查 VK_NUMLOCK 的状态。有关 VB 示例,请参见此处

You need to add a low level keyboard hook to do this. Stephen Toub wrote a tutorial on his blog on setting this up.

Your keyboard hook can check the status of VK_NUMLOCK. For a VB example see here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文