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 shor开发者_开发百科tcuts in .NET?
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.
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.
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
You can handle the Control.KeyPress event and inspect the key combination to make keyboard shortcuts: 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
. Doing this will make Alt+O activate the button from the parent form.
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:
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.
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.
Step No.1 set
KeyPreview = True;
Step No.2
if (e.Alt && e.KeyCode == Keys.C)
{
//your function
}
精彩评论