WPF 中, 有時候要用到鍵盤組合事件,比如說Ctrl+S做保存。。。。
public Window1() {
InitializeComponent();
// 下面這句代碼 你可以放在你想觸發這個事件的事件裡。。比如下面的那個 Window_KeyDown
AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
}
private void HandleKeyDownEvent(object sender, KeyEventArgs e) {
if (e.Key == Key.S && (Keyboard.Modifiers & (ModifierKeys.Control)) == (ModifIErKeys.Control)) {
MessageBox.Show("CTRL + s trapped");
}
}
private void Window_KeyDown(object sender, KeyEventArgs e) {
AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
}
}
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.Net/loveheye/archive/2009/12/17/5026616.ASPx