RegisterHotKey function in VC and hotkey control (CHotKeyCtrl)

Source: Internet
Author: User

1, in fact, about the RegisterHotKey function I wrote a simple example, mainly using this function to implement a global hotkey.

Specific Links: http://blog.csdn.net/wwkaven/article/details/30719563


2, the focus of this document is to use the Hot key control set hotkey, how to correctly register with the RegisterHotKey function.


3, the idea is very clear, the user in the control input through the function GetHotKey obtained, the value obtained includes two, one is the combination of hotkey type, such as CTRL + X, Shift + x, ALT + x or CTRL + SHIFT + X, and the other is x.


4, however, we find that the value obtained directly from the GetHotKey is executed in the RegisterHotKey function, sometimes with a problem. Of course, the problem here is not the problem of compilation, but when you register a hotkey, but found that has not been in the hotkey of the message function.


5, the specific situation is:

(1) Form a hotkey like Ctrl + X everything is OK;

(2) A hotkey with a shape such as SHIFT + X and ALT + X does not respond;

... ...


6, if the attentive friend will find that when you set the hotkey in the control to SHIFT + X and directly call the function RegisterHotKey, in fact, the result of registration is ALT + X. In other words, the two values of shift and ALT are reversed.


7, then, is not a bug it. Let's not take a closer look at the definition of both:

(1) Description of the third parameter of the RegisterHotKey function:

Fsmodifiers [in] specifies keys, must be pressed in combination with the key specified by the Nvirtkey parameter in or Der to generate a wm_hotkey message. The Fsmodifiers parameter can be a combination of the following values.

Value Description
Mod_alt Either ALT key must is held down.
Mod_control Either CTRL key must is held down.
Mod_keyup Both key up events and keys down events generate a Wm_hotkey message.
Mod_shift Either SHIFT key must is held down.
Mod_win Either WINDOWS key was held down. These keys is labeled with the Microsoft Windows logo.


(2) Take a look at the description of the second parameter of GetHotKey:

Call this function to retrieve the virtual key code and modifier flags of a hot key from a hot key control.

DWORD GetHotKey () const;
void GetHotKey (
   word &wvirtualkeycode,
   Word &wmodifiers 
) const;
ParametersWvirtualkeycode Virtual Key code of the hot key. For a list of the standard virtual key codes, see Winuser.h. Wmodifiers Modifier flags indicating the keys, when used in combination and wvirtualkeycode, define a hot key combin ation. Return Value

In the first usage above, a DWORD containing the virtual key code and modifier flags. The Low-order Word is the virtual key code, and the High-order Word is the modifier flags. The HiWord and LoWord of this 32-bit value can is used as the parameters in the SetHotKey member function. Remarks

The modifier flags can be a combination of the following values: hotkeyf_alt ALT key Hotkeyf_control CTRL key hotkeyf_ext Extended key hotkeyf_shift SHIFT key

8, pay attention to my marked red place, below we look at the definition:

(1) Microsoft Sdks\windows\v7.0a\include\winuser.h

#define MOD_ALT 0x0001 #define Mod_control 0x0002 #define Mod_shift 0x0004

(2) Microsoft sdks\windows\v7.0a\include\commctrl.h

#define HOTKEYF_SHIFT 0x01 #define Hotkeyf_control 0x02 #define Hotkeyf_alt 0x04

9, the problem is clear, provide two conversion functions:

     uint  modtohotkey (uint  fsmodifiers)   &nb sp;  {         if  (  (fsmodifiers & mod_ SHIFT)  && ! (Fsmodifiers & mod_alt)  )  // shift to ALT           {             fsmodifiers &= ~ Mod_shift;              Fsmodifiers |= hotkeyf_shift ;         }          else  if   ( ! ( Fsmodifiers & mod_shift)  &&  (fsmodifiers & mod_alt)  )    alt turn shift          {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.