// Registering against a stack event will cause memory leak, please unregister this event when you are done with it. |
ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage; |
// WM_KEYDOWN 是按下一个键是产生的消息。
const int WM_KEYDOWN = 0x100;
// WM_SYSKEYDOWN 是按下Alt键同时再按下别的键时产生的消息。
const int WM_SYSKEYDOWN = 0x0104;
void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled)
{
if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
{
// Examine if the Control and Enter keys are pressed, we also need to make sure that the
// currently keyborad focused element is TextBox instance itself.
// "|" 二进制或运算,有一个为1则结果为1。
// "&" 二进制与运算,两个都为1则结果为1. Keys转换为int的值就是Keys这个枚举类中定义的该枚举项的值。
Keys keyData = ((Keys)((int)(msg.wParam))) | System.Windows.Forms.Control.ModifierKeys;
if (((keyData & Keys.Control) == Keys.Control) &&
((keyData & Keys.Enter) == Keys.Enter) &&
Keyboard.FocusedElement == this.txtBox)
{
System.Windows.MessageBox.Show("Ctrl+Enter is pressed");
}
}
}