Win32 obtains the content of the HotKey control (HKM_GETHOTKEY ),

Source: Internet
Author: User

Win32 obtains the content of the HotKey control (HKM_GETHOTKEY ),

Windows provides us with a dialog box control HotKey that is very easy to use and can be used when setting hotkeys, however, Baidu did not find out how to obtain the content in the control through messages under win32, but found that the control classes encapsulated by MFC were used for operations. Finally, the answer was found on MSDN...

The message is to be sent to the control.HKM_GETHOTKEYHeader file: Commctrl. h

// IDC_HOTKEY_SETTING: IDSendMessage (GetDlgItem (hDlg, IDC_HOTKEY_SETTING), HKM_GETHOTKEY, 0, 0) of the hotkey control );
The returned value is the obtained key combination.

Returns the virtual key code and modifier flags.LOBYTEOfLOWORDIs the virtual key code of the hot key.HIBYTEOfLOWORDIs the key modifier that specifies the keys that define a hot key combination. The modifier flags can be a combination of the following values.

In the translation, the result is that the number of low 16 bits is valid. In the lower 16 bits, the lower 8 bits store the virtual key ), the 8-bit high is the modifier)

Virtual key is the macro defined as VK_SPACE and so on. The letter is its ASC code.
Modifier:

ValueMeaning
HOTKEYF_ALT ALT key
HOTKEYF_CONTROL CONTROL key
HOTKEYF_EXT Extended key
HOTKEYF_SHIFT SHIFT key

That is, use

DWORD dwVal;dwVal = SendMessage(GetDlgItem(hDlg, IDC_HOTKEY_SETTING), HKM_GETHOTKEY, 0, 0);UINT uVk = LOBYTE(LOWORD(dwVal));UINT uMod = HIBYTE(LOWORD(dwVal));

You can directly send the HKM_SETHOTKEY message and pass the obtained value to wParam to set the content of the control box.


If you want to use the obtained value to register the hotkey, You need to convert it because HOTKEYF_ALT andRegisterHotKeyThe MOD_ALT value of is different.

#define MOD_ALT                 0x0001#define MOD_CONTROL             0x0002#define MOD_SHIFT               0x0004//-------------------------------------#define HOTKEYF_SHIFT           0x01#define HOTKEYF_CONTROL         0x02#define HOTKEYF_ALT             0x04

Conversion required

UINT HotkeyToMod (UINT fsModifiers) {if (fsModifiers & HOTKEYF_SHIFT )&&! (FsModifiers & HOTKEYF_ALT) // shift to alt {fsModifiers & = ~ HOTKEYF_SHIFT; fsModifiers | = MOD_SHIFT;} else if (! (FsModifiers & HOTKEYF_SHIFT) & (fsModifiers & HOTKEYF_ALT) // alt to shift {fsModifiers & = ~ HOTKEYF_ALT; fsModifiers | = MOD_ALT;} return fsModifiers;} UINT ModToHotkey (UINT fsModifiers) {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 to shift {fsModifiers & = ~ MOD_ALT; fsModifiers | = HOTKEYF_ALT;} return fsModifiers;} uMod = HotkeyToMod (uMod); if (! RegisterHotKey (g_hWnd, HOTKEY_SHOT, uMod, uVk) {MessageBox (hDlg, L "hotkey registration fails, conflicts may occur", L "Error", MB_ OK); break ;}View Code

The Code converted above comes from the http://blog.csdn.net/wwkaven/article/details/44119059

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.