2016-11-21 71 views
1

嘿,我的wpf webbrowser有問題。例如,我不希望您按新快捷鍵「CRTL + N」。我已經發現如何去做,但如果我想處理更多的快捷方式,它只會阻止最後一個。我知道這將是非常簡單的,但目前我不知道如何修復它。這裏是我的代碼:使用wpf webbrowser防止快捷方式

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

你需要或你在一起的條件。

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) || ......) 

注意,我添加了括號,有些人會說是鑰匙被分離出來不必要的,但我更喜歡他們的可讀性。

+1

謝謝!有效 :) – nicoh