15 Commits
Author SHA1 Message Date
Skycoder42 7d214ac41e qpmx 2017-09-30 19:22:06 +02:00
Skycoder42 83910c8eca fixed invalid key detection 2017-09-30 19:21:26 +02:00
Skycoder42 91f3542b5d qm update 2017-06-25 14:41:23 +02:00
Skycoder42 1833a053f6 fixed xerrorhandler
fixes #4
2017-06-25 14:40:03 +02:00
Felix Barz f288e85ef1 Create README.md 2017-06-15 11:41:09 +02:00
Skycoder42 2578b69dc5 remove recursive call
detected by #4
2017-06-02 21:24:14 +02:00
Skycoder42 a0678c4cfc qpm update 2017-05-14 22:28:10 +02:00
Skycoder42 cdb4d37453 replaced XSync by XFlush 2017-05-14 22:27:26 +02:00
Skycoder42 9eace8fbbd do not discard events on XSync 2017-05-14 13:39:54 +02:00
Skycoder42 6baf644b40 fixed misread parameter
#3
2017-05-14 13:35:13 +02:00
Skycoder42 3fa176a380 Added XSync call for hotkey without window
For #3
2017-05-14 13:32:06 +02:00
Skycoder42 7c4ef6a03a qpm update 2017-05-01 12:17:18 +02:00
Skycoder42 15e34a7dbf fixed initial tab 2017-05-01 12:15:01 +02:00
Felix Barz b8f32c3381 Merge pull request #2 from ArsenArsen/patch-1
Fix a compile error on MXE
2017-05-01 12:13:48 +02:00
ArsenArsen 10d1e44b21 Fix a compile error on MXE 2017-05-01 11:48:53 +02:00
11 changed files with 119 additions and 50 deletions
+1 -1
View File
@@ -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">
+9
View File
@@ -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();
} }
+7 -1
View File
@@ -279,7 +279,13 @@ 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)
{ {
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
View File
@@ -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,11 +159,17 @@ 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;
}
} }
} }
} }
@@ -169,7 +177,7 @@ quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
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;
} }
+2 -2
View File
@@ -25,8 +25,8 @@ 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
+7 -4
View File
@@ -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,12 @@ quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode)
return VK_OEM_FJ_TOUROKU; return VK_OEM_FJ_TOUROKU;
default: default:
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 +218,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;
} }
+22 -12
View File
@@ -14,8 +14,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;
@@ -58,7 +58,7 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
return false; 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()); KeySym keysym = XStringToKeysym(QKeySequence(keycode).toString(QKeySequence::NativeText).toLatin1().constData());
if (keysym == NoSymbol) { if (keysym == NoSymbol) {
@@ -70,13 +70,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 +90,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,6 +110,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
GrabModeAsync, GrabModeAsync,
GrabModeAsync); GrabModeAsync);
} }
XSync(display, False);
if(errorHandler.hasError) { if(errorHandler.hasError) {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:" qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
@@ -129,11 +134,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,27 +160,32 @@ 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)
{ {
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:
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;
} }
default: default:
return 0; return 0;
} }
QT_WARNING_POP
} }
+5 -1
View File
@@ -15,12 +15,16 @@ The QHotkey is a class that can be used to create hotkeys/global shortcuts, aka
## 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:
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` 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 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. 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
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. 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.
+1 -1
View File
@@ -10,7 +10,7 @@ mac {
LIBS += -framework Carbon LIBS += -framework Carbon
} else:win32 { } else:win32 {
SOURCES += $$PWD/QHotkey/qhotkey_win.cpp SOURCES += $$PWD/QHotkey/qhotkey_win.cpp
LIBS += -lUser32 LIBS += -luser32
} else:unix { } else:unix {
SOURCES += $$PWD/QHotkey/qhotkey_x11.cpp SOURCES += $$PWD/QHotkey/qhotkey_x11.cpp
QT += x11extras QT += x11extras
+11 -11
View File
@@ -1,22 +1,22 @@
{ {
"name": "de.skycoder42.qhotkey",
"description": "A global shortcut/hotkey for Desktop Qt-Applications",
"author": { "author": {
"name": "Skycoder", "email": "skycoder42.de@gmx.de",
"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": { "repository": {
"type": "GITHUB", "type": "GITHUB",
"url": "https://github.com/Skycoder42/QHotkey.git" "url": "https://github.com/Skycoder42/QHotkey.git"
}, },
"version": { "version": {
"label": "1.1.1", "fingerprint": "",
"revision": "", "label": "1.2.0",
"fingerprint": "" "revision": ""
}, },
"dependencies": [
],
"license": "BSD_3_CLAUSE",
"pri_filename": "qhotkey.pri",
"webpage": "https://github.com/Skycoder42/QHotkey" "webpage": "https://github.com/Skycoder42/QHotkey"
} }
+28
View File
@@ -0,0 +1,28 @@
{
"dependencies": [
],
"license": {
"file": "LICENSE",
"name": "BSD_3_CLAUSE"
},
"prcFile": "",
"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
}