如何处理可自定义的热键设置? [英] How can I handle a customizable hotkey setting?

查看:123
本文介绍了如何处理可自定义的热键设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的应用程序用户为某些功能设置键盘热键的选项.

I'm trying to let my app's users an option to set a keyboard hotkey for for some feature.

现在我正在将TextBoxKeyDown事件一起使用,如下所示:

Right now I'm using a TextBox with the KeyDown event as follows:

Key Hotkey;
private void SetHotKey(object sender, KeyEventArgs e)
{
    (sender as TextBox).Text = e.Key.ToString();
    Hotkey = e.Key;
    e.Handled = true;
}

此方法的问题是我无法设置复杂的快捷方式,例如[Ctrl] + [F4].是否有一些第三方控制对此有所帮助?还是更适合在文本框上订阅的活动?

The problem with this approach is I can't set complex shortcuts, like [Ctrl]+[F4]. Is there some 3rd-party control that helps with that? Or a better-suited event to subscribe to on a textbox?

更新: 我更改了代码,但看来我还是做错了.

UPDATE: I've changed my code but it seems I'm still doing something wrong.

Key Hotkey;
bool lControl = false;
bool lAlt = false;
bool lShift = false;
private void SetHotKey(object sender, KeyEventArgs e)
{
    var k = e.Key;
    if (e.IsDown)
    {
        var tb = sender as TextBox;
        tb.Text = "";
        lControl = Keyboard.IsKeyDown(Key.LeftCtrl);
        lAlt = Keyboard.IsKeyDown(Key.LeftAlt);
        lShift = Keyboard.IsKeyDown(Key.LeftShift);
        if (lControl) tb.Text += "Ctrl+";
        if (lAlt) tb.Text += "Alt+";
        if (lShift) tb.Text += "Shift+";
        tb.Text = e.Key.ToString();
        Hotkey = e.Key;
    }
    e.Handled = true;
}

如何使它工作并看起来更干净?

How can I make it work and look cleaner as well?

推荐答案

让您以正确的方向开始.

To get you started in the right direction.

首先,您需要在System/ImeProcessed/DeadCharProcessed密钥后面的真实密钥.可以使用扩展方法来完成此操作,以便于访问.

First you will need the real key behind the System/ImeProcessed/DeadCharProcessed key. This can be done with extension method for easier access.

public static Key RealKey(this KeyEventArgs e)
{
    switch (e.Key)
    {
        case Key.System:
            return e.SystemKey;

        case Key.ImeProcessed:
            return e.ImeProcessedKey;

        case Key.DeadCharProcessed:
            return e.DeadCharProcessedKey;

        default:
            return e.Key;
    }
}

然后您应该将修改器的格式设置为快捷方式,而不仅仅是按下的键.您可以使用Keyboard.ModifierKeys来获取标志,并且为了便于格式化,可以将它们收集在列表中.而且,您应该只阻止修改键(Ctrl,Alt和Shift)来更新热键.

And then you should format your modifiers to the shortcut, not just the Key which was pressed. You can use Keyboard.ModifierKeys to get flags and for easier formatting, gather those in a list. And also you should block just a modifier key (Ctrl, Alt and Shift) from updating the hotkey.

private void SetHotKey(object sender, KeyEventArgs e)
{
    var nonShortcuttableKeys = new[] { Key.LeftAlt, Key.RightAlt, Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift };
    var actualKey = e.RealKey();

    if (e.IsDown && !nonShortcuttableKeys.Contains(actualKey))
    {
        var tb = sender as TextBox;

        var modifiers = new List<ModifierKeys>();
        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
        {
            modifiers.Add(ModifierKeys.Control);
        }

        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))
        {
            modifiers.Add(ModifierKeys.Alt);
        }

        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift))
        {
            modifiers.Add(ModifierKeys.Shift);
        }

        tb.Text = modifiers.Count == 0
            ? string.Format("{0}", actualKey)
            : string.Format("{0} + {1}", string.Join(" + ", modifiers), actualKey);

        Hotkey = actualKey;
    }

    e.Handled = true;
}

这篇关于如何处理可自定义的热键设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆