开发者

How can I capture CTRL-BACKSPACE in WPF/C#?

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)) {
        // 开发者_JAVA百科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?


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜