Results 1 to 5 of 5

Thread: WPF Binding Custom Shortcut Keys.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2016
    Posts
    106

    WPF Binding Custom Shortcut Keys.

    So I have an app that has buttons for Print and Save. Now I want to add ctrl +p and ctrl + s to perform the same functions. However, for printing my application has alot of code for custom print handling.

    I am trying to follow the example in this article.
    http://www.blackwasp.co.uk/WPFKeyBindings.aspx

    I have a datacontext class
    Code:
    Public Class MyDataContext
    
        Private _keycommands As ICommand = New KeyCommands()
    
        Public ReadOnly Property KeyCommands As ICommand
            Get
                Return _keycommands
            End Get
        End Property
    
    End Class
    and the KeyCommands class
    Code:
    Public Class KeyCommands
        Implements ICommand
        ';Public Shared ReadOnly Property Print As RoutedUICommand = New RoutedUICommand("Print The Document", "Print", GetType(KeyCommands))
        Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
    
        Public Sub Execute(parameter As Object) Implements ICommand.Execute
    
            Dim msg As String = String.Empty
    
            If parameter Is Nothing Then
                msg = "something"
            Else
                msg = parameter.ToString
            End If
            MessageBox.Show(msg)
        End Sub
    
        Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
            Return True
        End Function
    End Class
    Under initialization in MainWindow.vb, I instantiate
    Code:
     InitializeComponent()
            Dim dataContext As New MyDataContext
    and my Xaml is simply
    Code:
     <Window.InputBindings>
            <KeyBinding Command="{Binding KeyCommands}" Key="P" Modifiers="Ctrl" />
        </Window.InputBindings>
    The only thing that happens is the myDataContext class gets instantiated at runtime. I would like to capture the key press and then run my print code or my save code.

    Actually what would be super useful is if I could capture the keys and then just execute my printButton_Click event.

    For whatever reason, this Binding stuff is kicking my ass. Any help would be greatly appreciated.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: WPF Binding Custom Shortcut Keys.

    If you are amenable to a non-binding solution then this is what I am currently using:

    Code:
    AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
    Code:
    private void HandleKeyDownEvent(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
                {
                    MessageBox.Show("CTRL + SHIFT + TAB trapped");
                }
    
                //CTRL + F = Find
                //CTRL + S = Save and Close
                //CTRL + N = Save and New
    
                if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                {
                    buttonSaveNew.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                    e.Handled = true;
                }
                else if (e.Key == Key.E && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                {
                    buttonSaveClose.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                    e.Handled = true;
                }
            }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2016
    Posts
    106

    Re: WPF Binding Custom Shortcut Keys.

    Quote Originally Posted by dee-u View Post
    If you are amenable to a non-binding solution then this is what I am currently using:

    Code:
    AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
    Code:
    private void HandleKeyDownEvent(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
                {
                    MessageBox.Show("CTRL + SHIFT + TAB trapped");
                }
    
                //CTRL + F = Find
                //CTRL + S = Save and Close
                //CTRL + N = Save and New
    
                if (e.Key == Key.N && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                {
                    buttonSaveNew.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                    e.Handled = true;
                }
                else if (e.Key == Key.E && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                {
                    buttonSaveClose.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                    e.Handled = true;
                }
            }
    Thank you for your response. Yeah this is how I have been doing it in the past and it works great. I was trying to learn a new way to do it.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: WPF Binding Custom Shortcut Keys.

    This is in the wrong forum.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: WPF Binding Custom Shortcut Keys.

    Quote Originally Posted by dbasnett View Post
    This is in the wrong forum.
    Not anymore.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width