QT Global Hotkey (Windows Post) (register with RegisterHotKey and handles)

Source: Internet
Author: User
Tags function prototype

Reprint: http://www.cuteqt.com/blog/?p=2088

Qt for the bottom of the system, has not been very good support, such as serial port and port communication, as well as we often use the global hotkey, and so on. Since QT may, for some reason, not support these, we can only write our own code to invoke the system-related APIs.

Note that this is a Windows article, which means that the code only supports Windows systems. At the same time, it also indicates that there must be other platforms to write. But wait for O (∩_∩) o~

The API to invoke windows in Qt is simple, just add # include <windows.h> in the header file you need to use. Note, however, that QT supports only Win32 APIs, such as MFC, which are not supported.

The best way to use a global hotkey in Qt is to overload the Wineventfilter function in Qapplication. This function is used to respond to Windows system information. Its function prototype is virtual bool Wineventfilter (MSG *msg, Long *result). When the message in the MSG pointer (an element in the MSG structure) is a wm_hotkey type, the user fires the hotkey. The whole process is similar to the message response mechanism in VC, except for QT.

And if we want to add our own global hotkeys, we just need to use the RegisterHotKey function. Its function prototypes are:

BOOL RegisterHotKey (HWND hwnd, int ID, uint fsmodifiers, uint VK );

The HWND type is a window handle type in Windows, and in Qt, Qwidget and its subclasses can be obtained using the Winid () function.

The second parameter, which is an atomic operation type, is obtained using the Atom Globaladdatom (LPCTSTR lpstring ) function. This is also a win32api, according to a string parameter can get a unique value. After use, you must use the Globaldeleteatom function to remove the entire atom. Its function prototype is Atom Globaldeleteatom (Atom natom ).

The following two parameters, one is the modifier key, and the other is the normal button. For example, suppose we want to register CTRL+F4 this hotkey combination, then Fsmodifiers is Mod_control, and VK is Vk_f4.

Returns true if the registration succeeds, otherwise, returns false.

Note that you may not be able to register this hotkey any more after you use the Unregisterhotkey function to log out. Unless restarted. Its function prototype is: BOOL Unregisterhotkey (hwnd hwnd, int ID ).

OK, the hotkey registration is complete. When the hotkey is pressed, we receive a msg in the Wineventfilter function in Qapplication. This msg, as mentioned earlier, we need to know if one of its elements is a wm_hotkey. If it is, it proves that we have received the hotkey information of the Windows system. In the MSG structure, we may also need to understand two elements, namely wparam and lparam. WPARAM is the ID used to register the hotkey, which is atom (atom). And lparam is our hotkey. It is actually a 32-bit type, the front 16 bits represent the normal button, and the last 16 bits represent the modifier keys.

At this point, QT uses the global hotkey on Windows to complete. If you are interested in a friend, you can also try it yourself, modify or directly write a copy of your code.

http://blog.csdn.net/calm_agan/article/details/6340077

QT Global Hotkey (Windows Post) (register with RegisterHotKey and handles)

Related Article

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.