Disable Keyboard globally for a WPF Window

2019-08-30 01:57发布

I need to disable Keyboard Input for all UI elements except one or 2 exceptions.

Thing is, I need the arrow keys for controlling so I added a KeyEventHandler on the MainWindow level and added another for UI Elements like Tab Items, Textbox so that they ignore the Input when they got the focus. I ended up with a pretty buggie control. I still can browse e.g. thru tab items, the control event sometimes doesn't fire. I need a more reliable way to do this..

Any suggestions are appreciated!

1条回答
Lonely孤独者°
2楼-- · 2019-08-30 02:21

The arrow keys are not considered input keys by default. Therefore they still scroll through the controls on the window. To change this behavior you can subscribe to the PreviewKeyDown event (http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewkeydown.aspx) and set PreviewKeyDownEventArgs.IsInputKey = true;, see http://msdn.microsoft.com/en-us/library/system.windows.forms.previewkeydowneventargs.isinputkey.aspx. Then the arrow keys will also invoke other key event handlers as KeyDown.

查看更多
登录 后发表回答