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 |
@@ -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);
|
||||||
|
|||||||
+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
|
||||||
|
|
||||||
@@ -8,6 +8,14 @@
|
|||||||
|
|
||||||
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),
|
||||||
_keyCode(Qt::Key_unknown),
|
_keyCode(Qt::Key_unknown),
|
||||||
@@ -249,6 +257,11 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut 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;
|
||||||
@@ -279,6 +292,9 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
|
|||||||
|
|
||||||
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
|
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
|
||||||
{
|
{
|
||||||
|
if(mapping.contains({keycode, modifiers}))
|
||||||
|
return mapping.value({keycode, modifiers});
|
||||||
|
|
||||||
bool ok1, ok2 = false;
|
bool ok1, ok2 = false;
|
||||||
auto k = nativeKeycode(keycode, ok1);
|
auto k = nativeKeycode(keycode, ok1);
|
||||||
auto m = nativeModifiers(modifiers, ok2);
|
auto m = nativeModifiers(modifiers, ok2);
|
||||||
|
|||||||
+19
-5
@@ -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,6 +53,8 @@ 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
|
||||||
@@ -95,9 +107,11 @@ private:
|
|||||||
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
|
||||||
|
|||||||
+6
-1
@@ -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
|
||||||
|
|
||||||
@@ -32,8 +32,10 @@ protected:
|
|||||||
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping;
|
||||||
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
|
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 QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
Q_INVOKABLE QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
||||||
@@ -47,4 +49,7 @@ private:
|
|||||||
return hotkeyPrivate;\
|
return hotkeyPrivate;\
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(Qt::Key)
|
||||||
|
Q_DECLARE_METATYPE(Qt::KeyboardModifiers)
|
||||||
|
|
||||||
#endif // QHOTKEY_P_H
|
#endif // QHOTKEY_P_H
|
||||||
|
|||||||
@@ -202,10 +202,14 @@ quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok)
|
|||||||
return VK_OEM_FJ_TOUROKU;
|
return VK_OEM_FJ_TOUROKU;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
if(keycode <= 0xFFFF)
|
||||||
|
return (byte)keycode;
|
||||||
|
else {
|
||||||
ok = false;
|
ok = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
||||||
{
|
{
|
||||||
|
|||||||
+33
-4
@@ -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:
|
||||||
@@ -16,6 +21,7 @@ protected:
|
|||||||
// QHotkeyPrivate interface
|
// QHotkeyPrivate interface
|
||||||
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString QHotkeyPrivateX11::getX11String(Qt::Key keycode)
|
||||||
|
{
|
||||||
|
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)
|
quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok)
|
||||||
{
|
{
|
||||||
KeySym keysym = XStringToKeysym(QKeySequence(keycode).toString(QKeySequence::NativeText).toLatin1().constData());
|
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)
|
||||||
@@ -172,8 +202,6 @@ QHotkeyPrivateX11::HotkeyErrorHandler::~HotkeyErrorHandler()
|
|||||||
|
|
||||||
int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorEvent *error)
|
int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorEvent *error)
|
||||||
{
|
{
|
||||||
QT_WARNING_PUSH
|
|
||||||
QT_WARNING_DISABLE_GCC("-Wimplicit-fallthrough")
|
|
||||||
switch (error->error_code) {
|
switch (error->error_code) {
|
||||||
case BadAccess:
|
case BadAccess:
|
||||||
case BadValue:
|
case BadValue:
|
||||||
@@ -184,8 +212,9 @@ QT_WARNING_DISABLE_GCC("-Wimplicit-fallthrough")
|
|||||||
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;
|
||||||
}
|
}
|
||||||
QT_WARNING_POP
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ 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
|
## Installation
|
||||||
The package is providet as qpm package, [`de.skycoder42.qhotkey`](https://www.qpm.io/packages/de.skycoder42.qhotkey/index.html). To install:
|
The package is providet as qpm package, [`de.skycoder42.qhotkey`](https://www.qpm.io/packages/de.skycoder42.qhotkey/index.html). To install:
|
||||||
|
|
||||||
@@ -90,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
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
CONFIG += C++11
|
CONFIG += C++11
|
||||||
|
|
||||||
PUBLIC_HEADERS += $$PWD/QHotkey/qhotkey.h
|
PUBLIC_HEADERS += $$PWD/QHotkey/qhotkey.h \
|
||||||
|
$$PWD/QHotkey/QHotkey
|
||||||
|
|
||||||
HEADERS += $$PUBLIC_HEADERS \
|
HEADERS += $$PUBLIC_HEADERS \
|
||||||
$$PWD/QHotkey/qhotkey_p.h
|
$$PWD/QHotkey/qhotkey_p.h
|
||||||
|
|||||||
Reference in New Issue
Block a user