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;
答案 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) || ...... )
请注意,我添加了一些人会说不必要的括号,但我更喜欢它们以便于阅读。