简体   繁体   English

如何在.NET中制作键盘快捷键

[英]How to make keyboard shortcuts in .NET

I have a Windows application in C#/.NET. 我在C#/ .NET中有一个Windows应用程序。 I need to make some keyboard shortcuts to navigate between different forms. 我需要制作一些键盘快捷键来在不同的表单之间导航。 How do I make keyboard shortcuts in .NET? 如何在.NET中创建键盘快捷键?

You can use P/Invoke to register a global hotkey on the system . 您可以使用P / Invoke 在系统上注册全局热键 If you don't want something global, you can always handle KeyPress events in all of your forms. 如果您不想要全局的东西,您可以随时处理所有表单中的KeyPress事件。

I assume your trying to implement Control ? 我假设你试图实现Control ? type shortcuts like the way copy and paste work? 类似于复制和粘贴工作方式的快捷方式?

You can create a generic KeyDown and KeyUp handler that you attach to each form. 您可以创建附加到每个表单的通用KeyDownKeyUp处理程序。 Everytime you get a KeyDown store the key in a list (to account for hold down a key and hitting another). 每次你得到一个KeyDown存储键列表中的键( KeyDown按住键并击中另一个键)。 Everytime you add key to the list, check to see if you list contains any of your shortcut key combinations. 每次向列表添加密钥时,请检查列表是否包含任何快捷键组合。 If so execute whatever code you need. 如果是,请执行您需要的任何代码。

For every KeyUp event, make sure you are removing from the list (you only need to check for the shortcut on KeyDown additions. 对于每个KeyUp事件,请确保从列表中删除(您只需要检查KeyDown添加的快捷方式。

EDIT: Did a quick search and found this same solution implemented: 编辑:快速搜索,发现同样的解决方案实施:

class KeyboardShortcuts
{
    public static void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (m_keysDownList.Contains(e.Key) == false)
        {
            m_keysDownList.Add(e.Key);
            Debug.WriteLine(e.Key.ToString() + " Down");
        }

        CheckForKeyCombos();
    }

    public static void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        m_keysDownList.Remove(e.Key);
        Debug.WriteLine(e.Key.ToString() + " Up");
    }


    public static void CheckForKeyCombos()
    {
        if (m_keysDownList.Contains(System.Windows.Input.Key.LeftCtrl))
        {
            if (m_keysDownList.Contains(System.Windows.Input.Key.A))
            {
                if (m_keysDownList.Contains(System.Windows.Input.Key.C))
                {
                    // Clear list before handeling ( Dialogue boxes
                    // can hinder the listening for key up events, leaving
                    // keys in list - so clear first ).
                    ClearKeysDownList();

                    // Handle Ctrl + A + C Combo
                    HandleCtrlACCombo();
                }
            }
        }
    }

    private static void ClearKeysDownList()
    {               
        m_keysDownList.Clear();
    }

    public static void HandleCtrlACCombo()
    {
        if (handleCtrlACComboDelegate != null)
        {
            handleCtrlACComboDelegate();
        }
    }

    // Need a delegate instance for each combo 
    public delegate void HandleCtrlACComboDelegate();
    public static HandleCtrlACComboDelegate handleCtrlACComboDelegate;

    private static List<System.Windows.Input.Key> m_keysDownList = new List<System.Windows.Input.Key>();
}

You can see the full solution that this is quoted from here (just scroll to the bottom as it is on that evil site that we do not speak the name of. 你可以看到这里引用的完整解决方案(只是滚动到底部,因为它位于那个我们不会说出名字的邪恶网站上。

Such things are usually done using windows hooks. 这些东西通常使用Windows钩子完成。 Here is an MSDN article showing how to work with them: 这是一篇MSDN文章,展示了如何使用它们:

http://msdn.microsoft.com/en-us/magazine/cc188966.aspx http://msdn.microsoft.com/en-us/magazine/cc188966.aspx

Step No.1 set 第1步

        KeyPreview = True;

Step No.2 第2步

        if (e.Alt && e.KeyCode == Keys.C)
        {
            //your function
        }

您可以处理Control.KeyPress事件并检查组合键以生成键盘快捷键: http//msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

If your parent form has buttons or menus which the user selects to open sub forms, you can add a very simple hot key by adding an ampersand & to the .Text attribute. 如果你的父窗体具有用户选择打开子表单按钮或菜单,您可以通过添加符号添加一个非常简单的热键&对。文本属性。

For example, if you have a button such as Options you can alter its text as follows: &Options . 例如,如果您有一个按钮(如Options ,则可以按如下方式更改其文本: &Options Doing this will make Alt + O activate the button from the parent form. 这样做将使Alt + O激活父表单中的按钮。

Alternatively if you want hotkeys such as Ctrl + O to open a form, you'll have to subscribe to the main form's KeyDown event, and look for that key combination: 或者,如果您希望热键(如Ctrl + O)打开表单,则必须订阅主表单的KeyDown事件,并查找该组合键:

    private void FormMain_KeyDown(object sender, KeyEventArgs e)
    {
        if (ModifierKeys == Keys.Control && e.KeyCode == Keys.O))
            // open form
    }

Note that you may need to set the KeyPreview property of your form to True. 请注意,您可能需要将表单的KeyPreview属性设置为True。

Finally, tool strip menu items have a property ShortcutKeys which you can define in the properties panel, and VS will automatically add the required code to the Designer file to support the keyboard shortcut to activate the menu item. 最后,工具条菜单项具有可在属性面板中定义的属性ShortcutKeys ,VS将自动将所需代码添加到Designer文件以支持键盘快捷方式以激活菜单项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM