2019-01-01 20:07:29 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2019-01-01 20:07:29 +01:00
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QByteArray>
|
|
|
|
|
|
|
|
#include <qt_windows.h>
|
|
|
|
|
|
|
|
#include "globalshortcut.h"
|
|
|
|
#include "keymapper_win.h"
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
|
|
|
|
int GlobalShortcut::nativeModifiers(const Qt::KeyboardModifiers qt_mods) {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
2021-10-30 02:21:29 +02:00
|
|
|
int native_mods = 0;
|
2019-01-01 20:07:29 +01:00
|
|
|
if (qt_mods & Qt::ShiftModifier) native_mods |= MOD_SHIFT;
|
|
|
|
if (qt_mods & Qt::ControlModifier) native_mods |= MOD_CONTROL;
|
|
|
|
if (qt_mods & Qt::AltModifier) native_mods |= MOD_ALT;
|
|
|
|
if (qt_mods & Qt::MetaModifier) native_mods |= MOD_WIN;
|
|
|
|
return native_mods;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
int GlobalShortcut::nativeKeycode(const Qt::Key qt_keycode) {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
2021-10-30 02:21:29 +02:00
|
|
|
int key_code = 0;
|
2022-04-02 00:43:41 +02:00
|
|
|
if (KeyMapperWin::keymapper_win_.contains(qt_keycode)) {
|
|
|
|
key_code = KeyMapperWin::keymapper_win_.value(qt_keycode);
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
return key_code;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
int GlobalShortcut::nativeKeycode2(const Qt::Key qt_keycode) {
|
|
|
|
|
|
|
|
switch (qt_keycode) {
|
|
|
|
case Qt::Key_0:
|
|
|
|
return VK_NUMPAD0;
|
|
|
|
case Qt::Key_1:
|
|
|
|
return VK_NUMPAD1;
|
|
|
|
case Qt::Key_2:
|
|
|
|
return VK_NUMPAD2;
|
|
|
|
case Qt::Key_3:
|
|
|
|
return VK_NUMPAD3;
|
|
|
|
case Qt::Key_4:
|
|
|
|
return VK_NUMPAD4;
|
|
|
|
case Qt::Key_5:
|
|
|
|
return VK_NUMPAD5;
|
|
|
|
case Qt::Key_6:
|
|
|
|
return VK_NUMPAD6;
|
|
|
|
case Qt::Key_7:
|
|
|
|
return VK_NUMPAD7;
|
|
|
|
case Qt::Key_8:
|
|
|
|
return VK_NUMPAD8;
|
|
|
|
case Qt::Key_9:
|
|
|
|
return VK_NUMPAD9;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalShortcut::registerShortcut(const int native_key, const int native_mods) {
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
return RegisterHotKey(0, native_mods ^ native_key, native_mods, native_key);
|
2022-04-02 00:43:41 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:43:41 +02:00
|
|
|
bool GlobalShortcut::unregisterShortcut(const int native_key, const int native_mods) {
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
return UnregisterHotKey(0, native_mods ^ native_key);
|
2022-04-02 00:43:41 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 04:18:46 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
bool GlobalShortcut::nativeEventFilter(const QByteArray &eventtype, void *message, qintptr *result) {
|
|
|
|
#else
|
2019-01-01 20:07:29 +01:00
|
|
|
bool GlobalShortcut::nativeEventFilter(const QByteArray &eventtype, void *message, long *result) {
|
2020-07-18 04:18:46 +02:00
|
|
|
#endif
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
Q_UNUSED(eventtype);
|
|
|
|
Q_UNUSED(result);
|
|
|
|
|
|
|
|
MSG *msg = static_cast<MSG*>(message);
|
|
|
|
if (msg->message != WM_HOTKEY) return false;
|
|
|
|
|
|
|
|
quint32 key_code = HIWORD(msg->lParam);
|
|
|
|
quint32 modifiers = LOWORD(msg->lParam);
|
|
|
|
activateShortcut(key_code, modifiers);
|
2022-04-02 00:43:41 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|