7 Commits
Author SHA1 Message Date
Skycoder42 c9cfd8fb6f fixed library parts 2017-09-30 22:22:21 +02:00
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
10 changed files with 122 additions and 62 deletions
+7 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
+7 -4
View File
@@ -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 -14
View File
@@ -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,7 +110,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
GrabModeAsync,
GrabModeAsync);
}
XFlush(display);
XSync(display, False);
if(errorHandler.hasError) {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
@@ -130,12 +134,11 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
shortcut.modifier | specialMod,
DefaultRootWindow(display));
}
XFlush(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;
@@ -157,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
}
+5 -1
View File
@@ -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.
+6
View File
@@ -0,0 +1,6 @@
mac: LIBS += -framework Carbon
else:win32: LIBS += -luser32
else:unix {
QT += x11extras
LIBS += -lX11
}
+8 -12
View File
@@ -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)
+21 -21
View File
@@ -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.3",
"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"
}
+28
View File
@@ -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
}