Skip to main content Skip to footer

Hotkeys in C1Menu for WPF

When we talk about Menus, more often than not we see them with shortcut/hotkeys that can help us navigate through the menu using the keyboard itself. ComponentOne C1Menu also supports hotkeys to make the interaction easier and with this blog, we will discuss how to use shortcut/hotkeys with C1Menu and its items. To implement this behavior, we will leverage the native API provided by Microsoft which will help us implement the functionality of using hotkeys. We will use the AccessKeyManager class (which is included in the System.Windows.Input API) to register the desired hotkeys and with the help of Command Bindings. Let's take a look at the detailed step by step procedure to implement this functionality. Step1: First of all, we need to create our designer markup in XAML with a notation which defines our hotkey. For example, underline the alphabet which is used as the hotkey. In order to create underline text in C1Menu, we need to customize its header by adding a TextBlock element.


<c1:C1MenuItem x:Name="menu_File">  
<c1:C1MenuItem.Header>  
<TextBlock FontFamily="Times New Roman" FontSize="16" FontWeight="Bold"><Run Text="File("/><Underline><Run Text="F"/></Underline><Run Text=")" /></TextBlock>  
</c1:C1MenuItem.Header>  
<c1:C1MenuItem x:Name="menu_New" Command="New">  
<c1:C1MenuItem.Header>  
<TextBlock FontFamily="Times New Roman" FontSize="16" FontWeight="Bold"><Run Text="New("/><Underline><Run Text="N"/></Underline><Run Text=")"/></TextBlock>  
</c1:C1MenuItem.Header>  
</c1:C1MenuItem>  
</c1:C1MenuItem>  

Step 2: Now, we will register all the C1MenuItems with AccessKeyManager and assign an alphabet which will act as the hotkey.


AccessKeyManager.Register("f", menu_File);  
AccessKeyManager.Register("n", menu_New);  
AccessKeyManager.AddAccessKeyPressedHandler(menu_File, AccessKeyPressedEventHandler);  
AccessKeyManager.AddAccessKeyPressedHandler(menu_New, AccessKeyPressedEventHandler);  

Step 3: The next step is to define a CommandBinding for each of the C1MenuItems to perform their respective actions:


<Window.CommandBindings>  
<CommandBinding Command="New" Executed="FileNewExecute" />  
</Window.CommandBindings>  


private void AccessKeyPressedEventHandler(object sender, AccessKeyPressedEventArgs e)  
{  
var item = sender as C1MenuItem;  
if (item == null)  
return;  
switch(item.Name)  
{  
case "menu_File":  
menu_File.IsSubmenuOpen = true;  
break;  
case "menu_New":  
if(menu_File.IsSubmenuOpen)  
{  
menu_File.IsSubmenuOpen = false;  
menu_New.Command.Execute(null);  
}  
break;  
}  
e.Handled = true;  
}  

Step 4: The final step is to define the Callback methods for each C1MenuItem which will be executed when a hotkey is pressed.


private void FileNewExecute(object sender, ExecutedRoutedEventArgs e)  
{  
MessageBox.Show("New!");  
}  
private void EditCutExecute(object sender, ExecutedRoutedEventArgs e)  
{  
MessageBox.Show("Cut!");  
}  

Conclusion: With the above implementation, we can perform operations in C1Menu with the help of the keyboard. If you have some specific user requirements or customizations or suggestions regarding the same then let us know. We would be happy to cater to them :-) **See it in action](//cdn.mescius.io/assets/developer/blogs/legacy/c1/2015/04/C1Menu.zip)

MESCIUS inc.

comments powered by Disqus