How to check if a ctrl + enter is pressed on a control?

通过注册ComponentDispatcher.ThreadPreprocessMessage 事件实现。
// 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");

         }

      }
}


posted on 2008-08-22 16:24 cutebear 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/bear831204/archive/2008/08/22/1273188.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值