2

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

Abhishek
  • 6,526
  • 13
  • 53
  • 83
Javidan Guliyev
  • 217
  • 4
  • 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

6

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,184
  • 3
  • 52
  • 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
  • 55,302
  • 13
  • 128
  • 150
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
  • 77,212
  • 12
  • 152
  • 179
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,582
  • 2
  • 28
  • 51