locked
How to set short cut keys to wpf menu item.... RRS feed

  • Question

  • Hi all,

    I am working on wpf application, facing a problem how to set the short cut key to menue item in wpf. Please anybody knows help me exmplain in detail...

    Thanks in advanced.

    Mohammad Sharique

    Monday, July 11, 2011 4:32 AM

Answers

  • Hey Mohammed, I hope you're doing great.

    You may use one of the following solutions:

    1- Just place an "Underscore" character (_) before the header text like this: <MenuItem Name="MenuPlay" IsEnabled="False" Header="_Play" Click="menuPlay_Click"/>

    This way you'll use the keyboard shortcut (Alt+P) because the Underscore character is put before the "P" character. This is supported in WPF by default.

    Or you may do the following if the previous solution didn't work:

    2- Just handle the KeyDown event for the mainWindow of the application like this:

    <Window ................... KeyDown="Window_KeyDown"> then in the handling procedure you may do the following:

    if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.P)) // This will capture the Ctrl+P shortcut.
    {

       // Do the Play procedure here..

    }

    3- You may also need to check the following post in MSDN of a similar solution to your problem:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bceb7fd1-58f4-4768-a5cc-4546e7904553/

    I hope this helps. Please, mark my reply as "Answer" if it helps or vote it up if it gives a clue to the solution.

    Thanks in advance.


    Ahmed M. Gamil

    Monday, July 11, 2011 9:19 AM
  • Please play with following to get an idea and make your stuff work-

    1) Code behind-

    using System.Windows;
    using System.Windows.Input;

    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            int i = 0;
            public MainWindow()
            {
                InitializeComponent();
            }
          

            private void MenuItem_Click(object sender, RoutedEventArgs e)
            {
                i++;
                label1.Content = i.ToString();
            }

            private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.P && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                {
                    // call menuPlay_Click
                    MenuItem_Click(sender, e);
                }
            }
        }
    }

    /////////////////////////////////////

    Xaml --

    /////////////////////////////////////

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" PreviewKeyDown="Window_PreviewKeyDown">
        <Grid>
            <Menu Height="23" HorizontalAlignment="Left" Margin="22,24,0,0" Name="menu1" VerticalAlignment="Top" Width="200">
                <MenuItem Header="a" InputGestureText="Ctrl+P" Click="MenuItem_Click" />
                <MenuItem Header="b" />
            </Menu>
            <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="140,162,0,0" Name="label1" VerticalAlignment="Top" Width="64" />
        </Grid>
    </Window>

    /////////

    Use previewkeydown at form not keydown as previewkeydown will be fired first


    Monday, July 11, 2011 1:16 PM

All replies

  • You can use InputGestureText property to enable keyboard shortcuts.

    Monday, July 11, 2011 5:44 AM
  • Hi, sanguine

    i used like this, still is not working can u explain in detail plz

     

     

    <MenuItem Name="menuPlay" IsEnabled="False" Header="Play" InputGestureText="Ctrl + p" Click="menuPlay_Click"/>

     

     

     

    Thnx for reply....

    Monday, July 11, 2011 5:50 AM
  • InputGestureText is just a text that informs user about the possible key combination which he can use as alternative for the menu item but it does not provide any functionality behind the scene.

    In your case you can do this-

      private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.P && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                {
                      // call menuPlay_Click
                }
            }

     

    Let me know whether this solves your purpose.

    Monday, July 11, 2011 6:37 AM
  • Hi sanguine,

    i tried as you told but still its not working can u give me runable code.....

    plz it will be Appreciate able.

    Thnx for helping.....

     

    Moahmmad Sharique

    Monday, July 11, 2011 7:26 AM
  • Since you are using event in place of command so the solution I provided is just a work around. here you have to add-

    PreviewKeyDown="Window_PreviewKeyDown" at root  level(in xaml) and above handler thenafter.

    check this.

    • Proposed as answer by _sanguine_ Monday, July 11, 2011 12:53 PM
    Monday, July 11, 2011 9:08 AM
  • Hey Mohammed, I hope you're doing great.

    You may use one of the following solutions:

    1- Just place an "Underscore" character (_) before the header text like this: <MenuItem Name="MenuPlay" IsEnabled="False" Header="_Play" Click="menuPlay_Click"/>

    This way you'll use the keyboard shortcut (Alt+P) because the Underscore character is put before the "P" character. This is supported in WPF by default.

    Or you may do the following if the previous solution didn't work:

    2- Just handle the KeyDown event for the mainWindow of the application like this:

    <Window ................... KeyDown="Window_KeyDown"> then in the handling procedure you may do the following:

    if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.P)) // This will capture the Ctrl+P shortcut.
    {

       // Do the Play procedure here..

    }

    3- You may also need to check the following post in MSDN of a similar solution to your problem:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bceb7fd1-58f4-4768-a5cc-4546e7904553/

    I hope this helps. Please, mark my reply as "Answer" if it helps or vote it up if it gives a clue to the solution.

    Thanks in advance.


    Ahmed M. Gamil

    Monday, July 11, 2011 9:19 AM
  • Please play with following to get an idea and make your stuff work-

    1) Code behind-

    using System.Windows;
    using System.Windows.Input;

    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            int i = 0;
            public MainWindow()
            {
                InitializeComponent();
            }
          

            private void MenuItem_Click(object sender, RoutedEventArgs e)
            {
                i++;
                label1.Content = i.ToString();
            }

            private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.P && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                {
                    // call menuPlay_Click
                    MenuItem_Click(sender, e);
                }
            }
        }
    }

    /////////////////////////////////////

    Xaml --

    /////////////////////////////////////

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" PreviewKeyDown="Window_PreviewKeyDown">
        <Grid>
            <Menu Height="23" HorizontalAlignment="Left" Margin="22,24,0,0" Name="menu1" VerticalAlignment="Top" Width="200">
                <MenuItem Header="a" InputGestureText="Ctrl+P" Click="MenuItem_Click" />
                <MenuItem Header="b" />
            </Menu>
            <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="140,162,0,0" Name="label1" VerticalAlignment="Top" Width="64" />
        </Grid>
    </Window>

    /////////

    Use previewkeydown at form not keydown as previewkeydown will be fired first


    Monday, July 11, 2011 1:16 PM