mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9cfd8fb6f | ||
|
|
7d214ac41e | ||
|
|
83910c8eca | ||
|
|
91f3542b5d | ||
|
|
1833a053f6 | ||
|
|
f288e85ef1 | ||
|
|
2578b69dc5 | ||
|
|
a0678c4cfc | ||
|
|
cdb4d37453 | ||
|
|
9eace8fbbd | ||
|
|
6baf644b40 | ||
|
|
3fa176a380 |
@@ -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();
|
||||
}
|
||||
|
||||
+7
-1
@@ -279,7 +279,13 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
|
||||
|
||||
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
|
||||
{
|
||||
return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
|
||||
bool ok1, ok2 = false;
|
||||
auto k = nativeKeycode(keycode, ok1);
|
||||
auto m = nativeModifiers(modifiers, ok2);
|
||||
if(ok1 && ok2)
|
||||
return {k, m};
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-7
@@ -13,8 +13,8 @@ public:
|
||||
|
||||
protected:
|
||||
// QHotkeyPrivate interface
|
||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
||||
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||
bool registerShortcut(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;
|
||||
}
|
||||
|
||||
quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
||||
quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode, bool &ok)
|
||||
{
|
||||
// Constants found in NSEvent.h from AppKit.framework
|
||||
ok = true;
|
||||
switch (keycode) {
|
||||
case Qt::Key_Return:
|
||||
return kVK_Return;
|
||||
@@ -120,7 +121,8 @@ quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
||||
case Qt::Key_Up:
|
||||
return kVK_UpArrow;
|
||||
default:
|
||||
;
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
UTF16Char ch = keycode;
|
||||
@@ -157,11 +159,17 @@ quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
||||
long idx = keyToChar[k] & kUCKeyOutputGetIndexMask;
|
||||
if (stateRec && idx < stateRec->keyStateRecordCount) {
|
||||
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) {
|
||||
if (keyToChar[k] == ch) return k;
|
||||
if (keyToChar[k] == ch) {
|
||||
ok = true;
|
||||
return k;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,7 +177,7 @@ quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
quint32 QHotkeyPrivateMac::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||
quint32 QHotkeyPrivateMac::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
||||
{
|
||||
quint32 nMods = 0;
|
||||
if (modifiers & Qt::ShiftModifier)
|
||||
@@ -182,6 +190,7 @@ quint32 QHotkeyPrivateMac::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||
nMods |= controlKey;
|
||||
if (modifiers & Qt::KeypadModifier)
|
||||
nMods |= kEventKeyModifierNumLockMask;
|
||||
ok = true;
|
||||
return nMods;
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -25,8 +25,8 @@ public:
|
||||
protected:
|
||||
void activateShortcut(QHotkey::NativeShortcut shortcut);
|
||||
|
||||
virtual quint32 nativeKeycode(Qt::Key keycode) = 0;//platform implement
|
||||
virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) = 0;//platform implement
|
||||
virtual quint32 nativeKeycode(Qt::Key keycode, bool &ok) = 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 unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||
|
||||
@@ -13,8 +13,8 @@ public:
|
||||
|
||||
protected:
|
||||
// QHotkeyPrivate interface
|
||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
||||
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||
bool registerShortcut(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;
|
||||
}
|
||||
|
||||
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"
|
||||
const SHORT vKey = VkKeyScanW(keycode);
|
||||
if(vKey > -1)
|
||||
@@ -201,11 +202,12 @@ quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode)
|
||||
return VK_OEM_FJ_TOUROKU;
|
||||
|
||||
default:
|
||||
ok = false;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||
quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
||||
{
|
||||
quint32 nMods = 0;
|
||||
if (modifiers & Qt::ShiftModifier)
|
||||
@@ -216,6 +218,7 @@ quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||
nMods |= MOD_ALT;
|
||||
if (modifiers & Qt::MetaModifier)
|
||||
nMods |= MOD_WIN;
|
||||
ok = true;
|
||||
return nMods;
|
||||
}
|
||||
|
||||
|
||||
+22
-12
@@ -14,8 +14,8 @@ public:
|
||||
|
||||
protected:
|
||||
// QHotkeyPrivate interface
|
||||
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
||||
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||
|
||||
@@ -58,7 +58,7 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
|
||||
return false;
|
||||
}
|
||||
|
||||
quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode)
|
||||
quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok)
|
||||
{
|
||||
KeySym keysym = XStringToKeysym(QKeySequence(keycode).toString(QKeySequence::NativeText).toLatin1().constData());
|
||||
if (keysym == NoSymbol) {
|
||||
@@ -70,13 +70,16 @@ quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode)
|
||||
}
|
||||
|
||||
Display *display = QX11Info::display();
|
||||
if(display)
|
||||
return XKeysymToKeycode(QX11Info::display(), keysym);
|
||||
else
|
||||
if(display) {
|
||||
auto res = XKeysymToKeycode(QX11Info::display(), keysym);
|
||||
if(res != 0)
|
||||
ok = true;
|
||||
return res;
|
||||
} else
|
||||
return 0;
|
||||
}
|
||||
|
||||
quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||
quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
|
||||
{
|
||||
quint32 nMods = 0;
|
||||
if (modifiers & Qt::ShiftModifier)
|
||||
@@ -87,6 +90,7 @@ quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||
nMods |= Mod1Mask;
|
||||
if (modifiers & Qt::MetaModifier)
|
||||
nMods |= Mod4Mask;
|
||||
ok = true;
|
||||
return nMods;
|
||||
}
|
||||
|
||||
@@ -106,6 +110,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||
GrabModeAsync,
|
||||
GrabModeAsync);
|
||||
}
|
||||
XSync(display, False);
|
||||
|
||||
if(errorHandler.hasError) {
|
||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
||||
@@ -129,11 +134,11 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
||||
shortcut.modifier | specialMod,
|
||||
DefaultRootWindow(display));
|
||||
}
|
||||
XSync(display, False);
|
||||
|
||||
if(errorHandler.hasError) {
|
||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
||||
<< qPrintable(errorHandler.errorString);
|
||||
this->unregisterShortcut(shortcut);
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
@@ -155,27 +160,32 @@ QString QHotkeyPrivateX11::HotkeyErrorHandler::errorString;
|
||||
|
||||
QHotkeyPrivateX11::HotkeyErrorHandler::HotkeyErrorHandler()
|
||||
{
|
||||
this->prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
|
||||
prevHandler = XSetErrorHandler(&HotkeyErrorHandler::handleError);
|
||||
}
|
||||
|
||||
QHotkeyPrivateX11::HotkeyErrorHandler::~HotkeyErrorHandler()
|
||||
{
|
||||
XSetErrorHandler(this->prevHandler);
|
||||
XSetErrorHandler(prevHandler);
|
||||
hasError = false;
|
||||
errorString.clear();
|
||||
}
|
||||
|
||||
int QHotkeyPrivateX11::HotkeyErrorHandler::handleError(Display *display, XErrorEvent *error)
|
||||
{
|
||||
QT_WARNING_PUSH
|
||||
QT_WARNING_DISABLE_GCC("-Wimplicit-fallthrough")
|
||||
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);
|
||||
hasError = true;
|
||||
errorString = QHotkeyPrivateX11::formatX11Error(display, error->error_code);
|
||||
return 1;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
QT_WARNING_POP
|
||||
}
|
||||
|
||||
@@ -15,12 +15,16 @@ The QHotkey is a class that can be used to create hotkeys/global shortcuts, aka
|
||||
## 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))
|
||||
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
|
||||
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.
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
mac: LIBS += -framework Carbon
|
||||
else:win32: LIBS += -luser32
|
||||
else:unix {
|
||||
QT += x11extras
|
||||
LIBS += -lX11
|
||||
}
|
||||
+8
-12
@@ -1,20 +1,16 @@
|
||||
CONFIG += C++11
|
||||
|
||||
HEADERS += $$PWD/QHotkey/qhotkey.h \
|
||||
PUBLIC_HEADERS += $$PWD/QHotkey/qhotkey.h
|
||||
|
||||
HEADERS += $$PUBLIC_HEADERS \
|
||||
$$PWD/QHotkey/qhotkey_p.h
|
||||
|
||||
SOURCES += $$PWD/QHotkey/qhotkey.cpp
|
||||
|
||||
mac {
|
||||
SOURCES += $$PWD/QHotkey/qhotkey_mac.cpp
|
||||
LIBS += -framework Carbon
|
||||
} else:win32 {
|
||||
SOURCES += $$PWD/QHotkey/qhotkey_win.cpp
|
||||
LIBS += -luser32
|
||||
} else:unix {
|
||||
SOURCES += $$PWD/QHotkey/qhotkey_x11.cpp
|
||||
QT += x11extras
|
||||
LIBS += -lX11
|
||||
}
|
||||
mac: SOURCES += $$PWD/QHotkey/qhotkey_mac.cpp
|
||||
else:win32: SOURCES += $$PWD/QHotkey/qhotkey_win.cpp
|
||||
else:unix: SOURCES += $$PWD/QHotkey/qhotkey_x11.cpp
|
||||
|
||||
INCLUDEPATH += $$PWD/QHotkey
|
||||
|
||||
include($$PWD/qhotkey.prc)
|
||||
|
||||
@@ -1,22 +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.2",
|
||||
"revision": "",
|
||||
"fingerprint": ""
|
||||
},
|
||||
"dependencies": [
|
||||
],
|
||||
"license": "BSD_3_CLAUSE",
|
||||
"pri_filename": "qhotkey.pri",
|
||||
"webpage": "https://github.com/Skycoder42/QHotkey"
|
||||
"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.1",
|
||||
"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