如何禁用某些组合键(例如Shift +“某个数字键”)

时间:2015-04-29 02:00:13

标签: c# wpf

我创建了一个电话键盘,它包含0-9加哈希键(#)和星号键(*)。我能够禁用除这些数字键以外的其他键,以便当用户在提供的文本框中键入时,它只允许数字键。由于#是Shift + 3和*的组合是Shift + 8的组合,我的问题是除了那两个之外我无法禁用其他组合键。那么,有没有办法解决这个问题才能只允许Shift + 3和Shift + 8?

private void NumDisplayBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    //Determine whether the keystroke is a number from the top of the keyboard
    if (e.Key < Key.D0 || e.Key > Key.D9)
        //Determine whether the keystroke is a number from the keypad
        if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
            e.Handled = true;

    if (e.Key == Key.Space) //Disable the space key
        e.Handled = true;

    //Enable the following keys
    if (e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Left || e.Key == Key.Right)
        e.Handled = false;

    //Determine whether the keystroke is # or *    
    if ((Keyboard.Modifiers == ModifierKeys.Control) && (e.Key == Key.D3))
        e.Handled = false;
}

0 个答案:

没有答案