mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0678c4cfc | ||
|
|
cdb4d37453 | ||
|
|
9eace8fbbd | ||
|
|
6baf644b40 | ||
|
|
3fa176a380 | ||
|
|
7c4ef6a03a | ||
|
|
15e34a7dbf | ||
|
|
b8f32c3381 | ||
|
|
10d1e44b21 | ||
|
|
040cf5ec0c | ||
|
|
4446fe0a42 | ||
|
|
53bd8cdd14 |
@@ -17,7 +17,7 @@
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>3</number>
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="playTab">
|
||||
<attribute name="title">
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
#include "hottestwidget.h"
|
||||
#include <QApplication>
|
||||
|
||||
//#define START_BACKGROUND
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
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();
|
||||
#endif
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
+77
-72
@@ -10,81 +10,81 @@ Q_LOGGING_CATEGORY(logQHotkey, "QHotkey")
|
||||
|
||||
QHotkey::QHotkey(QObject *parent) :
|
||||
QObject(parent),
|
||||
key(Qt::Key_unknown),
|
||||
mods(Qt::NoModifier),
|
||||
nativeShortcut(),
|
||||
registered(false)
|
||||
_keyCode(Qt::Key_unknown),
|
||||
_modifiers(Qt::NoModifier),
|
||||
_nativeShortcut(),
|
||||
_registered(false)
|
||||
{}
|
||||
|
||||
QHotkey::QHotkey(const QKeySequence &sequence, bool autoRegister, QObject *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)
|
||||
{
|
||||
this->setShortcut(key, modifiers, autoRegister);
|
||||
setShortcut(keyCode, modifiers, autoRegister);
|
||||
}
|
||||
|
||||
QHotkey::QHotkey(const QHotkey::NativeShortcut &shortcut, bool autoRegister, QObject *parent) :
|
||||
QHotkey(parent)
|
||||
{
|
||||
this->setNativeShortcut(shortcut, autoRegister);
|
||||
setNativeShortcut(shortcut, autoRegister);
|
||||
}
|
||||
|
||||
QHotkey::~QHotkey()
|
||||
{
|
||||
if(this->registered)
|
||||
if(_registered)
|
||||
QHotkeyPrivate::instance()->removeShortcut(this);
|
||||
}
|
||||
|
||||
QKeySequence QHotkey::shortcut() const
|
||||
{
|
||||
if(this->key == Qt::Key_unknown)
|
||||
if(_keyCode == Qt::Key_unknown)
|
||||
return QKeySequence();
|
||||
else
|
||||
return QKeySequence(this->key | this->mods);
|
||||
return QKeySequence(_keyCode | _modifiers);
|
||||
}
|
||||
|
||||
Qt::Key QHotkey::keyCode() const
|
||||
{
|
||||
return this->key;
|
||||
return _keyCode;
|
||||
}
|
||||
|
||||
Qt::KeyboardModifiers QHotkey::modifiers() const
|
||||
{
|
||||
return this->mods;
|
||||
return _modifiers;
|
||||
}
|
||||
|
||||
QHotkey::NativeShortcut QHotkey::currentNativeShortcut() const
|
||||
{
|
||||
return this->nativeShortcut;
|
||||
return _nativeShortcut;
|
||||
}
|
||||
|
||||
bool QHotkey::isRegistered() const
|
||||
{
|
||||
return this->registered;
|
||||
return _registered;
|
||||
}
|
||||
|
||||
bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
|
||||
{
|
||||
if(shortcut.isEmpty()) {
|
||||
return this->resetShortcut();
|
||||
return resetShortcut();
|
||||
} else if(shortcut.count() > 1) {
|
||||
qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
|
||||
"Only the first shortcut will be used!");
|
||||
}
|
||||
|
||||
return this->setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
|
||||
Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask),
|
||||
autoRegister);
|
||||
return setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
|
||||
Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask),
|
||||
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(!QHotkeyPrivate::instance()->removeShortcut(this))
|
||||
return false;
|
||||
@@ -92,46 +92,46 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
|
||||
return false;
|
||||
}
|
||||
|
||||
if(key == Qt::Key_unknown) {
|
||||
this->key = Qt::Key_unknown;
|
||||
this->mods = Qt::NoModifier;
|
||||
this->nativeShortcut = NativeShortcut();
|
||||
if(keyCode == Qt::Key_unknown) {
|
||||
_keyCode = Qt::Key_unknown;
|
||||
_modifiers = Qt::NoModifier;
|
||||
_nativeShortcut = NativeShortcut();
|
||||
return true;
|
||||
}
|
||||
|
||||
this->key = key;
|
||||
this->mods = modifiers;
|
||||
this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers);
|
||||
if(this->nativeShortcut.isValid()) {
|
||||
_keyCode = keyCode;
|
||||
_modifiers = modifiers;
|
||||
_nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(keyCode, modifiers);
|
||||
if(_nativeShortcut.isValid()) {
|
||||
if(autoRegister)
|
||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||
else
|
||||
return true;
|
||||
} else {
|
||||
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << key << "Modifiers:" << modifiers;
|
||||
this->key = Qt::Key_unknown;
|
||||
this->mods = Qt::NoModifier;
|
||||
this->nativeShortcut = NativeShortcut();
|
||||
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers;
|
||||
_keyCode = Qt::Key_unknown;
|
||||
_modifiers = Qt::NoModifier;
|
||||
_nativeShortcut = NativeShortcut();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool QHotkey::resetShortcut()
|
||||
{
|
||||
if(this->registered &&
|
||||
if(_registered &&
|
||||
!QHotkeyPrivate::instance()->removeShortcut(this)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this->key = Qt::Key_unknown;
|
||||
this->mods = Qt::NoModifier;
|
||||
this->nativeShortcut = NativeShortcut();
|
||||
_keyCode = Qt::Key_unknown;
|
||||
_modifiers = Qt::NoModifier;
|
||||
_nativeShortcut = NativeShortcut();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool autoRegister)
|
||||
{
|
||||
if(this->registered) {
|
||||
if(_registered) {
|
||||
if(autoRegister) {
|
||||
if(!QHotkeyPrivate::instance()->removeShortcut(this))
|
||||
return false;
|
||||
@@ -140,27 +140,27 @@ bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool aut
|
||||
}
|
||||
|
||||
if(nativeShortcut.isValid()) {
|
||||
this->key = Qt::Key_unknown;
|
||||
this->mods = Qt::NoModifier;
|
||||
this->nativeShortcut = nativeShortcut;
|
||||
_keyCode = Qt::Key_unknown;
|
||||
_modifiers = Qt::NoModifier;
|
||||
_nativeShortcut = nativeShortcut;
|
||||
if(autoRegister)
|
||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||
else
|
||||
return true;
|
||||
} else {
|
||||
this->key = Qt::Key_unknown;
|
||||
this->mods = Qt::NoModifier;
|
||||
this->nativeShortcut = NativeShortcut();
|
||||
_keyCode = Qt::Key_unknown;
|
||||
_modifiers = Qt::NoModifier;
|
||||
_nativeShortcut = NativeShortcut();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool QHotkey::setRegistered(bool registered)
|
||||
{
|
||||
if(this->registered && !registered)
|
||||
if(_registered && !registered)
|
||||
return QHotkeyPrivate::instance()->removeShortcut(this);
|
||||
else if(!this->registered && registered) {
|
||||
if(!this->nativeShortcut.isValid())
|
||||
else if(!_registered && registered) {
|
||||
if(!_nativeShortcut.isValid())
|
||||
return false;
|
||||
else
|
||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||
@@ -181,15 +181,15 @@ QHotkeyPrivate::QHotkeyPrivate() :
|
||||
|
||||
QHotkeyPrivate::~QHotkeyPrivate()
|
||||
{
|
||||
if(!this->shortcuts.isEmpty())
|
||||
qCWarning(logQHotkey, "QHotkeyPrivate destroyed with registered shortcuts!");
|
||||
if(!shortcuts.isEmpty())
|
||||
qCWarning(logQHotkey) << "QHotkeyPrivate destroyed with registered shortcuts!";
|
||||
if(qApp && qApp->eventDispatcher())
|
||||
qApp->eventDispatcher()->removeNativeEventFilter(this);
|
||||
}
|
||||
|
||||
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::BlockingQueuedConnection);
|
||||
QHotkey::NativeShortcut res;
|
||||
@@ -204,10 +204,10 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb
|
||||
|
||||
bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
|
||||
{
|
||||
if(hotkey->registered)
|
||||
if(hotkey->_registered)
|
||||
return false;
|
||||
|
||||
Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ?
|
||||
Qt::ConnectionType conType = (QThread::currentThread() == thread() ?
|
||||
Qt::DirectConnection :
|
||||
Qt::BlockingQueuedConnection);
|
||||
bool res = false;
|
||||
@@ -224,10 +224,10 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
|
||||
|
||||
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
||||
{
|
||||
if(!hotkey->registered)
|
||||
if(!hotkey->_registered)
|
||||
return false;
|
||||
|
||||
Qt::ConnectionType conType = (QThread::currentThread() == this->thread() ?
|
||||
Qt::ConnectionType conType = (QThread::currentThread() == thread() ?
|
||||
Qt::DirectConnection :
|
||||
Qt::BlockingQueuedConnection);
|
||||
bool res = false;
|
||||
@@ -245,38 +245,43 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
||||
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
|
||||
{
|
||||
QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated);
|
||||
for(QHotkey *hkey : this->shortcuts.values(shortcut))
|
||||
for(QHotkey *hkey : shortcuts.values(shortcut))
|
||||
signal.invoke(hkey, Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey)
|
||||
{
|
||||
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
|
||||
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;
|
||||
|
||||
if(!this->shortcuts.contains(shortcut)) {
|
||||
if(!this->registerShortcut(shortcut))
|
||||
if(!shortcuts.contains(shortcut)) {
|
||||
if(!registerShortcut(shortcut))
|
||||
return false;
|
||||
}
|
||||
|
||||
this->shortcuts.insert(shortcut, hotkey);
|
||||
hotkey->registered = true;
|
||||
shortcuts.insert(shortcut, hotkey);
|
||||
hotkey->_registered = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
hotkey->registered = false;
|
||||
hotkey->_registered = false;
|
||||
emit hotkey->registeredChanged(true);
|
||||
if(this->shortcuts.count(shortcut) == 0)
|
||||
return this->unregisterShortcut(shortcut);
|
||||
if(shortcuts.count(shortcut) == 0)
|
||||
return unregisterShortcut(shortcut);
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
|
||||
{
|
||||
return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
|
||||
}
|
||||
|
||||
|
||||
|
||||
QHotkey::NativeShortcut::NativeShortcut() :
|
||||
@@ -293,21 +298,21 @@ QHotkey::NativeShortcut::NativeShortcut(quint32 key, quint32 modifier) :
|
||||
|
||||
bool QHotkey::NativeShortcut::isValid() const
|
||||
{
|
||||
return this->valid;
|
||||
return valid;
|
||||
}
|
||||
|
||||
bool QHotkey::NativeShortcut::operator ==(const QHotkey::NativeShortcut &other) const
|
||||
{
|
||||
return (this->key == other.key) &&
|
||||
(this->modifier == other.modifier) &&
|
||||
this->valid == other.valid;
|
||||
return (key == other.key) &&
|
||||
(modifier == other.modifier) &&
|
||||
valid == other.valid;
|
||||
}
|
||||
|
||||
bool QHotkey::NativeShortcut::operator !=(const QHotkey::NativeShortcut &other) const
|
||||
{
|
||||
return (this->key != other.key) ||
|
||||
(this->modifier != other.modifier) ||
|
||||
this->valid != other.valid;
|
||||
return (key != other.key) ||
|
||||
(modifier != other.modifier) ||
|
||||
valid != other.valid;
|
||||
}
|
||||
|
||||
uint qHash(const QHotkey::NativeShortcut &key)
|
||||
|
||||
+6
-6
@@ -48,7 +48,7 @@ public:
|
||||
//! Constructs a hotkey with a shortcut and optionally registers it
|
||||
explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
||||
//! 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
|
||||
explicit QHotkey(const NativeShortcut &shortcut, bool autoRegister = false, QObject *parent = Q_NULLPTR);
|
||||
//! Destructor
|
||||
@@ -73,7 +73,7 @@ public slots:
|
||||
//! WRITE-Accessor for QHotkey::shortcut
|
||||
bool setShortcut(const QKeySequence &shortcut, bool autoRegister = false);
|
||||
//! 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
|
||||
bool resetShortcut();
|
||||
|
||||
@@ -88,11 +88,11 @@ signals:
|
||||
void registeredChanged(bool registered);
|
||||
|
||||
private:
|
||||
Qt::Key key;
|
||||
Qt::KeyboardModifiers mods;
|
||||
Qt::Key _keyCode;
|
||||
Qt::KeyboardModifiers _modifiers;
|
||||
|
||||
NativeShortcut nativeShortcut;
|
||||
bool registered;
|
||||
NativeShortcut _nativeShortcut;
|
||||
bool _registered;
|
||||
};
|
||||
|
||||
uint qHash(const QHotkey::NativeShortcut &key);
|
||||
|
||||
@@ -163,9 +163,9 @@ quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
||||
else if (!(keyToChar[k] & kUCKeyOutputSequenceIndexMask) && keyToChar[k] < 0xFFFE) {
|
||||
if (keyToChar[k] == ch) return k;
|
||||
}
|
||||
} // for k
|
||||
} // for j
|
||||
} // for i
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -196,12 +196,12 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||
}
|
||||
|
||||
EventHotKeyID hkeyID;
|
||||
hkeyID.signature = shortcut.key;
|
||||
hkeyID.id = shortcut.modifier;
|
||||
hkeyID.signature = shortcut.key;
|
||||
hkeyID.id = shortcut.modifier;
|
||||
|
||||
EventHotKeyRef eventRef = 0;
|
||||
OSStatus status = RegisterEventHotKey(shortcut.key,
|
||||
shortcut.modifier,
|
||||
OSStatus status = RegisterEventHotKey(shortcut.key,
|
||||
shortcut.modifier,
|
||||
hkeyID,
|
||||
GetApplicationEventTarget(),
|
||||
0,
|
||||
|
||||
+4
-9
@@ -31,13 +31,12 @@ protected:
|
||||
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||
|
||||
private:/*functions*/
|
||||
Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey);
|
||||
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
|
||||
Q_INVOKABLE inline QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
||||
|
||||
private:
|
||||
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
|
||||
|
||||
Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey);
|
||||
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
|
||||
Q_INVOKABLE QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
||||
};
|
||||
|
||||
#define NATIVE_INSTANCE(ClassName) \
|
||||
@@ -48,8 +47,4 @@ private:
|
||||
return hotkeyPrivate;\
|
||||
}
|
||||
|
||||
inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
|
||||
return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
|
||||
}
|
||||
|
||||
#endif // QHOTKEY_P_H
|
||||
|
||||
+98
-96
@@ -9,35 +9,35 @@
|
||||
class QHotkeyPrivateX11 : public QHotkeyPrivate
|
||||
{
|
||||
public:
|
||||
// QAbstractNativeEventFilter interface
|
||||
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
|
||||
// QAbstractNativeEventFilter interface
|
||||
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
|
||||
|
||||
protected:
|
||||
// QHotkeyPrivate interface
|
||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
||||
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||
// QHotkeyPrivate interface
|
||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
||||
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||
|
||||
private:
|
||||
static const QVector<quint32> specialModifiers;
|
||||
static const quint32 validModsMask;
|
||||
static const QVector<quint32> specialModifiers;
|
||||
static const quint32 validModsMask;
|
||||
|
||||
static QString formatX11Error(Display *display, int errorCode);
|
||||
static QString formatX11Error(Display *display, int errorCode);
|
||||
|
||||
class HotkeyErrorHandler {
|
||||
public:
|
||||
HotkeyErrorHandler();
|
||||
~HotkeyErrorHandler();
|
||||
class HotkeyErrorHandler {
|
||||
public:
|
||||
HotkeyErrorHandler();
|
||||
~HotkeyErrorHandler();
|
||||
|
||||
static bool hasError;
|
||||
static QString errorString;
|
||||
|
||||
private:
|
||||
XErrorHandler prevHandler;
|
||||
private:
|
||||
XErrorHandler prevHandler;
|
||||
|
||||
static int handleError(Display *display, XErrorEvent *error);
|
||||
};
|
||||
static int handleError(Display *display, XErrorEvent *error);
|
||||
};
|
||||
};
|
||||
NATIVE_INSTANCE(QHotkeyPrivateX11)
|
||||
|
||||
@@ -46,104 +46,106 @@ const quint32 QHotkeyPrivateX11::validModsMask = ShiftMask | ControlMask | Mod1M
|
||||
|
||||
bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
|
||||
{
|
||||
Q_UNUSED(eventType);
|
||||
Q_UNUSED(result);
|
||||
Q_UNUSED(eventType);
|
||||
Q_UNUSED(result);
|
||||
|
||||
xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message);
|
||||
if (genericEvent->response_type == XCB_KEY_PRESS) {
|
||||
xcb_key_press_event_t *keyEvent = static_cast<xcb_key_press_event_t *>(message);
|
||||
this->activateShortcut({keyEvent->detail, keyEvent->state & QHotkeyPrivateX11::validModsMask});
|
||||
}
|
||||
xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message);
|
||||
if (genericEvent->response_type == XCB_KEY_PRESS) {
|
||||
xcb_key_press_event_t *keyEvent = static_cast<xcb_key_press_event_t *>(message);
|
||||
this->activateShortcut({keyEvent->detail, keyEvent->state & QHotkeyPrivateX11::validModsMask});
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode)
|
||||
{
|
||||
KeySym keysym = XStringToKeysym(QKeySequence(keycode).toString(QKeySequence::NativeText).toLatin1().constData());
|
||||
if (keysym == NoSymbol) {
|
||||
//not found -> just use the key
|
||||
if(keycode <= 0xFFFF)
|
||||
keysym = keycode;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
KeySym keysym = XStringToKeysym(QKeySequence(keycode).toString(QKeySequence::NativeText).toLatin1().constData());
|
||||
if (keysym == NoSymbol) {
|
||||
//not found -> just use the key
|
||||
if(keycode <= 0xFFFF)
|
||||
keysym = keycode;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
Display *display = QX11Info::display();
|
||||
if(display)
|
||||
return XKeysymToKeycode(QX11Info::display(), keysym);
|
||||
else
|
||||
return 0;
|
||||
Display *display = QX11Info::display();
|
||||
if(display)
|
||||
return XKeysymToKeycode(QX11Info::display(), keysym);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||
{
|
||||
quint32 nMods = 0;
|
||||
if (modifiers & Qt::ShiftModifier)
|
||||
nMods |= ShiftMask;
|
||||
if (modifiers & Qt::ControlModifier)
|
||||
nMods |= ControlMask;
|
||||
if (modifiers & Qt::AltModifier)
|
||||
nMods |= Mod1Mask;
|
||||
if (modifiers & Qt::MetaModifier)
|
||||
nMods |= Mod4Mask;
|
||||
return nMods;
|
||||
quint32 nMods = 0;
|
||||
if (modifiers & Qt::ShiftModifier)
|
||||
nMods |= ShiftMask;
|
||||
if (modifiers & Qt::ControlModifier)
|
||||
nMods |= ControlMask;
|
||||
if (modifiers & Qt::AltModifier)
|
||||
nMods |= Mod1Mask;
|
||||
if (modifiers & Qt::MetaModifier)
|
||||
nMods |= Mod4Mask;
|
||||
return nMods;
|
||||
}
|
||||
|
||||
bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||
{
|
||||
Display *display = QX11Info::display();
|
||||
if(!display)
|
||||
return false;
|
||||
Display *display = QX11Info::display();
|
||||
if(!display)
|
||||
return false;
|
||||
|
||||
HotkeyErrorHandler errorHandler;
|
||||
for(quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
|
||||
XGrabKey(display,
|
||||
HotkeyErrorHandler errorHandler;
|
||||
for(quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
|
||||
XGrabKey(display,
|
||||
shortcut.key,
|
||||
shortcut.modifier | specialMod,
|
||||
DefaultRootWindow(display),
|
||||
True,
|
||||
GrabModeAsync,
|
||||
GrabModeAsync);
|
||||
}
|
||||
DefaultRootWindow(display),
|
||||
True,
|
||||
GrabModeAsync,
|
||||
GrabModeAsync);
|
||||
}
|
||||
XFlush(display);
|
||||
|
||||
if(errorHandler.hasError) {
|
||||
qCWarning(logQHotkey) << "[QHotkey] Failed to register hotkey. Error:"
|
||||
if(errorHandler.hasError) {
|
||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
||||
<< qPrintable(errorHandler.errorString);
|
||||
this->unregisterShortcut(shortcut);
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
this->unregisterShortcut(shortcut);
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
||||
{
|
||||
Display *display = QX11Info::display();
|
||||
if(!display)
|
||||
return false;
|
||||
Display *display = QX11Info::display();
|
||||
if(!display)
|
||||
return false;
|
||||
|
||||
HotkeyErrorHandler errorHandler;
|
||||
for(quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
|
||||
XUngrabKey(display,
|
||||
HotkeyErrorHandler errorHandler;
|
||||
for(quint32 specialMod : QHotkeyPrivateX11::specialModifiers) {
|
||||
XUngrabKey(display,
|
||||
shortcut.key,
|
||||
shortcut.modifier | specialMod,
|
||||
DefaultRootWindow(display));
|
||||
}
|
||||
DefaultRootWindow(display));
|
||||
}
|
||||
XFlush(display);
|
||||
|
||||
if(errorHandler.hasError) {
|
||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
||||
<< qPrintable(errorHandler.errorString);
|
||||
this->unregisterShortcut(shortcut);
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
this->unregisterShortcut(shortcut);
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
|
||||
QString QHotkeyPrivateX11::formatX11Error(Display *display, int errorCode)
|
||||
{
|
||||
char errStr[256];
|
||||
XGetErrorText(display, errorCode, errStr, 256);
|
||||
return QString::fromLatin1(errStr);
|
||||
char errStr[256];
|
||||
XGetErrorText(display, errorCode, errStr, 256);
|
||||
return QString::fromLatin1(errStr);
|
||||
}
|
||||
|
||||
|
||||
@@ -155,27 +157,27 @@ QString QHotkeyPrivateX11::HotkeyErrorHandler::errorString;
|
||||
|
||||
QHotkeyPrivateX11::HotkeyErrorHandler::HotkeyErrorHandler()
|
||||
{
|
||||
this->prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
|
||||
this->prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
|
||||
}
|
||||
|
||||
QHotkeyPrivateX11::HotkeyErrorHandler::~HotkeyErrorHandler()
|
||||
{
|
||||
XSetErrorHandler(this->prevHandler);
|
||||
XSetErrorHandler(this->prevHandler);
|
||||
}
|
||||
|
||||
int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorEvent *error)
|
||||
{
|
||||
switch (error->error_code) {
|
||||
case BadAccess:
|
||||
case BadValue:
|
||||
case BadWindow:
|
||||
if (error->request_code == 33 || //grab key
|
||||
error->request_code == 34) {// ungrab key
|
||||
HotkeyErrorHandler::hasError = true;
|
||||
HotkeyErrorHandler::errorString = QHotkeyPrivateX11::formatX11Error(display, error->error_code);
|
||||
return 1;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
switch (error->error_code) {
|
||||
case BadAccess:
|
||||
case BadValue:
|
||||
case BadWindow:
|
||||
if (error->request_code == 33 || //grab key
|
||||
error->request_code == 34) {// ungrab key
|
||||
HotkeyErrorHandler::hasError = true;
|
||||
HotkeyErrorHandler::errorString = QHotkeyPrivateX11::formatX11Error(display, error->error_code);
|
||||
return 1;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,17 @@ 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)
|
||||
- Allows usage of native keycodes and modifiers, if needed
|
||||
|
||||
## 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))
|
||||
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.
|
||||
|
||||
## 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
|
||||
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 +78,7 @@ The documentation was created using [doxygen](http://www.doxygen.org). It includ
|
||||
## Technical
|
||||
### Requirements
|
||||
- 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
|
||||
|
||||
### Known Limitations
|
||||
|
||||
+1
-4
@@ -10,7 +10,7 @@ mac {
|
||||
LIBS += -framework Carbon
|
||||
} else:win32 {
|
||||
SOURCES += $$PWD/QHotkey/qhotkey_win.cpp
|
||||
LIBS += -lUser32
|
||||
LIBS += -luser32
|
||||
} else:unix {
|
||||
SOURCES += $$PWD/QHotkey/qhotkey_x11.cpp
|
||||
QT += x11extras
|
||||
@@ -18,6 +18,3 @@ mac {
|
||||
}
|
||||
|
||||
INCLUDEPATH += $$PWD/QHotkey
|
||||
|
||||
DISTFILES += \
|
||||
$$PWD/QHotkey/qhotkey.pri
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "de.skycoder42.qhotkey",
|
||||
"description": "A global shortcut/hotkey for Desktop Qt-Applications",
|
||||
"author": {
|
||||
"name": "Skycoder",
|
||||
"email": "skycoder42.de@gmx.de"
|
||||
},
|
||||
"repository": {
|
||||
"type": "GITHUB",
|
||||
"url": "https://github.com/Skycoder42/QHotkey.git"
|
||||
},
|
||||
"version": {
|
||||
"label": "1.1.3",
|
||||
"revision": "",
|
||||
"fingerprint": ""
|
||||
},
|
||||
"dependencies": [
|
||||
],
|
||||
"license": "BSD_3_CLAUSE",
|
||||
"pri_filename": "qhotkey.pri",
|
||||
"webpage": "https://github.com/Skycoder42/QHotkey"
|
||||
}
|
||||
Reference in New Issue
Block a user