Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make keyboard shortcuts in .NET

I have a Windows application in C#/.NET. I need to make some keyboard shortcuts to navigate between different forms. How do I make keyboard shortcuts in .NET?

like image 530
Vaibhav Jain Avatar asked Aug 26 '10 15:08

Vaibhav Jain


People also ask

How do I create a keyboard shortcut using CMD?

Create a Keyboard Shortcut to Launch Command Prompt Windows allows you to choose a custom combination of keys for the shortcut. To assign a shortcut: Right-click the Command Prompt shortcut on your desktop and select Properties. Select the Shortcut tab at the top of the Properties window.


3 Answers

You can use P/Invoke to register a global hotkey on the system. If you don't want something global, you can always handle KeyPress events in all of your forms.

like image 110
Steve Danner Avatar answered Sep 30 '22 19:09

Steve Danner


I assume your trying to implement 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. Everytime you get a KeyDown store the key in a list (to account for hold down a key and hitting another). 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.

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.

like image 32
Kelsey Avatar answered Sep 30 '22 18:09

Kelsey


Such things are usually done using windows hooks. Here is an MSDN article showing how to work with them:

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

like image 24
DevExpress Team Avatar answered Sep 30 '22 17:09

DevExpress Team