mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f76792098 | ||
|
|
483e07365e | ||
|
|
c373d83c36 | ||
|
|
055d242ded | ||
|
|
0352904ec1 | ||
|
|
7de281c4a6 | ||
|
|
83a0066760 | ||
|
|
7028920db5 | ||
|
|
df684dcc2a | ||
|
|
e113ace9a1 | ||
|
|
12e05bfd4e | ||
|
|
d32957e52c | ||
|
|
5b507a84df |
+13
-10
@@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.1)
|
||||
|
||||
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)
|
||||
|
||||
@@ -42,7 +43,7 @@ target_link_libraries(qhotkey ${LIBS})
|
||||
|
||||
target_include_directories(qhotkey
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/QHotkey>
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/QHotkey>
|
||||
$<INSTALL_INTERFACE:include>)
|
||||
|
||||
set_target_properties(qhotkey PROPERTIES
|
||||
@@ -53,18 +54,20 @@ if(QHOTKEY_EXAMPLES)
|
||||
add_subdirectory(HotkeyTest)
|
||||
endif()
|
||||
|
||||
include(GNUInstallDirs)
|
||||
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey)
|
||||
if(QHOTKEY_INSTALL)
|
||||
include(GNUInstallDirs)
|
||||
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey)
|
||||
|
||||
install(
|
||||
install(
|
||||
TARGETS qhotkey EXPORT QHotkeyConfig
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
install(FILES
|
||||
${CMAKE_SOURCE_DIR}/QHotkey/qhotkey.h
|
||||
${CMAKE_SOURCE_DIR}/QHotkey/QHotkey
|
||||
install(FILES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/qhotkey.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/QHotkey
|
||||
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
@@ -262,6 +262,13 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
|
||||
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)
|
||||
{
|
||||
mapping.insert({keycode, modifiers}, nativeShortcut);
|
||||
@@ -272,9 +279,11 @@ bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey)
|
||||
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
shortcuts.insert(shortcut, hotkey);
|
||||
hotkey->_registered = true;
|
||||
@@ -289,9 +298,13 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
|
||||
return false;
|
||||
hotkey->_registered = false;
|
||||
emit hotkey->registeredChanged(true);
|
||||
if(shortcuts.count(shortcut) == 0)
|
||||
return unregisterShortcut(shortcut);
|
||||
else
|
||||
if(shortcuts.count(shortcut) == 0) {
|
||||
if (!unregisterShortcut(shortcut)) {
|
||||
qCWarning(logQHotkey) << QHotkey::tr("Failed to unregister %1. Error: %2").arg(hotkey->shortcut().toString(), error);
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -100,6 +100,9 @@ signals:
|
||||
//! Will be emitted if the shortcut is pressed
|
||||
void activated(QPrivateSignal);
|
||||
|
||||
//! Will be emitted if the shortcut press is released
|
||||
void released(QPrivateSignal);
|
||||
|
||||
//! @notifyAcFn{QHotkey::registered}
|
||||
void registeredChanged(bool registered);
|
||||
|
||||
|
||||
+35
-10
@@ -9,7 +9,8 @@ public:
|
||||
// QAbstractNativeEventFilter interface
|
||||
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:
|
||||
// QHotkeyPrivate interface
|
||||
@@ -203,10 +204,15 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||
{
|
||||
if (!this->isHotkeyHandlerRegistered)
|
||||
{
|
||||
EventTypeSpec eventSpec;
|
||||
eventSpec.eventClass = kEventClassKeyboard;
|
||||
eventSpec.eventKind = kEventHotKeyPressed;
|
||||
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyEventHandler, 1, &eventSpec, NULL, NULL);
|
||||
EventTypeSpec pressEventSpec;
|
||||
pressEventSpec.eventClass = kEventClassKeyboard;
|
||||
pressEventSpec.eventKind = kEventHotKeyPressed;
|
||||
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyPressEventHandler, 1, &pressEventSpec, NULL, NULL);
|
||||
|
||||
EventTypeSpec releaseEventSpec;
|
||||
releaseEventSpec.eventClass = kEventClassKeyboard;
|
||||
releaseEventSpec.eventKind = kEventHotKeyReleased;
|
||||
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyReleaseEventHandler, 1, &releaseEventSpec, NULL, NULL);
|
||||
}
|
||||
|
||||
EventHotKeyID hkeyID;
|
||||
@@ -221,8 +227,7 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||
0,
|
||||
&eventRef);
|
||||
if (status != noErr) {
|
||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
||||
<< status;
|
||||
error = QString::number(status);
|
||||
return false;
|
||||
} else {
|
||||
this->hotkeyRefs.insert(shortcut, eventRef);
|
||||
@@ -235,8 +240,7 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
||||
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
|
||||
OSStatus status = UnregisterEventHotKey(eventRef);
|
||||
if (status != noErr) {
|
||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
||||
<< status;
|
||||
error = QString::number(status);
|
||||
return false;
|
||||
} else {
|
||||
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(data);
|
||||
@@ -264,3 +268,24 @@ OSStatus QHotkeyPrivateMac::hotkeyEventHandler(EventHandlerCallRef nextHandler,
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -25,6 +25,7 @@ public:
|
||||
|
||||
protected:
|
||||
void activateShortcut(QHotkey::NativeShortcut shortcut);
|
||||
void releaseShortcut(QHotkey::NativeShortcut shortcut);
|
||||
|
||||
virtual quint32 nativeKeycode(Qt::Key keycode, 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 unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||
|
||||
QString error;
|
||||
|
||||
private:
|
||||
QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping;
|
||||
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
|
||||
|
||||
+32
-7
@@ -2,16 +2,23 @@
|
||||
#include "qhotkey_p.h"
|
||||
#include <qt_windows.h>
|
||||
#include <QDebug>
|
||||
#include <QTimer>
|
||||
|
||||
#define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000)
|
||||
|
||||
#if !defined(MOD_NOREPEAT)
|
||||
#define MOD_NOREPEAT 0x4000
|
||||
#endif
|
||||
|
||||
class QHotkeyPrivateWin : public QHotkeyPrivate
|
||||
{
|
||||
public:
|
||||
QHotkeyPrivateWin();
|
||||
// QAbstractNativeEventFilter interface
|
||||
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
|
||||
|
||||
protected:
|
||||
void pollForHotkeyRelease();
|
||||
// QHotkeyPrivate interface
|
||||
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||
@@ -20,9 +27,16 @@ protected:
|
||||
|
||||
private:
|
||||
static QString formatWinError(DWORD winError);
|
||||
QTimer pollTimer;
|
||||
QHotkey::NativeShortcut polledShortcut;
|
||||
};
|
||||
NATIVE_INSTANCE(QHotkeyPrivateWin)
|
||||
|
||||
QHotkeyPrivateWin::QHotkeyPrivateWin(){
|
||||
pollTimer.setInterval(50);
|
||||
connect(&pollTimer, &QTimer::timeout, this, &QHotkeyPrivateWin::pollForHotkeyRelease);
|
||||
}
|
||||
|
||||
bool QHotkeyPrivate::isPlatformSupported()
|
||||
{
|
||||
return true;
|
||||
@@ -34,12 +48,25 @@ bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *mes
|
||||
Q_UNUSED(result)
|
||||
|
||||
MSG* msg = static_cast<MSG*>(message);
|
||||
if(msg->message == WM_HOTKEY)
|
||||
this->activateShortcut({HIWORD(msg->lParam), LOWORD(msg->lParam)});
|
||||
if(msg->message == WM_HOTKEY) {
|
||||
QHotkey::NativeShortcut shortcut = {HIWORD(msg->lParam), LOWORD(msg->lParam)};
|
||||
this->activateShortcut(shortcut);
|
||||
this->polledShortcut = shortcut;
|
||||
this->pollTimer.start();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
ok = true;
|
||||
@@ -235,13 +262,12 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||
{
|
||||
BOOL ok = RegisterHotKey(NULL,
|
||||
HKEY_ID(shortcut),
|
||||
shortcut.modifier,
|
||||
shortcut.modifier + MOD_NOREPEAT,
|
||||
shortcut.key);
|
||||
if(ok)
|
||||
return true;
|
||||
else {
|
||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
||||
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
|
||||
error = QHotkeyPrivateWin::formatWinError(::GetLastError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -252,8 +278,7 @@ bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
||||
if(ok)
|
||||
return true;
|
||||
else {
|
||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
||||
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
|
||||
error = QHotkeyPrivateWin::formatWinError(::GetLastError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+27
-6
@@ -3,6 +3,7 @@
|
||||
#include <QDebug>
|
||||
#include <QX11Info>
|
||||
#include <QThreadStorage>
|
||||
#include <QTimer>
|
||||
#include <X11/Xlib.h>
|
||||
#include <xcb/xcb.h>
|
||||
|
||||
@@ -28,6 +29,9 @@ protected:
|
||||
private:
|
||||
static const QVector<quint32> specialModifiers;
|
||||
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);
|
||||
|
||||
@@ -62,8 +66,27 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
|
||||
|
||||
xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message);
|
||||
if (genericEvent->response_type == XCB_KEY_PRESS) {
|
||||
xcb_key_press_event_t *keyEvent = static_cast<xcb_key_press_event_t *>(message);
|
||||
this->activateShortcut({keyEvent->detail, keyEvent->state & QHotkeyPrivateX11::validModsMask});
|
||||
xcb_key_press_event_t keyEvent = *static_cast<xcb_key_press_event_t *>(message);
|
||||
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;
|
||||
@@ -147,8 +170,7 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||
XSync(display, False);
|
||||
|
||||
if(errorHandler.hasError) {
|
||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
||||
<< qPrintable(errorHandler.errorString);
|
||||
error = errorHandler.errorString;
|
||||
this->unregisterShortcut(shortcut);
|
||||
return false;
|
||||
} else
|
||||
@@ -171,8 +193,7 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
||||
XSync(display, False);
|
||||
|
||||
if(errorHandler.hasError) {
|
||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
||||
<< qPrintable(errorHandler.errorString);
|
||||
error = errorHandler.errorString;
|
||||
return false;
|
||||
} else
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user