Windows key capture in C # (wpf)

I wrote a small program that displays sounds and images on the screen when any button is pressed. I always start when one of my little children crawls to my lap and, of course, accidentally presses the keys.

It works great, except for two keys, one of which is an ON / OFF switch, and the other is a Windows key. (the equivalent of CTRL-ESC, which I suppose), I can intercept it when it is pressed, but only after the start is shown.

The event I'm using is UIElement.KeyDown, and all I could come up with so far is: (e the KeyEventArgs)

if (e.Key == Key.LWin) e.Handled = true; 

but the launch window already shows that I'm afraid.

I already have 1 answer, but I would really like to know if there is wpf support?

I suspect that programming the main on / off may not be possible? Otherwise, any help there would also be welcome.

+6
c # wpf keyboard
source share
2 answers

You will need a keyboard hook . Unfortunately, this needs to be done with P / Invoke; this cannot be done with managed code.

Check out Baby Smash! from Scott Hanselman. It was hosted on code plex at http://www.codeplex.com/babysmash Github at https://github.com/shanselman/babysmash

Alternatively, check out ShapeShow for a CodeProject that looks similar.

+17
source share

See http://msdn.microsoft.com/en-us/library/system.windows.input.key(v=VS.90).aspx

Below you will see a simple example, I think that what you are looking for is something like that:

left window key: System.Windows.Input.Key.LWin

right window key: System.Windows.Input.Key.RWin

Example:

 private void OnKeyDownHandler(object sender, KeyEventArgs e) { if (e.Key == Key.LWin) { textBlock1.Text = "You Entered: " + textBox1.Text; } } 
+3
source share

All Articles