Santhosh
Santhosh

Reputation: 20406

focus wpf window on short cut key

I have created a WPF application, where if user press ctl + alt + s, my WPF application textbox needs to be focused.

Example: if you press ctl+w, automatically word web will get focused.

Thanks in advance.

Upvotes: 4

Views: 2566

Answers (3)

Navid Rahmani
Navid Rahmani

Reputation: 7958

You can achive this by using low level keyboard hook

http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

Upvotes: 1

brunnerh
brunnerh

Reputation: 184296

Use InputBindings, define a KeyBinding and create a command which does the focusing.

  <Window.InputBindings>
    <KeyBinding  Command="{Binding MyFocusCommand}" Key="S" Modifiers="Control+Alt"/>
  </Window.InputBindings>

Upvotes: 3

Alex Aza
Alex Aza

Reputation: 78447

You can subscribe to PreviewKeyDown event:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Alt) && 
        e.Key == Key.S)
    { 
        textBox1.Focus();
    }
}

Upvotes: 2

Related Questions