QT Global Hotkey (with Nativekeycode encapsulation API, no cross-platform)

Source: Internet
Author: User
Tags modifiers

Looking for a long time on the internet, everyone mentions a QT global Gellicoux (qxtglobalshortcut), which supports cross-platform. In this article, I will only show the settings for the global hotkey under the Windows platform.
The method provided here is to complete the API encapsulation of Windows in Myglobalshortcut and use it in main.cpp.
Directly on the code:
Mywineventfilter class:

Class Mywineventfilter:p ublic qabstractnativeeventfilter
{
Public

MyWinEventFilter(MyGlobalShortCut *shortcut);~MyWinEventFilter();virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long*);

Private

MyGlobalShortCut *m_shortcut;

};

Mywineventfilter::mywineventfilter (myglobalshortcut* shortcut): m_shortcut (shortcut)
{}
Mywineventfilter::~mywineventfilter ()
{}
BOOL Mywineventfilter::nativeeventfilter (const Qbytearray &eventtype, void message, long)
{

MSG *msg = static_cast(message);//static_cast<MSG*>(message)(把这段替换)if (msg->message == WM_HOTKEY){    const quint32 keycode = HIWORD(msg->lParam);    const quint32 modifiers = LOWORD(msg->lParam);    bool res = m_shortcut->shortcuts.value(qMakePair(keycode, modifiers));    if (res)    {        m_shortcut->activateShortcut();        return true;    }}return false;

}
Myglobalshortcut class:
Class Myglobalshortcut:p ublic qobject
{
Q_object
Public

MyGlobalShortCut(QKeySequence key);~MyGlobalShortCut();void activateShortcut();bool registerHotKey();bool unregisterHotKey();QHash<QPair<quint32, quint32>, MyGlobalShortCut*> shortcuts;

Private

QApplication *m_app;MyWinEventFilter *m_filter;QKeySequence m_key;Qt::Key key;Qt::KeyboardModifiers mods;static quint32 nativeKeycode(Qt::Key keycode);static quint32 nativeModifiers(Qt::KeyboardModifiers modifiers);

Signals:

void activated();

};

Myglobalshortcut::myglobalshortcut (qkeysequence key)
{

m_key = QKeySequence(key);m_filter = new MyWinEventFilter(this);m_app->installNativeEventFilter(m_filter);registerHotKey();

}

Myglobalshortcut::~myglobalshortcut ()
{

unregisterHotKey();

}

void Myglobalshortcut::activateshortcut ()
{

emit activated();

}

BOOL Myglobalshortcut::registerhotkey ()
{

Qt::KeyboardModifiers allMods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier;key = (m_key.isEmpty() ? Qt::Key(0) : Qt::Key((m_key[0] ^ allMods) & m_key[0]));mods = m_key.isEmpty() ? Qt::KeyboardModifiers(0) : Qt::KeyboardModifiers(m_key[0] & allMods);const quint32 nativeKey = nativeKeycode(key);const quint32 nativeMods = nativeModifiers(mods);shortcuts.insert(qMakePair(nativeKey, nativeMods), this);return RegisterHotKey(0, nativeMods ^ nativeKey, nativeMods, nativeKey);

}

BOOL Myglobalshortcut::unregisterhotkey ()
{

return UnregisterHotKey(0, (quint32)nativeModifiers(mods) ^ (quint32)nativeKeycode(key));

}

Quint32 Myglobalshortcut::nativekeycode (Qt::key Key)
{

Switch (key) {case Qt::key_escape:return vk_escape;case qt::key_tab:case qt::key_backtab:return vk_tab;case Qt::Key _backspace:return vk_back;case qt::key_return:case qt::key_enter:return vk_return;case qt::key_insert:return V K_insert;case qt::key_delete:return vk_delete;case qt::key_pause:return vk_pause;case qt::key_print:return VK_ Print;case qt::key_clear:return vk_clear;case qt::key_home:return vk_home;case qt::key_end:return VK_END;case     Qt::key_left:return vk_left;case qt::key_up:return vk_up;case qt::key_right:return VK_RIGHT;case Qt::Key_Down: return vk_down;case qt::key_pageup:return vk_prior;case qt::key_pagedown:return vk_next;case qt::key_f1:re Turn vk_f1;case qt::key_f2:return vk_f2;case qt::key_f3:return vk_f3;case qt::key_f4:return VK_F4;case Qt::Key _f5:return vk_f5;case qt::key_f6:return vk_f6;case qt::key_f7:return vk_f7;case qt::key_f8:return VK_F8;ca Se qt::key_f9:return Vk_f9;case qt::key_f10:return vk_f10;case qt::key_f11:return vk_f11;case qt::key_f12:return VK_F12;case Qt::K  Ey_f13:return vk_f13;case qt::key_f14:return vk_f14;case qt::key_f15:return vk_f15;case Qt::key_f16:return Vk_f16;case qt::key_f17:return vk_f17;case qt::key_f18:return vk_f18;case qt::key_f19:return VK_F19;case Qt:: Key_f20:return vk_f20;case qt::key_f21:return vk_f21;case qt::key_f22:return vk_f22;case Qt::key_f23:retur n vk_f23;case qt::key_f24:return vk_f24;case qt::key_space:return vk_space;case qt::key_asterisk:return VK_MUL Tiply;case qt::key_plus:return vk_add;case qt::key_comma:return vk_separator;case qt::key_minus:return VK_SUBT    Ract;case Qt::key_slash:return vk_divide; Numberscase qt::key_0:case qt::key_1:case qt::key_2:case qt::key_3:case qt::key_4:case Qt::Key_5:case Qt::Key_6:    Case Qt::key_7:case qt::key_8:case Qt::key_9:return Key; Letterscase qt::key_a:case Qt::key_b:Case Qt::key_c:case qt::key_d:case qt::key_e:case qt::key_f:case qt::key_g:case qt::key_h:case Qt::Key_I:case Qt::Key_ J:case qt::key_k:case qt::key_l:case qt::key_m:case qt::key_n:case qt::key_o:case Qt::Key_P:case Qt::Key_Q:case Qt:: Key_r:case qt::key_s:case qt::key_t:case qt::key_u:case qt::key_v:case qt::key_w:case Qt::Key_X:case Qt::Key_Y:case Qt :: Key_z:return key;default:return 0;}

}

Quint32 myglobalshortcut::nativemodifiers (qt::keyboardmodifiers modifiers)
{

// MOD_ALT, MOD_CONTROL, (MOD_KEYUP), MOD_SHIFT, MOD_WINquint32 native = 0;if (modifiers & Qt::ShiftModifier)    native |= MOD_SHIFT;if (modifiers & Qt::ControlModifier)    native |= MOD_CONTROL;if (modifiers & Qt::AltModifier)    native |= MOD_ALT;if (modifiers & Qt::MetaModifier)    native |= MOD_WIN;return native;

}
//////////////////////////////////////////////////////////////////////////////////////////////
Above is the encapsulation of the class, the following is used:
In the main () function, add:

Qkeysequence keysequence=qkeysequence (tr ("Alt+a"));//Set shortcut keys Alt+a
Myglobalshortcut *shortcut=new myglobalshortcut (keysequence);
Connect (shortcut,signal (activated ()), This,slot (onActivated ()));

OnActivated ()
{
What to do after the global hotkey is activated
}
This is only for Windows system implementation, because the work now only involves windows, the other system is similar, mainly to implement the Nativeeventfilter () method.
/////////////////////////////////////////////////////////////////////////////////////////////
(End of full text)

http://blog.csdn.net/u011915578/article/details/46491055

QT Global Hotkey (with Nativekeycode encapsulation API, no cross-platform)

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.