7

I have written a small program displaying sounds and images on the screen when pushing any button. I always start it when one of my little kids crawls onto my lap and start hitting the keys, of course, randomly.

It works fine except for 2 keys, one of them being the ON/OFF switch, the other being the Windows-key. (the CTRL-ESC equivalent I believe) I can intercept it as it is pressed, but only after the startmenu is showing.

The event I use is the UIElement.KeyDown and all I could came up with so far is : (the e being KeyEventArgs)

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

but than the start window is already showing I'm afraid.

I have already 1 answer but would very much like to know if there's any wpf-support?

I suspect programming the main on/off switch might not be possible? Otherwise, any help there would be welcome too..

2 Answers 2

18

You'll need a keyboard hook. Unfortunately, this has to be done with P/Invoke; it can't be done with managed code.

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

Alternatively, check out ShapeShow on CodeProject, which is similar.

Sign up to request clarification or add additional context in comments.

5 Comments

That's definitely an answer, tx. But it requires interop, no native wpf support you're aware of?
Yep. Needs interop. Updated AB's answer to explain that, and added link to ShapeShow.
Ok, If you guys are sure, thanks for answering! (I alreaydy have installed babysmash in the meantime)
Note: the answer by merrick (below) works perfectly. Though I'm not sure if that API was already available when this answer was written. (Just adding this here as I completely missed that answer the first time I read this page).
e.Key == Key.LWin is all you need. See answer by merrick
4

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

At the bottom you'll see a simple example, I think what you're looking for is something along these lines:

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

right windows 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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.