All posts on this site are reserved by this site and the original author. This article can only be reproduced if the copyright information, original article links, and original article authors are retained. Do not delete or modify the original article content for reprinting, it is not intended for commercial purposes. Thank you for your cooperation.
Link to the original article: QT global hotkey (Windows)
Qt has never provided good support for the underlying system, such as serial port communication and global hotkeys that we often use. Since QT may not be supported for some reason, we can only write code and call system-related APIs.
Note: In Windows, the Code only supports windows. At the same time, it also indicates that there must be other platforms to write. But wait for O (∩ _ ∩) O ~
To call the Windows API in QT, you only need to add # include <windows. h> to the header file you need. However, it should be noted that QT only supports Win32 APIs, and does not support such APIs as MFC.
To use global hotkeys in QT, the best way 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 (an element in the MSG structure) in the MSG pointer is of the wm_hotkey type, the hotkey is triggered by the user. The entire process is similar to the Message response mechanism in VC, but it is changed to QT.
If you want to add your own Global hotkeys, you only need to use the registerhotkey function. Its function prototype is:
Bool registerhotkey (hwndHwnd, IntID, UintFsmodifiers, UintVK);
The hwnd type is the window handle type in windows. In QT, qwidget and its subclass can be obtained using the winid () function.
The second parameter is an atomic operation type, which uses atom globaladdatom (lpctstrLpstring) Function. This is also a WIN32API. You can obtain a unique value based on a string parameter. After use, you must use the globaldeleteatom function to delete the entire atom. The function prototype is atom.
Globaldeleteatom (AtomNatom).
The next two parameters are modifier keys and common keys. For example, if we want to register the key combination Ctrl + F4, fsmodifiers is mod_control, and VK is vk_f4.
If the registration is successful, true is returned. Otherwise, false is returned.
Note: after use, you must use the unregisterhotkey function to log out. Otherwise, you may no longer be able to register this hot key! Unless it is restarted. The function prototype is bool unregisterhotkey (hwndHwnd, IntID).
OK. The hotkey registration is complete. After pressing the hot key, we will receive a MSG in the wineventfilter function of qapplication. This msg, as mentioned earlier, needs to know whether one of its elements is wm_hotkey. If yes, it indicates that we have received the hotkey information for Windows. In the MSG structure, we may also need to understand two elements: wparam and lparam. Wparam is the ID used to register the hot key, that is, Atom ). Lparam is our hotkey. It is actually a 32-bit type. The first 16 digits represent normal keys, and the last 16 digits represent modifier keys.
So far, QT uses the Global hotkey in windows. If you are interested, you can try it yourself and modify or directly write a copy of the code.
If any error occurs, please correct it.
I put the test code I wrote on the cuteqt Forum. If you are interested, you can download it.
Http://cuteqt.org/bbs/viewthread.php? Tid = 1641 & extra = Page % 3d1