mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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)
|
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,18 +54,20 @@ if(QHOTKEY_EXAMPLES)
|
|||||||
add_subdirectory(HotkeyTest)
|
add_subdirectory(HotkeyTest)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
if(QHOTKEY_INSTALL)
|
||||||
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey)
|
include(GNUInstallDirs)
|
||||||
|
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey)
|
||||||
|
|
||||||
install(
|
install(
|
||||||
TARGETS qhotkey EXPORT QHotkeyConfig
|
TARGETS qhotkey EXPORT QHotkeyConfig
|
||||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
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()
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
+33
-6
@@ -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;
|
||||||
@@ -244,7 +250,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 +270,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;
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
+30
-3
@@ -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,7 +262,7 @@ 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;
|
||||||
|
|||||||
+25
-2
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user