mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
547cf7a7cc | ||
|
|
f00ae2a071 | ||
|
|
9a90aff273 | ||
|
|
080ddd479a | ||
|
|
2d21833742 | ||
|
|
fc259100b2 | ||
|
|
670705e4e0 | ||
|
|
f549a7763f | ||
|
|
d46af31902 | ||
|
|
f4665e20ad | ||
|
|
9a04e8f539 | ||
|
|
974af9253c | ||
|
|
9a812a3eb4 | ||
|
|
0943bb0494 | ||
|
|
b910580fce | ||
|
|
c9cfd8fb6f | ||
|
|
7d214ac41e | ||
|
|
83910c8eca | ||
|
|
91f3542b5d | ||
|
|
1833a053f6 | ||
|
|
f288e85ef1 | ||
|
|
2578b69dc5 | ||
|
|
a0678c4cfc | ||
|
|
cdb4d37453 | ||
|
|
9eace8fbbd | ||
|
|
6baf644b40 | ||
|
|
3fa176a380 | ||
|
|
7c4ef6a03a | ||
|
|
15e34a7dbf | ||
|
|
b8f32c3381 | ||
|
|
10d1e44b21 | ||
|
|
040cf5ec0c | ||
|
|
4446fe0a42 | ||
|
|
53bd8cdd14 |
@@ -1,6 +1,8 @@
|
|||||||
#include "hottestwidget.h"
|
#include "hottestwidget.h"
|
||||||
#include "ui_hottestwidget.h"
|
#include "ui_hottestwidget.h"
|
||||||
|
|
||||||
|
//#define TEST_MAPPING
|
||||||
|
|
||||||
HotTestWidget::HotTestWidget(QWidget *parent) :
|
HotTestWidget::HotTestWidget(QWidget *parent) :
|
||||||
QWidget(parent),
|
QWidget(parent),
|
||||||
ui(new Ui::HotTestWidget),
|
ui(new Ui::HotTestWidget),
|
||||||
@@ -18,6 +20,11 @@ HotTestWidget::HotTestWidget(QWidget *parent) :
|
|||||||
this->thread4->start();
|
this->thread4->start();
|
||||||
this->thread5->start();
|
this->thread5->start();
|
||||||
|
|
||||||
|
#ifdef TEST_MAPPING
|
||||||
|
//shortcut mapping override
|
||||||
|
QHotkey::addGlobalMapping(QKeySequence("X"), QHotkey::NativeShortcut());// add invalid mapping to test if the overwrite works for all platforms
|
||||||
|
#endif
|
||||||
|
|
||||||
//1
|
//1
|
||||||
connect(this->ui->hotkeyCheckbox_1, &QCheckBox::toggled,
|
connect(this->ui->hotkeyCheckbox_1, &QCheckBox::toggled,
|
||||||
this->hotkey_1, &QHotkey::setRegistered);
|
this->hotkey_1, &QHotkey::setRegistered);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QTabWidget" name="tabWidget">
|
<widget class="QTabWidget" name="tabWidget">
|
||||||
<property name="currentIndex">
|
<property name="currentIndex">
|
||||||
<number>3</number>
|
<number>0</number>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="playTab">
|
<widget class="QWidget" name="playTab">
|
||||||
<attribute name="title">
|
<attribute name="title">
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
#include "hottestwidget.h"
|
#include "hottestwidget.h"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
|
||||||
|
//#define START_BACKGROUND
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
HotTestWidget w;
|
HotTestWidget w;
|
||||||
|
|
||||||
|
#ifdef START_BACKGROUND
|
||||||
|
auto startKey = new QHotkey(QKeySequence(Qt::MetaModifier | Qt::ControlModifier | Qt::Key_S), true, &w);
|
||||||
|
QObject::connect(startKey, &QHotkey::activated,
|
||||||
|
&w, &QWidget::show);
|
||||||
|
#else
|
||||||
w.show();
|
w.show();
|
||||||
|
#endif
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -1,7 +1,8 @@
|
|||||||
TEMPLATE = subdirs
|
TEMPLATE = subdirs
|
||||||
|
|
||||||
SUBDIRS += \
|
SUBDIRS += \
|
||||||
HotkeyTest
|
HotkeyTest \
|
||||||
|
QHotkey
|
||||||
|
|
||||||
DISTFILES += README.md \
|
DISTFILES += README.md \
|
||||||
LICENSE \
|
LICENSE \
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
TEMPLATE = lib
|
||||||
|
win32: CONFIG += dll
|
||||||
|
|
||||||
|
TARGET = QHotkey
|
||||||
|
VERSION = 1.2.1
|
||||||
|
|
||||||
|
include(../qhotkey.pri)
|
||||||
|
|
||||||
|
DEFINES += QHOTKEY_LIB QHOTKEY_LIB_BUILD
|
||||||
|
|
||||||
|
# use INSTALL_ROOT to modify the install location
|
||||||
|
headers.files = $$PUBLIC_HEADERS
|
||||||
|
headers.path = $$[QT_INSTALL_HEADERS]
|
||||||
|
target.path = $$[QT_INSTALL_LIBS]
|
||||||
|
INSTALLS += target headers
|
||||||
|
|
||||||
+97
-70
@@ -8,83 +8,91 @@
|
|||||||
|
|
||||||
Q_LOGGING_CATEGORY(logQHotkey, "QHotkey")
|
Q_LOGGING_CATEGORY(logQHotkey, "QHotkey")
|
||||||
|
|
||||||
|
void QHotkey::addGlobalMapping(const QKeySequence &shortcut, const QHotkey::NativeShortcut &nativeShortcut)
|
||||||
|
{
|
||||||
|
QMetaObject::invokeMethod(QHotkeyPrivate::instance(), "addMappingInvoked", Qt::QueuedConnection,
|
||||||
|
Q_ARG(Qt::Key, Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask)),
|
||||||
|
Q_ARG(Qt::KeyboardModifiers, Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask)),
|
||||||
|
Q_ARG(QHotkey::NativeShortcut, nativeShortcut));
|
||||||
|
}
|
||||||
|
|
||||||
QHotkey::QHotkey(QObject *parent) :
|
QHotkey::QHotkey(QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
key(Qt::Key_unknown),
|
_keyCode(Qt::Key_unknown),
|
||||||
mods(Qt::NoModifier),
|
_modifiers(Qt::NoModifier),
|
||||||
nativeShortcut(),
|
_nativeShortcut(),
|
||||||
registered(false)
|
_registered(false)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
QHotkey::QHotkey(const QKeySequence &sequence, bool autoRegister, QObject *parent) :
|
QHotkey::QHotkey(const QKeySequence &sequence, bool autoRegister, QObject *parent) :
|
||||||
QHotkey(parent)
|
QHotkey(parent)
|
||||||
{
|
{
|
||||||
this->setShortcut(sequence, autoRegister);
|
setShortcut(sequence, autoRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkey::QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
|
QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
|
||||||
QHotkey(parent)
|
QHotkey(parent)
|
||||||
{
|
{
|
||||||
this->setShortcut(key, modifiers, autoRegister);
|
setShortcut(keyCode, modifiers, autoRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkey::QHotkey(const QHotkey::NativeShortcut &shortcut, bool autoRegister, QObject *parent) :
|
QHotkey::QHotkey(const QHotkey::NativeShortcut &shortcut, bool autoRegister, QObject *parent) :
|
||||||
QHotkey(parent)
|
QHotkey(parent)
|
||||||
{
|
{
|
||||||
this->setNativeShortcut(shortcut, autoRegister);
|
setNativeShortcut(shortcut, autoRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkey::~QHotkey()
|
QHotkey::~QHotkey()
|
||||||
{
|
{
|
||||||
if(this->registered)
|
if(_registered)
|
||||||
QHotkeyPrivate::instance()->removeShortcut(this);
|
QHotkeyPrivate::instance()->removeShortcut(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
QKeySequence QHotkey::shortcut() const
|
QKeySequence QHotkey::shortcut() const
|
||||||
{
|
{
|
||||||
if(this->key == Qt::Key_unknown)
|
if(_keyCode == Qt::Key_unknown)
|
||||||
return QKeySequence();
|
return QKeySequence();
|
||||||
else
|
else
|
||||||
return QKeySequence(this->key | this->mods);
|
return QKeySequence(_keyCode | _modifiers);
|
||||||
}
|
}
|
||||||
|
|
||||||
Qt::Key QHotkey::keyCode() const
|
Qt::Key QHotkey::keyCode() const
|
||||||
{
|
{
|
||||||
return this->key;
|
return _keyCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
Qt::KeyboardModifiers QHotkey::modifiers() const
|
Qt::KeyboardModifiers QHotkey::modifiers() const
|
||||||
{
|
{
|
||||||
return this->mods;
|
return _modifiers;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkey::NativeShortcut QHotkey::currentNativeShortcut() const
|
QHotkey::NativeShortcut QHotkey::currentNativeShortcut() const
|
||||||
{
|
{
|
||||||
return this->nativeShortcut;
|
return _nativeShortcut;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::isRegistered() const
|
bool QHotkey::isRegistered() const
|
||||||
{
|
{
|
||||||
return this->registered;
|
return _registered;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
|
bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
|
||||||
{
|
{
|
||||||
if(shortcut.isEmpty()) {
|
if(shortcut.isEmpty()) {
|
||||||
return this->resetShortcut();
|
return resetShortcut();
|
||||||
} else if(shortcut.count() > 1) {
|
} else if(shortcut.count() > 1) {
|
||||||
qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
|
qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
|
||||||
"Only the first shortcut will be used!");
|
"Only the first shortcut will be used!");
|
||||||
}
|
}
|
||||||
|
|
||||||
return this->setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
|
return setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
|
||||||
Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask),
|
Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask),
|
||||||
autoRegister);
|
autoRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister)
|
bool QHotkey::setShortcut(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister)
|
||||||
{
|
{
|
||||||
if(this->registered) {
|
if(_registered) {
|
||||||
if(autoRegister) {
|
if(autoRegister) {
|
||||||
if(!QHotkeyPrivate::instance()->removeShortcut(this))
|
if(!QHotkeyPrivate::instance()->removeShortcut(this))
|
||||||
return false;
|
return false;
|
||||||
@@ -92,46 +100,46 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(key == Qt::Key_unknown) {
|
if(keyCode == Qt::Key_unknown) {
|
||||||
this->key = Qt::Key_unknown;
|
_keyCode = Qt::Key_unknown;
|
||||||
this->mods = Qt::NoModifier;
|
_modifiers = Qt::NoModifier;
|
||||||
this->nativeShortcut = NativeShortcut();
|
_nativeShortcut = NativeShortcut();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->key = key;
|
_keyCode = keyCode;
|
||||||
this->mods = modifiers;
|
_modifiers = modifiers;
|
||||||
this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers);
|
_nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(keyCode, modifiers);
|
||||||
if(this->nativeShortcut.isValid()) {
|
if(_nativeShortcut.isValid()) {
|
||||||
if(autoRegister)
|
if(autoRegister)
|
||||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << key << "Modifiers:" << modifiers;
|
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers;
|
||||||
this->key = Qt::Key_unknown;
|
_keyCode = Qt::Key_unknown;
|
||||||
this->mods = Qt::NoModifier;
|
_modifiers = Qt::NoModifier;
|
||||||
this->nativeShortcut = NativeShortcut();
|
_nativeShortcut = NativeShortcut();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::resetShortcut()
|
bool QHotkey::resetShortcut()
|
||||||
{
|
{
|
||||||
if(this->registered &&
|
if(_registered &&
|
||||||
!QHotkeyPrivate::instance()->removeShortcut(this)) {
|
!QHotkeyPrivate::instance()->removeShortcut(this)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->key = Qt::Key_unknown;
|
_keyCode = Qt::Key_unknown;
|
||||||
this->mods = Qt::NoModifier;
|
_modifiers = Qt::NoModifier;
|
||||||
this->nativeShortcut = NativeShortcut();
|
_nativeShortcut = NativeShortcut();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool autoRegister)
|
bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool autoRegister)
|
||||||
{
|
{
|
||||||
if(this->registered) {
|
if(_registered) {
|
||||||
if(autoRegister) {
|
if(autoRegister) {
|
||||||
if(!QHotkeyPrivate::instance()->removeShortcut(this))
|
if(!QHotkeyPrivate::instance()->removeShortcut(this))
|
||||||
return false;
|
return false;
|
||||||
@@ -140,27 +148,27 @@ bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool aut
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(nativeShortcut.isValid()) {
|
if(nativeShortcut.isValid()) {
|
||||||
this->key = Qt::Key_unknown;
|
_keyCode = Qt::Key_unknown;
|
||||||
this->mods = Qt::NoModifier;
|
_modifiers = Qt::NoModifier;
|
||||||
this->nativeShortcut = nativeShortcut;
|
_nativeShortcut = nativeShortcut;
|
||||||
if(autoRegister)
|
if(autoRegister)
|
||||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
this->key = Qt::Key_unknown;
|
_keyCode = Qt::Key_unknown;
|
||||||
this->mods = Qt::NoModifier;
|
_modifiers = Qt::NoModifier;
|
||||||
this->nativeShortcut = NativeShortcut();
|
_nativeShortcut = NativeShortcut();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::setRegistered(bool registered)
|
bool QHotkey::setRegistered(bool registered)
|
||||||
{
|
{
|
||||||
if(this->registered && !registered)
|
if(_registered && !registered)
|
||||||
return QHotkeyPrivate::instance()->removeShortcut(this);
|
return QHotkeyPrivate::instance()->removeShortcut(this);
|
||||||
else if(!this->registered && registered) {
|
else if(!_registered && registered) {
|
||||||
if(!this->nativeShortcut.isValid())
|
if(!_nativeShortcut.isValid())
|
||||||
return false;
|
return false;
|
||||||
else
|
else
|
||||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||||
@@ -181,15 +189,15 @@ QHotkeyPrivate::QHotkeyPrivate() :
|
|||||||
|
|
||||||
QHotkeyPrivate::~QHotkeyPrivate()
|
QHotkeyPrivate::~QHotkeyPrivate()
|
||||||
{
|
{
|
||||||
if(!this->shortcuts.isEmpty())
|
if(!shortcuts.isEmpty())
|
||||||
qCWarning(logQHotkey, "QHotkeyPrivate destroyed with registered shortcuts!");
|
qCWarning(logQHotkey) << "QHotkeyPrivate destroyed with registered shortcuts!";
|
||||||
if(qApp && qApp->eventDispatcher())
|
if(qApp && qApp->eventDispatcher())
|
||||||
qApp->eventDispatcher()->removeNativeEventFilter(this);
|
qApp->eventDispatcher()->removeNativeEventFilter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
|
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
|
||||||
{
|
{
|
||||||
Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ?
|
Qt::ConnectionType conType = (QThread::currentThread() == thread() ?
|
||||||
Qt::DirectConnection :
|
Qt::DirectConnection :
|
||||||
Qt::BlockingQueuedConnection);
|
Qt::BlockingQueuedConnection);
|
||||||
QHotkey::NativeShortcut res;
|
QHotkey::NativeShortcut res;
|
||||||
@@ -204,10 +212,10 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb
|
|||||||
|
|
||||||
bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
|
bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
|
||||||
{
|
{
|
||||||
if(hotkey->registered)
|
if(hotkey->_registered)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ?
|
Qt::ConnectionType conType = (QThread::currentThread() == thread() ?
|
||||||
Qt::DirectConnection :
|
Qt::DirectConnection :
|
||||||
Qt::BlockingQueuedConnection);
|
Qt::BlockingQueuedConnection);
|
||||||
bool res = false;
|
bool res = false;
|
||||||
@@ -224,10 +232,10 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
|
|||||||
|
|
||||||
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
||||||
{
|
{
|
||||||
if(!hotkey->registered)
|
if(!hotkey->_registered)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ?
|
Qt::ConnectionType conType = (QThread::currentThread() == thread() ?
|
||||||
Qt::DirectConnection :
|
Qt::DirectConnection :
|
||||||
Qt::BlockingQueuedConnection);
|
Qt::BlockingQueuedConnection);
|
||||||
bool res = false;
|
bool res = false;
|
||||||
@@ -245,38 +253,57 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
|||||||
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
|
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
|
||||||
{
|
{
|
||||||
QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated);
|
QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated);
|
||||||
for(QHotkey *hkey : this->shortcuts.values(shortcut))
|
for(QHotkey *hkey : shortcuts.values(shortcut))
|
||||||
signal.invoke(hkey, Qt::QueuedConnection);
|
signal.invoke(hkey, Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut)
|
||||||
|
{
|
||||||
|
mapping.insert({keycode, modifiers}, nativeShortcut);
|
||||||
|
}
|
||||||
|
|
||||||
bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey)
|
bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey)
|
||||||
{
|
{
|
||||||
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
|
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;
|
||||||
|
|
||||||
if(!this->shortcuts.contains(shortcut)) {
|
if(!shortcuts.contains(shortcut)) {
|
||||||
if(!this->registerShortcut(shortcut))
|
if(!registerShortcut(shortcut))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->shortcuts.insert(shortcut, hotkey);
|
shortcuts.insert(shortcut, hotkey);
|
||||||
hotkey->registered = true;
|
hotkey->_registered = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
|
bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
|
||||||
{
|
{
|
||||||
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
|
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;
|
||||||
|
|
||||||
if(this->shortcuts.remove(shortcut, hotkey) == 0)
|
if(shortcuts.remove(shortcut, hotkey) == 0)
|
||||||
return false;
|
return false;
|
||||||
hotkey->registered = false;
|
hotkey->_registered = false;
|
||||||
emit hotkey->registeredChanged(true);
|
emit hotkey->registeredChanged(true);
|
||||||
if(this->shortcuts.count(shortcut) == 0)
|
if(shortcuts.count(shortcut) == 0)
|
||||||
return this->unregisterShortcut(shortcut);
|
return unregisterShortcut(shortcut);
|
||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
|
||||||
|
{
|
||||||
|
if(mapping.contains({keycode, modifiers}))
|
||||||
|
return mapping.value({keycode, modifiers});
|
||||||
|
|
||||||
|
bool ok1, ok2 = false;
|
||||||
|
auto k = nativeKeycode(keycode, ok1);
|
||||||
|
auto m = nativeModifiers(modifiers, ok2);
|
||||||
|
if(ok1 && ok2)
|
||||||
|
return {k, m};
|
||||||
|
else
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
QHotkey::NativeShortcut::NativeShortcut() :
|
QHotkey::NativeShortcut::NativeShortcut() :
|
||||||
@@ -293,21 +320,21 @@ QHotkey::NativeShortcut::NativeShortcut(quint32 key, quint32 modifier) :
|
|||||||
|
|
||||||
bool QHotkey::NativeShortcut::isValid() const
|
bool QHotkey::NativeShortcut::isValid() const
|
||||||
{
|
{
|
||||||
return this->valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::NativeShortcut::operator ==(const QHotkey::NativeShortcut &other) const
|
bool QHotkey::NativeShortcut::operator ==(const QHotkey::NativeShortcut &other) const
|
||||||
{
|
{
|
||||||
return (this->key == other.key) &&
|
return (key == other.key) &&
|
||||||
(this->modifier == other.modifier) &&
|
(modifier == other.modifier) &&
|
||||||
this->valid == other.valid;
|
valid == other.valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::NativeShortcut::operator !=(const QHotkey::NativeShortcut &other) const
|
bool QHotkey::NativeShortcut::operator !=(const QHotkey::NativeShortcut &other) const
|
||||||
{
|
{
|
||||||
return (this->key != other.key) ||
|
return (key != other.key) ||
|
||||||
(this->modifier != other.modifier) ||
|
(modifier != other.modifier) ||
|
||||||
this->valid != other.valid;
|
valid != other.valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint qHash(const QHotkey::NativeShortcut &key)
|
uint qHash(const QHotkey::NativeShortcut &key)
|
||||||
|
|||||||
+25
-11
@@ -6,8 +6,18 @@
|
|||||||
#include <QPair>
|
#include <QPair>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
|
|
||||||
|
#ifdef QHOTKEY_LIB
|
||||||
|
#ifdef QHOTKEY_LIB_BUILD
|
||||||
|
#define QHOTKEY_SHARED_EXPORT Q_DECL_EXPORT
|
||||||
|
#else
|
||||||
|
#define QHOTKEY_SHARED_EXPORT Q_DECL_IMPORT
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define QHOTKEY_SHARED_EXPORT
|
||||||
|
#endif
|
||||||
|
|
||||||
//! A class to define global, systemwide Hotkeys
|
//! A class to define global, systemwide Hotkeys
|
||||||
class QHotkey : public QObject
|
class QHOTKEY_SHARED_EXPORT QHotkey : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
friend class QHotkeyPrivate;
|
friend class QHotkeyPrivate;
|
||||||
@@ -19,7 +29,7 @@ class QHotkey : public QObject
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
//! Defines shortcut with native keycodes
|
//! Defines shortcut with native keycodes
|
||||||
class NativeShortcut {
|
class QHOTKEY_SHARED_EXPORT NativeShortcut {
|
||||||
public:
|
public:
|
||||||
//! The native keycode
|
//! The native keycode
|
||||||
quint32 key;
|
quint32 key;
|
||||||
@@ -43,12 +53,14 @@ public:
|
|||||||
bool valid;
|
bool valid;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void addGlobalMapping(const QKeySequence &shortcut, const NativeShortcut &nativeShortcut);
|
||||||
|
|
||||||
//! Constructor
|
//! Constructor
|
||||||
explicit QHotkey(QObject *parent = Q_NULLPTR);
|
explicit QHotkey(QObject *parent = Q_NULLPTR);
|
||||||
//! Constructs a hotkey with a shortcut and optionally registers it
|
//! Constructs a hotkey with a shortcut and optionally registers it
|
||||||
explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
||||||
//! Constructs a hotkey with a key and modifiers and optionally registers it
|
//! Constructs a hotkey with a key and modifiers and optionally registers it
|
||||||
explicit QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
explicit QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
||||||
//! Constructs a hotkey from a native shortcut and optionally registers it
|
//! Constructs a hotkey from a native shortcut and optionally registers it
|
||||||
explicit QHotkey(const NativeShortcut &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
explicit QHotkey(const NativeShortcut &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
||||||
//! Destructor
|
//! Destructor
|
||||||
@@ -73,7 +85,7 @@ public slots:
|
|||||||
//! WRITE-Accessor for QHotkey::shortcut
|
//! WRITE-Accessor for QHotkey::shortcut
|
||||||
bool setShortcut(const QKeySequence &shortcut, bool autoRegister = false);
|
bool setShortcut(const QKeySequence &shortcut, bool autoRegister = false);
|
||||||
//! WRITE-Accessor for QHotkey::shortcut
|
//! WRITE-Accessor for QHotkey::shortcut
|
||||||
bool setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister = false);
|
bool setShortcut(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister = false);
|
||||||
//! RESET-Accessor for QHotkey::shortcut
|
//! RESET-Accessor for QHotkey::shortcut
|
||||||
bool resetShortcut();
|
bool resetShortcut();
|
||||||
|
|
||||||
@@ -88,16 +100,18 @@ signals:
|
|||||||
void registeredChanged(bool registered);
|
void registeredChanged(bool registered);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Qt::Key key;
|
Qt::Key _keyCode;
|
||||||
Qt::KeyboardModifiers mods;
|
Qt::KeyboardModifiers _modifiers;
|
||||||
|
|
||||||
NativeShortcut nativeShortcut;
|
NativeShortcut _nativeShortcut;
|
||||||
bool registered;
|
bool _registered;
|
||||||
};
|
};
|
||||||
|
|
||||||
uint qHash(const QHotkey::NativeShortcut &key);
|
uint QHOTKEY_SHARED_EXPORT qHash(const QHotkey::NativeShortcut &key);
|
||||||
uint qHash(const QHotkey::NativeShortcut &key, uint seed);
|
uint QHOTKEY_SHARED_EXPORT qHash(const QHotkey::NativeShortcut &key, uint seed);
|
||||||
|
|
||||||
Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
|
QHOTKEY_SHARED_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(QHotkey::NativeShortcut)
|
||||||
|
|
||||||
#endif // QHOTKEY_H
|
#endif // QHOTKEY_H
|
||||||
|
|||||||
+19
-10
@@ -13,8 +13,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
// QHotkeyPrivate interface
|
// QHotkeyPrivate interface
|
||||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||||
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
@@ -35,9 +35,10 @@ bool QHotkeyPrivateMac::nativeEventFilter(const QByteArray &eventType, void *mes
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode, bool &ok)
|
||||||
{
|
{
|
||||||
// Constants found in NSEvent.h from AppKit.framework
|
// Constants found in NSEvent.h from AppKit.framework
|
||||||
|
ok = true;
|
||||||
switch (keycode) {
|
switch (keycode) {
|
||||||
case Qt::Key_Return:
|
case Qt::Key_Return:
|
||||||
return kVK_Return;
|
return kVK_Return;
|
||||||
@@ -120,7 +121,8 @@ quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
|||||||
case Qt::Key_Up:
|
case Qt::Key_Up:
|
||||||
return kVK_UpArrow;
|
return kVK_UpArrow;
|
||||||
default:
|
default:
|
||||||
;
|
ok = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
UTF16Char ch = keycode;
|
UTF16Char ch = keycode;
|
||||||
@@ -157,19 +159,25 @@ quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
|||||||
long idx = keyToChar[k] & kUCKeyOutputGetIndexMask;
|
long idx = keyToChar[k] & kUCKeyOutputGetIndexMask;
|
||||||
if (stateRec && idx < stateRec->keyStateRecordCount) {
|
if (stateRec && idx < stateRec->keyStateRecordCount) {
|
||||||
UCKeyStateRecord* rec = reinterpret_cast<UCKeyStateRecord*>(data + stateRec->keyStateRecordOffsets[idx]);
|
UCKeyStateRecord* rec = reinterpret_cast<UCKeyStateRecord*>(data + stateRec->keyStateRecordOffsets[idx]);
|
||||||
if (rec->stateZeroCharData == ch) return k;
|
if (rec->stateZeroCharData == ch) {
|
||||||
|
ok = true;
|
||||||
|
return k;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!(keyToChar[k] & kUCKeyOutputSequenceIndexMask) && keyToChar[k] < 0xFFFE) {
|
else if (!(keyToChar[k] & kUCKeyOutputSequenceIndexMask) && keyToChar[k] < 0xFFFE) {
|
||||||
if (keyToChar[k] == ch) return k;
|
if (keyToChar[k] == ch) {
|
||||||
|
ok = true;
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} // for k
|
|
||||||
} // for j
|
|
||||||
} // for i
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivateMac::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
quint32 QHotkeyPrivateMac::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
||||||
{
|
{
|
||||||
quint32 nMods = 0;
|
quint32 nMods = 0;
|
||||||
if (modifiers & Qt::ShiftModifier)
|
if (modifiers & Qt::ShiftModifier)
|
||||||
@@ -182,6 +190,7 @@ quint32 QHotkeyPrivateMac::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
|||||||
nMods |= controlKey;
|
nMods |= controlKey;
|
||||||
if (modifiers & Qt::KeypadModifier)
|
if (modifiers & Qt::KeypadModifier)
|
||||||
nMods |= kEventKeyModifierNumLockMask;
|
nMods |= kEventKeyModifierNumLockMask;
|
||||||
|
ok = true;
|
||||||
return nMods;
|
return nMods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-11
@@ -7,7 +7,7 @@
|
|||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QGlobalStatic>
|
#include <QGlobalStatic>
|
||||||
|
|
||||||
class QHotkeyPrivate : public QObject, public QAbstractNativeEventFilter
|
class QHOTKEY_SHARED_EXPORT QHotkeyPrivate : public QObject, public QAbstractNativeEventFilter
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@@ -25,19 +25,20 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void activateShortcut(QHotkey::NativeShortcut shortcut);
|
void activateShortcut(QHotkey::NativeShortcut shortcut);
|
||||||
|
|
||||||
virtual quint32 nativeKeycode(Qt::Key keycode) = 0;//platform implement
|
virtual quint32 nativeKeycode(Qt::Key keycode, bool &ok) = 0;//platform implement
|
||||||
virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) = 0;//platform implement
|
virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) = 0;//platform implement
|
||||||
|
|
||||||
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||||
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||||
|
|
||||||
private:/*functions*/
|
private:
|
||||||
|
QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping;
|
||||||
|
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
|
||||||
|
|
||||||
|
Q_INVOKABLE void addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut);
|
||||||
Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey);
|
Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey);
|
||||||
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
|
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
|
||||||
Q_INVOKABLE inline QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
Q_INVOKABLE QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
||||||
|
|
||||||
private:
|
|
||||||
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define NATIVE_INSTANCE(ClassName) \
|
#define NATIVE_INSTANCE(ClassName) \
|
||||||
@@ -48,8 +49,7 @@ private:
|
|||||||
return hotkeyPrivate;\
|
return hotkeyPrivate;\
|
||||||
}
|
}
|
||||||
|
|
||||||
inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
|
Q_DECLARE_METATYPE(Qt::Key)
|
||||||
return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
|
Q_DECLARE_METATYPE(Qt::KeyboardModifiers)
|
||||||
}
|
|
||||||
|
|
||||||
#endif // QHOTKEY_P_H
|
#endif // QHOTKEY_P_H
|
||||||
|
|||||||
+11
-4
@@ -13,8 +13,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
// QHotkeyPrivate interface
|
// QHotkeyPrivate interface
|
||||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||||
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
@@ -35,8 +35,9 @@ bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *mes
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode)
|
quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok)
|
||||||
{
|
{
|
||||||
|
ok = true;
|
||||||
if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character"
|
if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character"
|
||||||
const SHORT vKey = VkKeyScanW(keycode);
|
const SHORT vKey = VkKeyScanW(keycode);
|
||||||
if(vKey > -1)
|
if(vKey > -1)
|
||||||
@@ -201,11 +202,16 @@ quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode)
|
|||||||
return VK_OEM_FJ_TOUROKU;
|
return VK_OEM_FJ_TOUROKU;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
if(keycode <= 0xFFFF)
|
||||||
|
return (byte)keycode;
|
||||||
|
else {
|
||||||
|
ok = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
||||||
{
|
{
|
||||||
quint32 nMods = 0;
|
quint32 nMods = 0;
|
||||||
if (modifiers & Qt::ShiftModifier)
|
if (modifiers & Qt::ShiftModifier)
|
||||||
@@ -216,6 +222,7 @@ quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
|||||||
nMods |= MOD_ALT;
|
nMods |= MOD_ALT;
|
||||||
if (modifiers & Qt::MetaModifier)
|
if (modifiers & Qt::MetaModifier)
|
||||||
nMods |= MOD_WIN;
|
nMods |= MOD_WIN;
|
||||||
|
ok = true;
|
||||||
return nMods;
|
return nMods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+53
-14
@@ -6,6 +6,11 @@
|
|||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <xcb/xcb.h>
|
#include <xcb/xcb.h>
|
||||||
|
|
||||||
|
//compability to pre Qt 5.8
|
||||||
|
#ifndef Q_FALLTHROUGH
|
||||||
|
#define Q_FALLTHROUGH() (void)0
|
||||||
|
#endif
|
||||||
|
|
||||||
class QHotkeyPrivateX11 : public QHotkeyPrivate
|
class QHotkeyPrivateX11 : public QHotkeyPrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -14,8 +19,9 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
// QHotkeyPrivate interface
|
// QHotkeyPrivate interface
|
||||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||||
|
QString getX11String(Qt::Key keycode);
|
||||||
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
@@ -58,9 +64,33 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode)
|
QString QHotkeyPrivateX11::getX11String(Qt::Key keycode)
|
||||||
{
|
{
|
||||||
KeySym keysym = XStringToKeysym(QKeySequence(keycode).toString(QKeySequence::NativeText).toLatin1().constData());
|
switch(keycode){
|
||||||
|
|
||||||
|
case Qt::Key_MediaLast :
|
||||||
|
case Qt::Key_MediaPrevious :
|
||||||
|
return "XF86AudioPrev";
|
||||||
|
case Qt::Key_MediaNext :
|
||||||
|
return "XF86AudioNext";
|
||||||
|
case Qt::Key_MediaPause :
|
||||||
|
case Qt::Key_MediaPlay :
|
||||||
|
case Qt::Key_MediaTogglePlayPause :
|
||||||
|
return "XF86AudioPlay";
|
||||||
|
case Qt::Key_MediaRecord :
|
||||||
|
return "XF86AudioRecord";
|
||||||
|
case Qt::Key_MediaStop :
|
||||||
|
return "XF86AudioStop";
|
||||||
|
default :
|
||||||
|
return QKeySequence(keycode).toString(QKeySequence::NativeText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok)
|
||||||
|
{
|
||||||
|
QString keyString = getX11String(keycode);
|
||||||
|
|
||||||
|
KeySym keysym = XStringToKeysym(keyString.toLatin1().constData());
|
||||||
if (keysym == NoSymbol) {
|
if (keysym == NoSymbol) {
|
||||||
//not found -> just use the key
|
//not found -> just use the key
|
||||||
if(keycode <= 0xFFFF)
|
if(keycode <= 0xFFFF)
|
||||||
@@ -70,13 +100,16 @@ quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Display *display = QX11Info::display();
|
Display *display = QX11Info::display();
|
||||||
if(display)
|
if(display) {
|
||||||
return XKeysymToKeycode(QX11Info::display(), keysym);
|
auto res = XKeysymToKeycode(QX11Info::display(), keysym);
|
||||||
else
|
if(res != 0)
|
||||||
|
ok = true;
|
||||||
|
return res;
|
||||||
|
} else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
||||||
{
|
{
|
||||||
quint32 nMods = 0;
|
quint32 nMods = 0;
|
||||||
if (modifiers & Qt::ShiftModifier)
|
if (modifiers & Qt::ShiftModifier)
|
||||||
@@ -87,6 +120,7 @@ quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
|||||||
nMods |= Mod1Mask;
|
nMods |= Mod1Mask;
|
||||||
if (modifiers & Qt::MetaModifier)
|
if (modifiers & Qt::MetaModifier)
|
||||||
nMods |= Mod4Mask;
|
nMods |= Mod4Mask;
|
||||||
|
ok = true;
|
||||||
return nMods;
|
return nMods;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,9 +140,10 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
GrabModeAsync,
|
GrabModeAsync,
|
||||||
GrabModeAsync);
|
GrabModeAsync);
|
||||||
}
|
}
|
||||||
|
XSync(display, False);
|
||||||
|
|
||||||
if(errorHandler.hasError) {
|
if(errorHandler.hasError) {
|
||||||
qCWarning(logQHotkey) << "[QHotkey] Failed to register hotkey. Error:"
|
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
||||||
<< qPrintable(errorHandler.errorString);
|
<< qPrintable(errorHandler.errorString);
|
||||||
this->unregisterShortcut(shortcut);
|
this->unregisterShortcut(shortcut);
|
||||||
return false;
|
return false;
|
||||||
@@ -129,11 +164,11 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
shortcut.modifier | specialMod,
|
shortcut.modifier | specialMod,
|
||||||
DefaultRootWindow(display));
|
DefaultRootWindow(display));
|
||||||
}
|
}
|
||||||
|
XSync(display, False);
|
||||||
|
|
||||||
if(errorHandler.hasError) {
|
if(errorHandler.hasError) {
|
||||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
||||||
<< qPrintable(errorHandler.errorString);
|
<< qPrintable(errorHandler.errorString);
|
||||||
this->unregisterShortcut(shortcut);
|
|
||||||
return false;
|
return false;
|
||||||
} else
|
} else
|
||||||
return true;
|
return true;
|
||||||
@@ -155,12 +190,14 @@ QString QHotkeyPrivateX11::HotkeyErrorHandler::errorString;
|
|||||||
|
|
||||||
QHotkeyPrivateX11::HotkeyErrorHandler::HotkeyErrorHandler()
|
QHotkeyPrivateX11::HotkeyErrorHandler::HotkeyErrorHandler()
|
||||||
{
|
{
|
||||||
this->prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
|
prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkeyPrivateX11::HotkeyErrorHandler::~HotkeyErrorHandler()
|
QHotkeyPrivateX11::HotkeyErrorHandler::~HotkeyErrorHandler()
|
||||||
{
|
{
|
||||||
XSetErrorHandler(this->prevHandler);
|
XSetErrorHandler(prevHandler);
|
||||||
|
hasError = false;
|
||||||
|
errorString.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorEvent *error)
|
int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorEvent *error)
|
||||||
@@ -171,10 +208,12 @@ int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorE
|
|||||||
case BadWindow:
|
case BadWindow:
|
||||||
if (error->request_code == 33 || //grab key
|
if (error->request_code == 33 || //grab key
|
||||||
error->request_code == 34) {// ungrab key
|
error->request_code == 34) {// ungrab key
|
||||||
HotkeyErrorHandler::hasError = true;
|
hasError = true;
|
||||||
HotkeyErrorHandler::errorString = QHotkeyPrivateX11::formatX11Error(display, error->error_code);
|
errorString = QHotkeyPrivateX11::formatX11Error(display, error->error_code);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Q_FALLTHROUGH();
|
||||||
|
// fall through
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,23 @@ The QHotkey is a class that can be used to create hotkeys/global shortcuts, aka
|
|||||||
- Thread-Safe - Can be used on all threads (See section Thread safety)
|
- Thread-Safe - Can be used on all threads (See section Thread safety)
|
||||||
- Allows usage of native keycodes and modifiers, if needed
|
- Allows usage of native keycodes and modifiers, if needed
|
||||||
|
|
||||||
|
**Note:** For now Wayland is not supported, as it is simply not possible to register a global shortcut with wayland. For more details, or possible Ideas on how to get Hotkeys working on wayland, see [Issue #14](https://github.com/Skycoder42/QHotkey/issues/14).
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
The package is providet as qpm package, [`de.skycoder42.qhotkey`](https://www.qpm.io/packages/de.skycoder42.qhotkey/index.html). To install:
|
||||||
|
|
||||||
|
1. Install qpm (See [GitHub - Installing](https://github.com/Cutehacks/qpm/blob/master/README.md#installing), for **windows** see below)
|
||||||
|
2. In your projects root directory, run `qpm install de.skycoder42.qhotkey`
|
||||||
|
3. Include qpm to your project by adding `include(vendor/vendor.pri)` to your `.pro` file
|
||||||
|
|
||||||
|
Check their [GitHub - Usage for App Developers](https://github.com/Cutehacks/qpm/blob/master/README.md#usage-for-app-developers) to learn more about qpm.
|
||||||
|
|
||||||
|
**Important for Windows users:** QPM Version *0.10.0* (the one you can download on the website) is currently broken on windows! It's already fixed in master, but not released yet. Until a newer versions gets released, you can download the latest dev build from here:
|
||||||
|
- https://storage.googleapis.com/www.qpm.io/download/latest/windows_amd64/qpm.exe
|
||||||
|
- https://storage.googleapis.com/www.qpm.io/download/latest/windows_386/qpm.exe
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
Just copy the repository into you application (preferebly by adding it as a git submodule) and add the line `include(./QHotkey/qhotkey.pri)` to your .pro-file. This way all files and required libraries will automatically be added. Use `#include <QHotkey>` to access the class.
|
The general usage is to create QHotkey instances for specific hotkeys, register them and then simply connect to the signal emitted once the hotkey is pressed.
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
The following example shows a simple application that will run without a window in the background until you press the key-combination <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Q</kbd> (<kbd>⌘</kbd>+<kbd>⌥</kbd>+<kbd>Q</kbd> on Mac). This will quit the application. The debug output will tell if the hotkey was successfully registered and that it was pressed.
|
The following example shows a simple application that will run without a window in the background until you press the key-combination <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Q</kbd> (<kbd>⌘</kbd>+<kbd>⌥</kbd>+<kbd>Q</kbd> on Mac). This will quit the application. The debug output will tell if the hotkey was successfully registered and that it was pressed.
|
||||||
@@ -69,7 +84,7 @@ The documentation was created using [doxygen](http://www.doxygen.org). It includ
|
|||||||
## Technical
|
## Technical
|
||||||
### Requirements
|
### Requirements
|
||||||
- I built it with Qt 5.7, but may work with earlier versions, too
|
- I built it with Qt 5.7, but may work with earlier versions, too
|
||||||
- At least the QtGui-Module (a QGuiApplication). Hotkeys on console based applications are not supported. (By the operating systems)
|
- At least the QtGui-Module (a QGuiApplication). Hotkeys on console based applications are not supported (By the operating systems). You can however create a gui application without any visible window.
|
||||||
- C++11
|
- C++11
|
||||||
|
|
||||||
### Known Limitations
|
### Known Limitations
|
||||||
@@ -77,3 +92,4 @@ The documentation was created using [doxygen](http://www.doxygen.org). It includ
|
|||||||
- Qt::Key makes no difference between normal numbers and the Numpad numbers. Most keyboards however require this. Thus, you can't register shortcuts for the numpad, unless you use a native shortcut.
|
- Qt::Key makes no difference between normal numbers and the Numpad numbers. Most keyboards however require this. Thus, you can't register shortcuts for the numpad, unless you use a native shortcut.
|
||||||
- Supports not all keys, but most of the common ones. There are differences between platforms and it depends on the Keyboard-Layout. "Delete", for example, works on windows and mac, but not on X11 (At least on my test machines). I tried to use OS-Functions where possible, but since the Qt::Key values need to be converted into native keys, there are some limitations. I can use need such a key, try using the native shortcut.
|
- Supports not all keys, but most of the common ones. There are differences between platforms and it depends on the Keyboard-Layout. "Delete", for example, works on windows and mac, but not on X11 (At least on my test machines). I tried to use OS-Functions where possible, but since the Qt::Key values need to be converted into native keys, there are some limitations. I can use need such a key, try using the native shortcut.
|
||||||
- The registered keys will be "taken" by QHotkey. This means after a hotkey was cosumend by your application, it will not be sent to the active application. This is done this way by the operating systems and cannot be changed.
|
- The registered keys will be "taken" by QHotkey. This means after a hotkey was cosumend by your application, it will not be sent to the active application. This is done this way by the operating systems and cannot be changed.
|
||||||
|
- If you get a `QHotkey: Failed to register hotkey. Error: BadAccess (attempt to access private resource denied)` error on X11, this means you are trying to register a hotkey that is private to X11. Those keys simply cannot be registered using the normal API
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
mac: LIBS += -framework Carbon
|
||||||
|
else:win32: LIBS += -luser32
|
||||||
|
else:unix {
|
||||||
|
QT += x11extras
|
||||||
|
LIBS += -lX11
|
||||||
|
}
|
||||||
+8
-14
@@ -1,23 +1,17 @@
|
|||||||
CONFIG += C++11
|
CONFIG += C++11
|
||||||
|
|
||||||
HEADERS += $$PWD/QHotkey/qhotkey.h \
|
PUBLIC_HEADERS += $$PWD/QHotkey/qhotkey.h \
|
||||||
|
$$PWD/QHotkey/QHotkey
|
||||||
|
|
||||||
|
HEADERS += $$PUBLIC_HEADERS \
|
||||||
$$PWD/QHotkey/qhotkey_p.h
|
$$PWD/QHotkey/qhotkey_p.h
|
||||||
|
|
||||||
SOURCES += $$PWD/QHotkey/qhotkey.cpp
|
SOURCES += $$PWD/QHotkey/qhotkey.cpp
|
||||||
|
|
||||||
mac {
|
mac: SOURCES += $$PWD/QHotkey/qhotkey_mac.cpp
|
||||||
SOURCES += $$PWD/QHotkey/qhotkey_mac.cpp
|
else:win32: SOURCES += $$PWD/QHotkey/qhotkey_win.cpp
|
||||||
LIBS += -framework Carbon
|
else:unix: SOURCES += $$PWD/QHotkey/qhotkey_x11.cpp
|
||||||
} else:win32 {
|
|
||||||
SOURCES += $$PWD/QHotkey/qhotkey_win.cpp
|
|
||||||
LIBS += -lUser32
|
|
||||||
} else:unix {
|
|
||||||
SOURCES += $$PWD/QHotkey/qhotkey_x11.cpp
|
|
||||||
QT += x11extras
|
|
||||||
LIBS += -lX11
|
|
||||||
}
|
|
||||||
|
|
||||||
INCLUDEPATH += $$PWD/QHotkey
|
INCLUDEPATH += $$PWD/QHotkey
|
||||||
|
|
||||||
DISTFILES += \
|
include($$PWD/qhotkey.prc)
|
||||||
$$PWD/QHotkey/qhotkey.pri
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"author": {
|
||||||
|
"email": "skycoder42.de@gmx.de",
|
||||||
|
"name": "Skycoder"
|
||||||
|
},
|
||||||
|
"dependencies": [
|
||||||
|
],
|
||||||
|
"description": "A global shortcut/hotkey for Desktop Qt-Applications",
|
||||||
|
"license": "BSD_3_CLAUSE",
|
||||||
|
"name": "de.skycoder42.qhotkey",
|
||||||
|
"pri_filename": "qhotkey.pri",
|
||||||
|
"repository": {
|
||||||
|
"type": "GITHUB",
|
||||||
|
"url": "https://github.com/Skycoder42/QHotkey.git"
|
||||||
|
},
|
||||||
|
"version": {
|
||||||
|
"fingerprint": "",
|
||||||
|
"label": "1.2.2",
|
||||||
|
"revision": ""
|
||||||
|
},
|
||||||
|
"webpage": "https://github.com/Skycoder42/QHotkey"
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"dependencies": [
|
||||||
|
],
|
||||||
|
"license": {
|
||||||
|
"file": "LICENSE",
|
||||||
|
"name": "BSD_3_CLAUSE"
|
||||||
|
},
|
||||||
|
"prcFile": "qhotkey.prc",
|
||||||
|
"priFile": "qhotkey.pri",
|
||||||
|
"priIncludes": [
|
||||||
|
],
|
||||||
|
"publishers": {
|
||||||
|
"qpm": {
|
||||||
|
"author": {
|
||||||
|
"email": "skycoder42.de@gmx.de",
|
||||||
|
"name": "Skycoder"
|
||||||
|
},
|
||||||
|
"description": "A global shortcut/hotkey for Desktop Qt-Applications",
|
||||||
|
"name": "de.skycoder42.qhotkey",
|
||||||
|
"repository": {
|
||||||
|
"type": "GITHUB",
|
||||||
|
"url": "https://github.com/Skycoder42/QHotkey.git"
|
||||||
|
},
|
||||||
|
"webpage": "https://github.com/Skycoder42/QHotkey"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": false
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user