使用wpf webbrowser防止快捷方式

时间:2016-11-21 15:54:59

标签: c# wpf

嘿,我的wpf webbrowser有问题。我不希望你能按下快捷键,例如" CRTL + N"例如,一个新选项卡。我已经找到了如何做到这一点,但如果我想处理更多的快捷方式,它只会阻止最后一个。我知道这将非常简单但我现在还不知道如何修复它。这是我的代码:

e.Handled = e.Key == Key.N && e.KeyboardDevice.Modifiers == ModifierKeys.Control; 
        e.Handled = e.Key == Key.O && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
        e.Handled = e.Key == Key.OemMinus && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
        e.Handled = e.Key == Key.OemPlus && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
        e.Handled = e.Key == Key.Subtract && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
        e.Handled = e.Key == Key.Add && e.KeyboardDevice.Modifiers == ModifierKeys.Control;

1 个答案:

答案 0 :(得分:1)

您需要将您的条件与OR结合在一起。

e.Handled = ((e.Key == Key.N) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
    ((e.Key == Key.O) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
    ((e.Key == Key.OemMinus) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
    ((e.Key == Key.OemPlus) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
    ((e.Key == Key.Subtract) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
    ((e.Key == Key.Add) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control));

由于修饰符CTRL似乎很常见,因此可以将其从键和键中分离出来。简化的代码就像是

e.Handled = (e.KeyboardDevice.Modifiers == ModifierKeys.Control) &&
    ((e.Key == Key.N) || (e.Key == Key.O) || (e.Key == Key.OemMinus) || ...... )

请注意,我添加了一些人会说不必要的括号,但我更喜欢它们以便于阅读。