tags:

views:

37

answers:

2

My objective is to allow users of my app to bringup what I'm calling my debug console by pressing CTRL + F11 on their keyboard.

Simply put, I need to call a ToggleDebug(); method, which will enable my debug tracing code and display the window. I'd like for my application to do this at any point when CTRL + F11 is pressed, regardless of where the user currently has focus the cursor as long as my application is the currently focused window.

My app is deployed through Click Once -- so its a partial trust type environment.

In an old VB6 app, I had been using a wend loop with call to DoEvents() and a windows API call... needless to say, I'm hoping there is a better way now.

+2  A: 

You can handle the PreviewKeyDown event of your window.

public MainWindow()
{
    InitializeComponent();
    this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
}

void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if ((e.Key == Key.F11) && (Keyboard.Modifiers == ModifierKeys.Control))
    { 

    }
}
karmicpuppet
Why are you recomending PreviewKeyDown vs KeyDown?
Nate Bross
PreviewKeyDown on the window will surely get hit every time. KeyDown does not. For instance, if a control in your window internally handles KeyDown events and sets its e.Handled to True, that event will not get raised on the Window.
karmicpuppet
A: 

I would just add a PreviewKeyDown event to your primary window of your application. It's the easiest thing to do.

void primaryWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F11 && Keyboard.Modifiers == ModifierKeys.Control)
    {
        //do something...
    }
}
jsmith
Why are you recommending KeyDown vs PreviewKeyDown?
Nate Bross
This seems to be being swallowed by the controls on the Window, the event never fires.
Nate Bross
PreviewKeydown uses routed events which is an advantage that comes with WPF. Wasn't taking it into consideration when I answered it, I would use Preview. http://stackoverflow.com/questions/1460170/what-are-wpf-preview-events
jsmith
I added y our code to constructor as well as the method, and it never fires. Am I doing something wrong? Or do I need to set an additional property on the Window?
Nate Bross