-1

I have a Window which has a Frame containing a Page from another project. I want to get notified if the user presses the Enter button. The problem I'm facing:

When I press the Enter button not the event is triggered but instead the context menu shown in the picture appears. I have tried several things with Focus() and Keyboard.SetFocus() but nothing helped. enter image description here

The MainWindow is maximized and the WindowStyle is set to none but even when I change it does not change anything. If you need further information feel free to ask.

if (e.Key == Key.Enter)
{
    ValidateCredentials();
}
M Stoerzel
  • 300
  • 1
  • 13
  • 1) which event is not triggered? (there are many events related to keys) 2) Can you show code of your event handler? 3) Can you post a screenshot of your whole window so we can see more clearly what you are trying to achieve? Thanks – pm101 Jun 11 '19 at 10:44

2 Answers2

0

The problem was as following: As demanded it was necessary to navigate through the application with the function keys F1 to F12. F10 Key activates as default the menu bar. F10 was the Navigation Key for the page from above. So when I pressed F10 to navigate to the page the menu bar has got the focus. When I press Enter the menu bar gets opened.

Solution is set the F10 key to handled.

M Stoerzel
  • 300
  • 1
  • 13
0

A better answer is using the correct event.

You need to use the KeyDown event to trap keystrokes. The KeyPress or KeyUp events are too late in the pipeline and are referred back to default OS Context Menu behaviour. You could use Function keys but that's a hack the users will despise (many keyboards don't have function keys anymore).

See this example with the Mouse instead of the Keyboard, same device input logic applies: https://stackoverflow.com/a/53255798/495455

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321