views:

53

answers:

1

I have an application which uses PreviewKeyDown to capture CTRLX keystrokes (where X is any letter) along the lines of:

Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
if (e.KeyboardDevice.Modifiers == ModifierKeys.Control) {
    switch (key) {
        case Key.A: keyStroke = 0x01; break;
        case Key.B: keyStroke = 0x02; break;
        :

Because there doesn't appear to be a keystroke for backspace, I'm capturing that in a PreviewTextInput (along with the nonCTRL keystrokes so as to avoid trying to map keys to characters manually):

private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e) {
    Char ch = e.Text.ToCharArray()[0];
    if ((ch >= (Char)0x01) && (ch <= (Char)0x7e)) {
        // Do something with character.
    }
}

However, I need to be able to capture the user pressing CTRLBACKSPACE. I'm at a loss since the PreviewKeyDown doesn't seem to know about backspace and the TextCompositionEventArgs event doesn't seem to carry the keystates (like CTRL or ALT).

What's the best way (or, indeed, any way) to intercept CTRLBACKSPACE?

+1  A: 

To detect the backspace key you need to use the Key.Back value of the Key enumeration.

For instance:

Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
if (e.KeyboardDevice.Modifiers == ModifierKeys.Control) {
    switch (key) {
        case Key.A: keyStroke = 0x01; break;
        case Key.B: keyStroke = 0x02; break;
        case Key.Back: // Do something
        break;
    }
}
Fara