MK_CONTROL 与 VK_CONTROL 有什么区别,用&与用==的区别

MK_CONTROL 与 VK_CONTROL 有什么区别,各个评论的汇总:

1.

VK_CONTROL

用在键盘消息中,判断是否为 Ctrl 键的消息。
由于 WM_KEYDOWN/WM_KEYUP 一次只有一个键的消息,所以直接相等判断

    if (wParam == VK_CONTROL)

MK_CONTROL

用在鼠标消息中,判断鼠标点击时 Ctrl 的按下状态。
这时 wParam 是位标志,就需要按位判断。
比如你要响应 Ctrol+鼠标左键点击,那么再 WM_LBUTTONDOWN 消息中:
由于左键按下才会触发消息,所以不用判断 MK_LBUTTON 了,只要判断 MK_CONTROL 就行。
单是再此之前可能 Shift 是按下的、也可能右键是按下的,收到的 wParam 可能从 MK_LBUTTON | MK_CONTROL 到 MK_LBUTTON | MK_CONTROL | MK_SHIFT | MK_CONTROL 各种组合都有,相等判断不如位判断。

    if (wParam & MK_CONTROL)
    if ((wParam & MK_CONTROL) == MK_CONTROL)

要容易理解就用下面那一句。

2.

 

What is the difference between MK_CONTROL and VK_CONTROL in Win32 API?

Firstly, I checked the defined values.

MK_CONTROL : 0x0008 VK_CONTROL : 0x11

Secondly, I tested in my simple code like this.

case WM_MOUSEMOVE:
    if(wParam & MK_CONTROL)     // This works here.
        abort();
    if(wParam & VK_CONTROL)     // This doesn't work here.
        abort();    

case WM_KEYDOWN:
    if(wParam & MK_CONTROL)     // This doesn't work here.
        abort();
    if(wParam & VK_CONTROL)     // This works here.
        abort();    

case WM_LBUTTONDOWN:
    if(wParam & MK_CONTROL)     // This doens't work here.
        abort();
    if(wParam == VK_CONTROL)    // This doesn't work here.
        abort();

Does wParam value depend on the state of mouse device?

What is the difference between MK_.. and VK_.. thing?


VK_CONTROL is the virtual key number for the Ctrl key. It is only valid in keyboard messages like WM_KEYDOWN. MK_CONTROL is only valid in mouse messages, it is flag that indicates that the Ctrl key was down when the message was generated. It helps you to detect, say, the user holding down the Ctrl key while clicking. Ctrl+Click is used to expand selections. –  Hans Passant  Dec 27 '14 at 13:05
 
Thanks! Now I understood! –  Jenix  Dec 27 '14 at 13:12


用&与用==的区别

1.

用&是为了取ML_CONTROL的那一个位。仅判断那个位是1,也就是说 只要按下了CONTROL键,可能是单按了CONTROL,也可能是CTRL+ALT、CTRL+SHIFT等等。VK是虚拟键值;ML是用来相应WM_KEYDOWN等消息的

2.单按control键:wparam==VK_CONTROL
含有control键的组合键:wparam==VK_CONTROL不成立,但wparam&MK_CONTROL成立

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值