23 Commits
Author SHA1 Message Date
Felix Barz eb7ddaba57 Merge pull request #47 from Shatur95/fix-clang-tidy-warnings
Fix Clang Tidy warnings
2020-10-21 07:10:48 +02:00
Shatur95 8584ff1b15 Use QTimer::singleShort
https://doc.qt.io/qt-5/qtimer.html#singleShot-4
2020-10-20 13:33:22 +03:00
Shatur95 384f780b28 Use QStringLiteral 2020-10-17 01:08:08 +03:00
Shatur95 4bb901a41e Pass NativeShortcut by value (trivially copyable) 2020-10-17 01:07:57 +03:00
Shatur95 2a87965ed6 Add mising override 2020-10-15 22:12:11 +03:00
Shatur95 f0799fd4a4 Fix Clang Tidy warnings
readability-convert-member-functions-to-static
modernize-use-auto
readability-else-after-return
readability-static-accessed-through-instance
readability-inconsistent-declaration-parameter-name
2020-10-15 21:50:58 +03:00
Felix Barz 53745ba936 Merge pull request #46 from Shatur95/fix-implicit-conversion-warning
Fix implicit conversion warning
2020-10-15 08:33:24 +02:00
Shatur95 b73b4955ae Fix implicit conversion warning 2020-10-15 01:31:52 +03:00
Felix Barz bd35d8b07a Merge pull request #44 from olegantonyan/fix-wayland-crash
fix SIGSEGV on wayland
2020-08-21 08:56:54 +02:00
Oleg Antonyan 941cb2f15f fix SIGSEGV on wayland 2020-08-18 20:43:44 +02:00
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 203 additions and 123 deletions
+18 -15
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,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()
+59 -52
View File
@@ -8,7 +8,7 @@
Q_LOGGING_CATEGORY(logQHotkey, "QHotkey") Q_LOGGING_CATEGORY(logQHotkey, "QHotkey")
void QHotkey::addGlobalMapping(const QKeySequence &shortcut, const QHotkey::NativeShortcut &nativeShortcut) void QHotkey::addGlobalMapping(const QKeySequence &shortcut, QHotkey::NativeShortcut nativeShortcut)
{ {
QMetaObject::invokeMethod(QHotkeyPrivate::instance(), "addMappingInvoked", Qt::QueuedConnection, QMetaObject::invokeMethod(QHotkeyPrivate::instance(), "addMappingInvoked", Qt::QueuedConnection,
Q_ARG(Qt::Key, Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask)), Q_ARG(Qt::Key, Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask)),
@@ -25,14 +25,13 @@ QHotkey::QHotkey(QObject *parent) :
QObject(parent), QObject(parent),
_keyCode(Qt::Key_unknown), _keyCode(Qt::Key_unknown),
_modifiers(Qt::NoModifier), _modifiers(Qt::NoModifier),
_nativeShortcut(),
_registered(false) _registered(false)
{} {}
QHotkey::QHotkey(const QKeySequence &sequence, bool autoRegister, QObject *parent) : QHotkey::QHotkey(const QKeySequence &shortcut, bool autoRegister, QObject *parent) :
QHotkey(parent) QHotkey(parent)
{ {
setShortcut(sequence, autoRegister); setShortcut(shortcut, autoRegister);
} }
QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) : QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
@@ -41,7 +40,7 @@ QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegi
setShortcut(keyCode, modifiers, autoRegister); setShortcut(keyCode, modifiers, autoRegister);
} }
QHotkey::QHotkey(const QHotkey::NativeShortcut &shortcut, bool autoRegister, QObject *parent) : QHotkey::QHotkey(QHotkey::NativeShortcut shortcut, bool autoRegister, QObject *parent) :
QHotkey(parent) QHotkey(parent)
{ {
setNativeShortcut(shortcut, autoRegister); setNativeShortcut(shortcut, autoRegister);
@@ -57,8 +56,7 @@ QKeySequence QHotkey::shortcut() const
{ {
if(_keyCode == Qt::Key_unknown) if(_keyCode == Qt::Key_unknown)
return QKeySequence(); return QKeySequence();
else return QKeySequence(static_cast<int>(_keyCode | _modifiers));
return QKeySequence(_keyCode | _modifiers);
} }
Qt::Key QHotkey::keyCode() const Qt::Key QHotkey::keyCode() const
@@ -83,9 +81,9 @@ bool QHotkey::isRegistered() const
bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister) bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
{ {
if(shortcut.isEmpty()) { if(shortcut.isEmpty())
return resetShortcut(); return resetShortcut();
} else if(shortcut.count() > 1) { if(shortcut.count() > 1) {
qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! " qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
"Only the first shortcut will be used!"); "Only the first shortcut will be used!");
} }
@@ -118,15 +116,14 @@ bool QHotkey::setShortcut(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool
if(_nativeShortcut.isValid()) { if(_nativeShortcut.isValid()) {
if(autoRegister) if(autoRegister)
return QHotkeyPrivate::instance()->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
else return true;
return true;
} else {
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers;
_keyCode = Qt::Key_unknown;
_modifiers = Qt::NoModifier;
_nativeShortcut = NativeShortcut();
return false;
} }
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers;
_keyCode = Qt::Key_unknown;
_modifiers = Qt::NoModifier;
_nativeShortcut = NativeShortcut();
return false;
} }
bool QHotkey::resetShortcut() bool QHotkey::resetShortcut()
@@ -158,35 +155,32 @@ bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool aut
_nativeShortcut = nativeShortcut; _nativeShortcut = nativeShortcut;
if(autoRegister) if(autoRegister)
return QHotkeyPrivate::instance()->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
else
return true;
} else {
_keyCode = Qt::Key_unknown;
_modifiers = Qt::NoModifier;
_nativeShortcut = NativeShortcut();
return true; return true;
} }
_keyCode = Qt::Key_unknown;
_modifiers = Qt::NoModifier;
_nativeShortcut = NativeShortcut();
return true;
} }
bool QHotkey::setRegistered(bool registered) bool QHotkey::setRegistered(bool registered)
{ {
if(_registered && !registered) if(_registered && !registered)
return QHotkeyPrivate::instance()->removeShortcut(this); return QHotkeyPrivate::instance()->removeShortcut(this);
else if(!_registered && registered) { if(!_registered && registered) {
if(!_nativeShortcut.isValid()) if(!_nativeShortcut.isValid())
return false; return false;
else return QHotkeyPrivate::instance()->addShortcut(this);
return QHotkeyPrivate::instance()->addShortcut(this); }
} else return true;
return true;
} }
// ---------- QHotkeyPrivate implementation ---------- // ---------- QHotkeyPrivate implementation ----------
QHotkeyPrivate::QHotkeyPrivate() : QHotkeyPrivate::QHotkeyPrivate()
shortcuts()
{ {
Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated"); Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated");
qApp->eventDispatcher()->installNativeEventFilter(this); qApp->eventDispatcher()->installNativeEventFilter(this);
@@ -211,8 +205,8 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb
Q_ARG(Qt::Key, keycode), Q_ARG(Qt::Key, keycode),
Q_ARG(Qt::KeyboardModifiers, modifiers))) { Q_ARG(Qt::KeyboardModifiers, modifiers))) {
return QHotkey::NativeShortcut(); return QHotkey::NativeShortcut();
} else }
return res; return res;
} }
bool QHotkeyPrivate::addShortcut(QHotkey *hotkey) bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
@@ -228,11 +222,11 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
Q_RETURN_ARG(bool, res), Q_RETURN_ARG(bool, res),
Q_ARG(QHotkey*, hotkey))) { Q_ARG(QHotkey*, hotkey))) {
return false; return false;
} else {
if(res)
emit hotkey->registeredChanged(true);
return res;
} }
if(res)
emit hotkey->registeredChanged(true);
return res;
} }
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey) bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
@@ -248,11 +242,11 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
Q_RETURN_ARG(bool, res), Q_RETURN_ARG(bool, res),
Q_ARG(QHotkey*, hotkey))) { Q_ARG(QHotkey*, hotkey))) {
return false; return false;
} else {
if(res)
emit hotkey->registeredChanged(false);
return res;
} }
if(res)
emit hotkey->registeredChanged(false);
return res;
} }
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut) void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
@@ -262,7 +256,14 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
signal.invoke(hkey, Qt::QueuedConnection); signal.invoke(hkey, Qt::QueuedConnection);
} }
void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut) 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, QHotkey::NativeShortcut nativeShortcut)
{ {
mapping.insert({keycode, modifiers}, nativeShortcut); mapping.insert({keycode, modifiers}, nativeShortcut);
} }
@@ -272,8 +273,10 @@ 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);
@@ -289,10 +292,14 @@ 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;
}
return true; return true;
}
return true;
} }
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers) QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
@@ -300,13 +307,13 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Q
if(mapping.contains({keycode, modifiers})) if(mapping.contains({keycode, modifiers}))
return mapping.value({keycode, modifiers}); return mapping.value({keycode, modifiers});
bool ok1, ok2 = false; bool ok1 = false;
auto k = nativeKeycode(keycode, ok1); auto k = nativeKeycode(keycode, ok1);
bool ok2 = false;
auto m = nativeModifiers(modifiers, ok2); auto m = nativeModifiers(modifiers, ok2);
if(ok1 && ok2) if(ok1 && ok2)
return {k, m}; return {k, m};
else return {};
return {};
} }
@@ -328,26 +335,26 @@ bool QHotkey::NativeShortcut::isValid() const
return valid; return valid;
} }
bool QHotkey::NativeShortcut::operator ==(const QHotkey::NativeShortcut &other) const bool QHotkey::NativeShortcut::operator ==(QHotkey::NativeShortcut other) const
{ {
return (key == other.key) && return (key == other.key) &&
(modifier == other.modifier) && (modifier == other.modifier) &&
valid == other.valid; valid == other.valid;
} }
bool QHotkey::NativeShortcut::operator !=(const QHotkey::NativeShortcut &other) const bool QHotkey::NativeShortcut::operator !=(QHotkey::NativeShortcut other) const
{ {
return (key != other.key) || return (key != other.key) ||
(modifier != other.modifier) || (modifier != other.modifier) ||
valid != other.valid; valid != other.valid;
} }
uint qHash(const QHotkey::NativeShortcut &key) uint qHash(QHotkey::NativeShortcut key)
{ {
return qHash(key.key) ^ qHash(key.modifier); return qHash(key.key) ^ qHash(key.modifier);
} }
uint qHash(const QHotkey::NativeShortcut &key, uint seed) uint qHash(QHotkey::NativeShortcut key, uint seed)
{ {
return qHash(key.key, seed) ^ qHash(key.modifier, seed); return qHash(key.key, seed) ^ qHash(key.modifier, seed);
} }
+11 -8
View File
@@ -46,16 +46,16 @@ public:
bool isValid() const; bool isValid() const;
//! Equality operator //! Equality operator
bool operator ==(const NativeShortcut &other) const; bool operator ==(NativeShortcut other) const;
//! Inequality operator //! Inequality operator
bool operator !=(const NativeShortcut &other) const; bool operator !=(NativeShortcut other) const;
private: private:
bool valid; bool valid;
}; };
//! Adds a global mapping of a key sequence to a replacement native shortcut //! Adds a global mapping of a key sequence to a replacement native shortcut
static void addGlobalMapping(const QKeySequence &shortcut, const NativeShortcut &nativeShortcut); static void addGlobalMapping(const QKeySequence &shortcut, NativeShortcut nativeShortcut);
//! Checks if global shortcuts are supported by the current platform //! Checks if global shortcuts are supported by the current platform
static bool isPlatformSupported(); static bool isPlatformSupported();
@@ -67,8 +67,8 @@ public:
//! Constructs a hotkey with a key and modifiers and optionally registers it //! Constructs a hotkey with a key and modifiers and optionally registers it
explicit QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister = false, QObject *parent = nullptr); explicit QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister = false, QObject *parent = nullptr);
//! Constructs a hotkey from a native shortcut and optionally registers it //! Constructs a hotkey from a native shortcut and optionally registers it
explicit QHotkey(const NativeShortcut &shortcut, bool autoRegister = false, QObject *parent = nullptr); explicit QHotkey(NativeShortcut shortcut, bool autoRegister = false, QObject *parent = nullptr);
~QHotkey(); ~QHotkey() override;
//! @readAcFn{QHotkey::registered} //! @readAcFn{QHotkey::registered}
bool isRegistered() const; bool isRegistered() const;
@@ -94,12 +94,15 @@ public slots:
bool resetShortcut(); bool resetShortcut();
//! Set this hotkey to a native shortcut //! Set this hotkey to a native shortcut
bool setNativeShortcut(NativeShortcut nativeShortcut, bool autoRegister = false); bool setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool autoRegister = false);
signals: 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);
@@ -111,8 +114,8 @@ private:
bool _registered; bool _registered;
}; };
uint QHOTKEY_SHARED_EXPORT qHash(const QHotkey::NativeShortcut &key); uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key);
uint QHOTKEY_SHARED_EXPORT qHash(const QHotkey::NativeShortcut &key, uint seed); uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key, uint seed);
QHOTKEY_SHARED_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey) QHOTKEY_SHARED_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
+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;
}
+4 -1
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,11 +33,13 @@ 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;
Q_INVOKABLE void addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut); Q_INVOKABLE void addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, QHotkey::NativeShortcut nativeShortcut);
Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey); Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey);
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey); Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
Q_INVOKABLE QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers); Q_INVOKABLE QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
+33 -8
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,17 +48,30 @@ 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;
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(static_cast<WCHAR>(keycode));
if(vKey > -1) if(vKey > -1)
return LOBYTE(vKey); return LOBYTE(vKey);
} }
@@ -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;
} }
} }
+36 -22
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>
@@ -21,13 +22,15 @@ protected:
// 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;
QString getX11String(Qt::Key keycode); static QString getX11String(Qt::Key keycode);
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;
private: private:
static const QVector<quint32> specialModifiers; static const QVector<quint32> specialModifiers;
static const quint32 validModsMask; static const quint32 validModsMask;
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);
@@ -60,10 +63,23 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
Q_UNUSED(eventType) Q_UNUSED(eventType)
Q_UNUSED(result) Q_UNUSED(result)
xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message); auto *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::singleShot(50, [this, keyEvent] {
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});
}
});
this->prevHandledEvent = keyEvent;
} }
return false; return false;
@@ -75,17 +91,17 @@ QString QHotkeyPrivateX11::getX11String(Qt::Key keycode)
case Qt::Key_MediaLast : case Qt::Key_MediaLast :
case Qt::Key_MediaPrevious : case Qt::Key_MediaPrevious :
return "XF86AudioPrev"; return QStringLiteral("XF86AudioPrev");
case Qt::Key_MediaNext : case Qt::Key_MediaNext :
return "XF86AudioNext"; return QStringLiteral("XF86AudioNext");
case Qt::Key_MediaPause : case Qt::Key_MediaPause :
case Qt::Key_MediaPlay : case Qt::Key_MediaPlay :
case Qt::Key_MediaTogglePlayPause : case Qt::Key_MediaTogglePlayPause :
return "XF86AudioPlay"; return QStringLiteral("XF86AudioPlay");
case Qt::Key_MediaRecord : case Qt::Key_MediaRecord :
return "XF86AudioRecord"; return QStringLiteral("XF86AudioRecord");
case Qt::Key_MediaStop : case Qt::Key_MediaStop :
return "XF86AudioStop"; return QStringLiteral("XF86AudioStop");
default : default :
return QKeySequence(keycode).toString(QKeySequence::NativeText); return QKeySequence(keycode).toString(QKeySequence::NativeText);
} }
@@ -109,8 +125,8 @@ quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok)
if(res != 0) if(res != 0)
ok = true; ok = true;
return res; return res;
} else }
return 0; return 0;
} }
quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok)
@@ -131,7 +147,7 @@ quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool
bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
{ {
Display *display = QX11Info::display(); Display *display = QX11Info::display();
if(!display) if(!display || !QX11Info::isPlatformX11())
return false; return false;
HotkeyErrorHandler errorHandler; HotkeyErrorHandler errorHandler;
@@ -147,12 +163,11 @@ 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 }
return true; return true;
} }
bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut) bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
@@ -166,16 +181,15 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
XUngrabKey(display, XUngrabKey(display,
shortcut.key, shortcut.key,
shortcut.modifier | specialMod, shortcut.modifier | specialMod,
DefaultRootWindow(display)); XDefaultRootWindow(display));
} }
XSync(display, False); XSync(display, False);
if(errorHandler.hasError) { if(HotkeyErrorHandler::hasError) {
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:" error = HotkeyErrorHandler::errorString;
<< qPrintable(errorHandler.errorString);
return false; return false;
} else }
return true; return true;
} }
QString QHotkeyPrivateX11::formatX11Error(Display *display, int errorCode) QString QHotkeyPrivateX11::formatX11Error(Display *display, int errorCode)