File indexing completed on 2024-04-28 05:52:14

0001 // SPDX-License-Identifier: LGPL-2.1-only
0002 
0003 #include "qglobalshortcut.h"
0004 #include <QKeySequence>
0005 #include <Windows.h>
0006 
0007 bool QGlobalShortcut::QGlobalShortcutEventFilter::nativeEventFilter(const QByteArray &event_type,
0008                                         void *message, long *result)
0009 {
0010     MSG* msg = static_cast<MSG*>(message);
0011     if (msg->message == WM_HOTKEY) {
0012         quint32 id = static_cast<quint32>(msg->wParam);
0013         activate(id);
0014         return true;
0015     }
0016     return false;
0017 }
0018 
0019 quint32 QGlobalShortcut::toNativeKeycode(Qt::Key k) {
0020     // F1 ~ F24
0021     if (k >= Qt::Key_F1 && k <= Qt::Key_F24)
0022         return VK_F1 + (k - Qt::Key_F1);
0023     // 0 ~ 9
0024     if (k >= Qt::Key_0 && k <= Qt::Key_9)
0025         return k;
0026     // A ~ Z
0027     if (k >= Qt::Key_A && k <= Qt::Key_Z)
0028         return k;
0029 
0030     switch (k) {
0031     case Qt::Key_Escape:
0032         return VK_ESCAPE;
0033     case Qt::Key_Tab:
0034     case Qt::Key_Backtab:
0035         return VK_TAB;
0036     case Qt::Key_Backspace:
0037         return VK_BACK;
0038     case Qt::Key_Return:
0039     case Qt::Key_Enter:
0040         return VK_RETURN;
0041     case Qt::Key_Insert:
0042         return VK_INSERT;
0043     case Qt::Key_Delete:
0044         return VK_DELETE;
0045     case Qt::Key_Pause:
0046         return VK_PAUSE;
0047     case Qt::Key_Print:
0048         return VK_PRINT;
0049     case Qt::Key_Clear:
0050         return VK_CLEAR;
0051     case Qt::Key_Home:
0052         return VK_HOME;
0053     case Qt::Key_End:
0054         return VK_END;
0055     case Qt::Key_Left:
0056         return VK_LEFT;
0057     case Qt::Key_Up:
0058         return VK_UP;
0059     case Qt::Key_Right:
0060         return VK_RIGHT;
0061     case Qt::Key_Down:
0062         return VK_DOWN;
0063     case Qt::Key_PageUp:
0064         return VK_PRIOR;
0065     case Qt::Key_PageDown:
0066         return VK_NEXT;
0067     case Qt::Key_Space:
0068         return VK_SPACE;
0069     case Qt::Key_Asterisk:
0070         return VK_MULTIPLY;
0071     case Qt::Key_Plus:
0072         return VK_ADD;
0073     case Qt::Key_Comma:
0074         return VK_SEPARATOR;
0075     case Qt::Key_Minus:
0076         return VK_SUBTRACT;
0077     case Qt::Key_Slash:
0078         return VK_DIVIDE;
0079     case Qt::Key_Backslash:
0080         return VK_OEM_102;        // ?
0081     case Qt::Key_MediaNext:
0082         return VK_MEDIA_NEXT_TRACK;
0083     case Qt::Key_MediaPrevious:
0084         return VK_MEDIA_PREV_TRACK;
0085     case Qt::Key_MediaPlay:
0086         return VK_MEDIA_PLAY_PAUSE;
0087     case Qt::Key_MediaStop:
0088         return VK_MEDIA_STOP;
0089     //case Qt::Key_MediaLast:     // ??
0090     //case Qt::Key_MediaRecord:   // ??
0091     case Qt::Key_VolumeDown:
0092         return VK_VOLUME_DOWN;
0093     case Qt::Key_VolumeUp:
0094         return VK_VOLUME_UP;
0095     case Qt::Key_VolumeMute:
0096         return VK_VOLUME_MUTE;
0097     default:
0098         return 0;
0099     }
0100 }
0101 
0102 quint32 QGlobalShortcut::toNativeModifiers(Qt::KeyboardModifiers m) {
0103     quint32 mods = 0;
0104     if (m & Qt::AltModifier)      mods |= MOD_ALT;
0105     if (m & Qt::ControlModifier)  mods |= MOD_CONTROL;
0106     if (m & Qt::ShiftModifier)    mods |= MOD_SHIFT;
0107     if (m & Qt::MetaModifier)     mods |= MOD_WIN;
0108     return mods;
0109 }
0110 
0111 void QGlobalShortcut::registerKey(quint32 k, quint32 m, quint32 id) {
0112     RegisterHotKey(NULL, id, m, k);
0113 }
0114 
0115 void QGlobalShortcut::unregisterKey(quint32 k, quint32 m, quint32 id) {
0116     UnregisterHotKey(NULL, id);
0117 }