-
Mar 1st, 2019, 04:05 PM
#1
Thread Starter
Lively Member
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.
-
Mar 1st, 2019, 08:29 PM
#2
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;
}
}
-
Mar 6th, 2019, 09:37 AM
#3
Thread Starter
Lively Member
Re: WPF Binding Custom Shortcut Keys.
Originally Posted by dee-u
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.
-
Mar 6th, 2019, 10:03 AM
#4
Re: WPF Binding Custom Shortcut Keys.
This is in the wrong forum.
-
Mar 6th, 2019, 10:05 AM
#5
Re: WPF Binding Custom Shortcut Keys.
Originally Posted by dbasnett
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|