8

I have developed an On Screen Keyboard in WPF. I need to capture the key press event (via Key Board) in order to keep a track of Caps Lock, Shift etc (whether they are pressed). Please note that my application loses focus when any other application (say notepad) is opened.

Could anyone suggest how to achieve this in WPF? in short, my WPF application needs to capture the key press events even though it does not have focus. Kindly help.

Kishor
  • 81
  • 1
  • 1
  • 3
  • 3
    You have to use the Windows API to register a keyboard hook with Windows. https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/ – Logan Fields Jun 02 '17 at 23:18

2 Answers2

0

This was helpful for me : Disable WPF Window Focus

Its not exactly the same as your problem, he is trying to catch an event without getting focus , but its a good starting point

-2

I use a simple code behind:

In the xaml I use a KeyDown event called MyTestKey.

<Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        KeyDown="myTestKey" >

This is what the keydown routine looks like where I check for the number 1:

private void myTestKey(object sender, KeyEventArgs e)
{
            if ((e.Key == Key.D1) || (e.Key == Key.NumPad1))
            {
                //do some stuff here
                return;
            }
}

This is an easy way to get to any key. I hope this helps.

Mark W
  • 801
  • 2
  • 11
  • 17