13 Commits
Author SHA1 Message Date
Felix Barz 4f76792098 Merge pull request #40 from Shatur95/improve-error-messages
Improve error messages
2020-07-03 08:04:28 +02:00
Shatur95 483e07365e Improve error messages
* Show information about shortcut.
* Make errors translatable.
2020-07-01 23:16:13 +03:00
Felix Barz c373d83c36 Merge pull request #38 from Shatur95/improve-cmake-rule
Improve CMake rules
2020-06-07 21:00:21 +02:00
Shatur95 055d242ded Improve CMake rules
* Disable building examples by default.
* Use CMAKE_CURRENT_SOURCE_DIR instead of CMAKE_SOURCE_DIR.
* Add an option to disable install rules.
2020-06-06 22:48:44 +03:00
Patrizio Bekerle 0352904ec1 Fix not declared MOD_NOREPEAT (Skycoder42/QHotkey/#36) (#37)
Fix not declared MOD_NOREPEAT (#36)
2020-05-23 07:46:08 +02:00
Felix Barz 7de281c4a6 Merge pull request #34 from mostvexingparse/cmake_include_fixes
fix include path in CMakeLists.txt
2020-05-06 22:05:57 +02:00
Sven Kipry 83a0066760 fix include path in CMakeLists.txt
The include path is not set correctly when QHotkey is build as a
subproject. To fix that CMAKE_CURRENT_SOURCE_DIR should be used instead
of CMAKE_SOURCE_DIR in target_include_directories.
2020-05-05 07:53:57 +02:00
Skycoder42 7028920db5 fix #33 2020-05-02 21:05:04 +02:00
Felix Barz df684dcc2a Merge pull request #32 from alexandrvicente/releasedSignal
Hotkey release signal
2020-04-23 07:47:50 +02:00
Alexandre Vicente e113ace9a1 Indentation fix 2020-04-21 22:10:49 -03:00
Alexandre Vicente 12e05bfd4e Fix missing poll connect on Windows 2020-04-21 22:02:52 -03:00
Alexandre Vicente d32957e52c X11 header fix 2020-04-21 15:48:50 -03:00
Alexandre Vicente 5b507a84df Hotkey release signal 2020-04-21 12:45:10 -03:00
7 changed files with 142 additions and 49 deletions
+7 -4
View File
@@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.1)
project(qhotkey VERSION 1.2.2 LANGUAGES CXX) project(qhotkey VERSION 1.2.2 LANGUAGES CXX)
option(QHOTKEY_EXAMPLES "Build examples" ON) option(QHOTKEY_EXAMPLES "Build examples" OFF)
option(QHOTKEY_INSTALL "Enable install rule" ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@@ -42,7 +43,7 @@ target_link_libraries(qhotkey ${LIBS})
target_include_directories(qhotkey target_include_directories(qhotkey
PUBLIC PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/QHotkey> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/QHotkey>
$<INSTALL_INTERFACE:include>) $<INSTALL_INTERFACE:include>)
set_target_properties(qhotkey PROPERTIES set_target_properties(qhotkey PROPERTIES
@@ -53,6 +54,7 @@ if(QHOTKEY_EXAMPLES)
add_subdirectory(HotkeyTest) add_subdirectory(HotkeyTest)
endif() endif()
if(QHOTKEY_INSTALL)
include(GNUInstallDirs) include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey) set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey)
@@ -62,9 +64,10 @@ install(
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES install(FILES
${CMAKE_SOURCE_DIR}/QHotkey/qhotkey.h ${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/qhotkey.h
${CMAKE_SOURCE_DIR}/QHotkey/QHotkey ${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/QHotkey
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/) DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/)
install(EXPORT QHotkeyConfig DESTINATION ${INSTALL_CONFIGDIR}) install(EXPORT QHotkeyConfig DESTINATION ${INSTALL_CONFIGDIR})
export(TARGETS qhotkey FILE QHotkeyConfig.cmake) export(TARGETS qhotkey FILE QHotkeyConfig.cmake)
endif()
+17 -4
View File
@@ -262,6 +262,13 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
signal.invoke(hkey, Qt::QueuedConnection); signal.invoke(hkey, Qt::QueuedConnection);
} }
void QHotkeyPrivate::releaseShortcut(QHotkey::NativeShortcut shortcut)
{
QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::released);
for(QHotkey *hkey : shortcuts.values(shortcut))
signal.invoke(hkey, Qt::QueuedConnection);
}
void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut) void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut)
{ {
mapping.insert({keycode, modifiers}, nativeShortcut); mapping.insert({keycode, modifiers}, nativeShortcut);
@@ -272,9 +279,11 @@ bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey)
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut; QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;
if(!shortcuts.contains(shortcut)) { if(!shortcuts.contains(shortcut)) {
if(!registerShortcut(shortcut)) if(!registerShortcut(shortcut)) {
qCWarning(logQHotkey) << QHotkey::tr("Failed to register %1. Error: %2").arg(hotkey->shortcut().toString(), error);
return false; return false;
} }
}
shortcuts.insert(shortcut, hotkey); shortcuts.insert(shortcut, hotkey);
hotkey->_registered = true; hotkey->_registered = true;
@@ -289,9 +298,13 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
return false; return false;
hotkey->_registered = false; hotkey->_registered = false;
emit hotkey->registeredChanged(true); emit hotkey->registeredChanged(true);
if(shortcuts.count(shortcut) == 0) if(shortcuts.count(shortcut) == 0) {
return unregisterShortcut(shortcut); if (!unregisterShortcut(shortcut)) {
else qCWarning(logQHotkey) << QHotkey::tr("Failed to unregister %1. Error: %2").arg(hotkey->shortcut().toString(), error);
return false;
} else
return true;
} else
return true; return true;
} }
+3
View File
@@ -100,6 +100,9 @@ signals:
//! Will be emitted if the shortcut is pressed //! Will be emitted if the shortcut is pressed
void activated(QPrivateSignal); void activated(QPrivateSignal);
//! Will be emitted if the shortcut press is released
void released(QPrivateSignal);
//! @notifyAcFn{QHotkey::registered} //! @notifyAcFn{QHotkey::registered}
void registeredChanged(bool registered); void registeredChanged(bool registered);
+35 -10
View File
@@ -9,7 +9,8 @@ public:
// QAbstractNativeEventFilter interface // QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
static OSStatus hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data); static OSStatus hotkeyPressEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
static OSStatus hotkeyReleaseEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
protected: protected:
// QHotkeyPrivate interface // QHotkeyPrivate interface
@@ -203,10 +204,15 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
{ {
if (!this->isHotkeyHandlerRegistered) if (!this->isHotkeyHandlerRegistered)
{ {
EventTypeSpec eventSpec; EventTypeSpec pressEventSpec;
eventSpec.eventClass = kEventClassKeyboard; pressEventSpec.eventClass = kEventClassKeyboard;
eventSpec.eventKind = kEventHotKeyPressed; pressEventSpec.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyEventHandler, 1, &eventSpec, NULL, NULL); InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyPressEventHandler, 1, &pressEventSpec, NULL, NULL);
EventTypeSpec releaseEventSpec;
releaseEventSpec.eventClass = kEventClassKeyboard;
releaseEventSpec.eventKind = kEventHotKeyReleased;
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyReleaseEventHandler, 1, &releaseEventSpec, NULL, NULL);
} }
EventHotKeyID hkeyID; EventHotKeyID hkeyID;
@@ -221,8 +227,7 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
0, 0,
&eventRef); &eventRef);
if (status != noErr) { if (status != noErr) {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:" error = QString::number(status);
<< status;
return false; return false;
} else { } else {
this->hotkeyRefs.insert(shortcut, eventRef); this->hotkeyRefs.insert(shortcut, eventRef);
@@ -235,8 +240,7 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut); EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
OSStatus status = UnregisterEventHotKey(eventRef); OSStatus status = UnregisterEventHotKey(eventRef);
if (status != noErr) { if (status != noErr) {
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:" error = QString::number(status);
<< status;
return false; return false;
} else { } else {
this->hotkeyRefs.remove(shortcut); this->hotkeyRefs.remove(shortcut);
@@ -244,7 +248,7 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
} }
} }
OSStatus QHotkeyPrivateMac::hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data) OSStatus QHotkeyPrivateMac::hotkeyPressEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data)
{ {
Q_UNUSED(nextHandler); Q_UNUSED(nextHandler);
Q_UNUSED(data); Q_UNUSED(data);
@@ -264,3 +268,24 @@ OSStatus QHotkeyPrivateMac::hotkeyEventHandler(EventHandlerCallRef nextHandler,
return noErr; return noErr;
} }
OSStatus QHotkeyPrivateMac::hotkeyReleaseEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data)
{
Q_UNUSED(nextHandler);
Q_UNUSED(data);
if (GetEventClass(event) == kEventClassKeyboard &&
GetEventKind(event) == kEventHotKeyReleased) {
EventHotKeyID hkeyID;
GetEventParameter(event,
kEventParamDirectObject,
typeEventHotKeyID,
NULL,
sizeof(EventHotKeyID),
NULL,
&hkeyID);
hotkeyPrivate->releaseShortcut({hkeyID.signature, hkeyID.id});
}
return noErr;
}
+3
View File
@@ -25,6 +25,7 @@ public:
protected: protected:
void activateShortcut(QHotkey::NativeShortcut shortcut); void activateShortcut(QHotkey::NativeShortcut shortcut);
void releaseShortcut(QHotkey::NativeShortcut shortcut);
virtual quint32 nativeKeycode(Qt::Key keycode, bool &ok) = 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 quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) = 0;//platform implement
@@ -32,6 +33,8 @@ protected:
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
QString error;
private: private:
QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping; QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping;
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts; QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
+32 -7
View File
@@ -2,16 +2,23 @@
#include "qhotkey_p.h" #include "qhotkey_p.h"
#include <qt_windows.h> #include <qt_windows.h>
#include <QDebug> #include <QDebug>
#include <QTimer>
#define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000) #define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000)
#if !defined(MOD_NOREPEAT)
#define MOD_NOREPEAT 0x4000
#endif
class QHotkeyPrivateWin : public QHotkeyPrivate class QHotkeyPrivateWin : public QHotkeyPrivate
{ {
public: public:
QHotkeyPrivateWin();
// QAbstractNativeEventFilter interface // QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
protected: protected:
void pollForHotkeyRelease();
// 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;
@@ -20,9 +27,16 @@ protected:
private: private:
static QString formatWinError(DWORD winError); static QString formatWinError(DWORD winError);
QTimer pollTimer;
QHotkey::NativeShortcut polledShortcut;
}; };
NATIVE_INSTANCE(QHotkeyPrivateWin) NATIVE_INSTANCE(QHotkeyPrivateWin)
QHotkeyPrivateWin::QHotkeyPrivateWin(){
pollTimer.setInterval(50);
connect(&pollTimer, &QTimer::timeout, this, &QHotkeyPrivateWin::pollForHotkeyRelease);
}
bool QHotkeyPrivate::isPlatformSupported() bool QHotkeyPrivate::isPlatformSupported()
{ {
return true; return true;
@@ -34,12 +48,25 @@ bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *mes
Q_UNUSED(result) Q_UNUSED(result)
MSG* msg = static_cast<MSG*>(message); MSG* msg = static_cast<MSG*>(message);
if(msg->message == WM_HOTKEY) if(msg->message == WM_HOTKEY) {
this->activateShortcut({HIWORD(msg->lParam), LOWORD(msg->lParam)}); QHotkey::NativeShortcut shortcut = {HIWORD(msg->lParam), LOWORD(msg->lParam)};
this->activateShortcut(shortcut);
this->polledShortcut = shortcut;
this->pollTimer.start();
}
return false; return false;
} }
void QHotkeyPrivateWin::pollForHotkeyRelease()
{
bool pressed = (GetAsyncKeyState(this->polledShortcut.key) & (1 << 15)) != 0;
if(!pressed) {
this->pollTimer.stop();
this->releaseShortcut(this->polledShortcut);
}
}
quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok) quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok)
{ {
ok = true; ok = true;
@@ -235,13 +262,12 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
{ {
BOOL ok = RegisterHotKey(NULL, BOOL ok = RegisterHotKey(NULL,
HKEY_ID(shortcut), HKEY_ID(shortcut),
shortcut.modifier, shortcut.modifier + MOD_NOREPEAT,
shortcut.key); shortcut.key);
if(ok) if(ok)
return true; return true;
else { else {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:" error = QHotkeyPrivateWin::formatWinError(::GetLastError());
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
return false; return false;
} }
} }
@@ -252,8 +278,7 @@ bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
if(ok) if(ok)
return true; return true;
else { else {
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:" error = QHotkeyPrivateWin::formatWinError(::GetLastError());
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
return false; return false;
} }
} }
+27 -6
View File
@@ -3,6 +3,7 @@
#include <QDebug> #include <QDebug>
#include <QX11Info> #include <QX11Info>
#include <QThreadStorage> #include <QThreadStorage>
#include <QTimer>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
@@ -28,6 +29,9 @@ protected:
private: private:
static const QVector<quint32> specialModifiers; static const QVector<quint32> specialModifiers;
static const quint32 validModsMask; static const quint32 validModsMask;
QTimer *releaseTimer = nullptr;
xcb_key_press_event_t prevHandledEvent;
xcb_key_press_event_t prevEvent;
static QString formatX11Error(Display *display, int errorCode); static QString formatX11Error(Display *display, int errorCode);
@@ -62,8 +66,27 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message); xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message);
if (genericEvent->response_type == XCB_KEY_PRESS) { if (genericEvent->response_type == XCB_KEY_PRESS) {
xcb_key_press_event_t *keyEvent = static_cast<xcb_key_press_event_t *>(message); xcb_key_press_event_t keyEvent = *static_cast<xcb_key_press_event_t *>(message);
this->activateShortcut({keyEvent->detail, keyEvent->state & QHotkeyPrivateX11::validModsMask}); this->prevEvent = keyEvent;
if (this->prevHandledEvent.response_type == XCB_KEY_RELEASE) {
if(this->prevHandledEvent.time == keyEvent.time) return false;
}
this->activateShortcut({keyEvent.detail, keyEvent.state & QHotkeyPrivateX11::validModsMask});
} else if (genericEvent->response_type == XCB_KEY_RELEASE) {
xcb_key_release_event_t keyEvent = *static_cast<xcb_key_release_event_t *>(message);
this->prevEvent = keyEvent;
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
timer->setInterval(50);
connect(timer, &QTimer::timeout, this, [this, keyEvent, timer] {
if(this->prevEvent.time == keyEvent.time && this->prevEvent.response_type == keyEvent.response_type && this->prevEvent.detail == keyEvent.detail){
this->releaseShortcut({keyEvent.detail, keyEvent.state & QHotkeyPrivateX11::validModsMask});
}
delete timer;
});
timer->start();
this->releaseTimer = timer;
this->prevHandledEvent = keyEvent;
} }
return false; return false;
@@ -147,8 +170,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
XSync(display, False); XSync(display, False);
if(errorHandler.hasError) { if(errorHandler.hasError) {
qCWarning(logQHotkey) << "Failed to register hotkey. Error:" error = errorHandler.errorString;
<< qPrintable(errorHandler.errorString);
this->unregisterShortcut(shortcut); this->unregisterShortcut(shortcut);
return false; return false;
} else } else
@@ -171,8 +193,7 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
XSync(display, False); XSync(display, False);
if(errorHandler.hasError) { if(errorHandler.hasError) {
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:" error = errorHandler.errorString;
<< qPrintable(errorHandler.errorString);
return false; return false;
} else } else
return true; return true;