Click here to Skip to main content
15,883,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to make my WPF app to show me a message anytime that I press 'Enter',
This is what I want to Achieve anytime that the app is working whenever I press Enter It Gives me a message or throw an exception so I can see the It's reading my key press.
Below I put my code (As I've searched so many solutions for my problem within the last week It's very similar to the codes I've found about this problem on SO, MSDN, Channel9 and so on)

My XAML code:
XML
<Window.InputBindings>
    <KeyBinding Command="{Binding throwExceptionTest}" Key="Enter"/>
</Window.InputBindings>


MainWindow code:
C#
public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }


ViewModel code:
C#
public class MyViewModel
   {
       private ICommand throwExceptionTest;

       public ICommand ThrowExceptionTest => throwExceptionTest?? (throwExceptionTest = new ActionCommand(() =>
                      {
                          MessageBox.Show("ThrowExceptionTest");
                      }));
   }


The ICommand code:
C#
public class ActionCommand : ICommand
   {
       private readonly Action _action;

       public ActionCommand(Action action)
       {
           _action = action;
       }

       public void Execute(object parameter)
       {
           _action();
       }

       public bool CanExecute(object parameter)
       {
           return true;
       }

       public event EventHandler CanExecuteChanged;
   }


The Other problem I have here As I'm a beginner in WPF is that I don't know how I can trace this keypress event.

Any help, hint, comment could be helpful.
In advance, I appreciate that you spend your time reading this question and helping me.
Thanks,

What I have tried:

I searched the past week over SO, MSDN, Channel9, YouTube and yet The problem still persists.
Posted
Updated 5-Jul-18 0:46am
v2

1 solution

You're setting the DataContext of the MainWindow to itself, so you will need to add the following to the MainWindow code behind:
C#
public ICommand MyCommand { get; }
    = new ActionCommand(() =>
    {
        Debug.WriteLine("==> Enter Key Pressed");
    });

Then add the following to your MainWindow XAML:
XML
<Window.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding MyCommand}"/>
</Window.InputBindings>

Now when you press the Enter key, the Command will fire.
 
Share this answer
 
Comments
m.r.m.40 5-Jul-18 6:54am    
Thank you very much, It worked.
Graeme_Grant 5-Jul-18 6:58am    
No Worries ... The other thing to keep in mind is that when using binding, you bind to a Property, not a class or instance variable. If you want to Bind to your ViewModel, then set the DataContext to an instance of the VM and not the MainWindow itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900