2

I want to call function when Ctrl+space pushed. I searched more but couldn't find what I want.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
Javidan Guliyev
  • 227
  • 5
  • 13
  • probably duplicate of http://stackoverflow.com/questions/1100285/how-to-detect-the-currently-pressed-key – FosterZ Nov 17 '11 at 07:58

4 Answers4

7

You need to add an event handler for KeyDown like: KeyDown="TextBox_KeyDown" on your TextBox. And then in the event handler:

if (e.Key == Key.Space && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{ 
      //Do Stuff
}
Adrian Fâciu
  • 12,414
  • 3
  • 53
  • 68
2

Use something like this:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Space && 
       (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        // Do what you need here
    }
}
Marco
  • 56,740
  • 14
  • 129
  • 152
1

This should get you working -

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Space && Keyboard.Modifiers == ModifierKeys.Control)
  { 
  }
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
1

If you want to catch all the keys, whether you have the focus or not, in your class you just need to add in the constructor:

// To capture keyboard
EventManager.RegisterClassHandler(typeof(Window), Keyboard.KeyDownEvent, new System.Windows.Input.KeyEventHandler(keyDown), true);

And add the method: (it's an example, it's not for adapted for what you want)

private void keyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Space)
    {
        code;
    }
    else if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) &&    Keyboard.IsKeyDown(Key.T))
    {
        code;
    }
}
mlemay
  • 1,622
  • 2
  • 32
  • 53