为可自定义的键盘快捷键创建输入

发布于 2024-12-18 22:49:35 字数 1359 浏览 1 评论 0原文

我正在使用 Visual Studio 2010 创建一个可视化 C# 应用程序,并且我希望在我的应用程序的首选项中包含一些选项,以便使用某种文本框输入自定义键盘快捷键。我了解如何记录键盘输入,以及如何将其保存到用户应用程序设置,但我找不到任何具有此功能的输入控件。

即类似这样的内容:

在此处输入图像描述

但是使用 Windows 表单(注意:以上内容来自 Divvy for OS X,来自应用商店)。

是否有任何内置功能可以处理这个问题? 我可以使用任何好的库或自定义输入吗?

否则,关于如何实施这样的事情有什么建议吗?

解决方案:

使用Bas B的答案和其他一些逻辑:

private void fShortcut_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode != Keys.Back)
    {
        Keys modifierKeys = e.Modifiers;
        Keys pressedKey = e.KeyData ^ modifierKeys; //remove modifier keys

        if (modifierKeys != Keys.None && pressedKey != Keys.None)
        {
            //do stuff with pressed and modifier keys
            var converter = new KeysConverter();
            fShortcut.Text = converter.ConvertToString(e.KeyData);
            //At this point, we know a one or more modifiers and another key were pressed
            //modifierKeys contains the modifiers
            //pressedKey contains the other pressed key
            //Do stuff with results here
        }
    }
    else
    {
        e.Handled = false;
        e.SuppressKeyPress = true;

        fShortcut.Text = "";
    }
}

上面是一种通过检查两个修饰键和另一个键是否已输入来判断何时输入有效快捷键组合的方法。按下。

I am using Visual Studio 2010 to create a visual C# application, and I want to include some options in my application's preferences for customization of keyboard shortcuts using some sort of text box input. I understand how to record keyboard input, and how to save it to the user application settings, but I can not find any input controls that have this functionality.

I.e. something like this:

enter image description here

But using windows forms (Note: The above is from Divvy for OS X from the app store).

Is there any built-in functionality to handle this?
Are there any good libraries or custom inputs I could use?

Otherwise, any suggestions on how to go about implementing something like this?

Solution:

Using Bas B's answer and some other logic:

private void fShortcut_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode != Keys.Back)
    {
        Keys modifierKeys = e.Modifiers;
        Keys pressedKey = e.KeyData ^ modifierKeys; //remove modifier keys

        if (modifierKeys != Keys.None && pressedKey != Keys.None)
        {
            //do stuff with pressed and modifier keys
            var converter = new KeysConverter();
            fShortcut.Text = converter.ConvertToString(e.KeyData);
            //At this point, we know a one or more modifiers and another key were pressed
            //modifierKeys contains the modifiers
            //pressedKey contains the other pressed key
            //Do stuff with results here
        }
    }
    else
    {
        e.Handled = false;
        e.SuppressKeyPress = true;

        fShortcut.Text = "";
    }
}

The above is a way to tell when a valid shortcut combination is entered by checking if both modifier keys, and another key is pressed.

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

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

发布评论

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

评论(1

愁杀 2024-12-25 22:49:35

您可以让用户在 TextBox 中输入首选快捷方式,然后处理 KeyDown 事件,例如:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        Keys modifierKeys = e.Modifiers;

        Keys pressedKey = e.KeyData ^ modifierKeys; //remove modifier keys

        //do stuff with pressed and modifier keys
        var converter = new KeysConverter();
        textBox1.Text = converter.ConvertToString(e.KeyData);
}

编辑:更新以包含 Stecya 的答案。

You could have the user enter the preferred shortcut in a TextBox, then handle the KeyDown event, for instance:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        Keys modifierKeys = e.Modifiers;

        Keys pressedKey = e.KeyData ^ modifierKeys; //remove modifier keys

        //do stuff with pressed and modifier keys
        var converter = new KeysConverter();
        textBox1.Text = converter.ConvertToString(e.KeyData);
}

Edit: Updated to include Stecya's answer.

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