diff --git a/HotkeyTest/hottestwidget.h b/HotkeyTest/hottestwidget.h
index abdde3d..3152e9f 100644
--- a/HotkeyTest/hottestwidget.h
+++ b/HotkeyTest/hottestwidget.h
@@ -40,6 +40,8 @@ private slots:
void on_threadEnableCheckBox_clicked();
+ void on_loggingCheckBox_clicked(bool checked);
+
private:
Ui::HotTestWidget *ui;
diff --git a/HotkeyTest/hottestwidget.ui b/HotkeyTest/hottestwidget.ui
index adb6e4c..745422f 100644
--- a/HotkeyTest/hottestwidget.ui
+++ b/HotkeyTest/hottestwidget.ui
@@ -31,13 +31,10 @@
- -
-
-
- -
-
+
-
+
- Count:
+ Hotkey &2:
@@ -151,6 +148,16 @@
+ -
+
+
+ -
+
+
+ Count:
+
+
+
-
@@ -158,23 +165,6 @@
- -
-
-
- Hotkey &2:
-
-
-
- -
-
-
- -
-
-
- Count:
-
-
-
-
@@ -285,13 +275,6 @@
- -
-
-
- Qt::LeftArrow
-
-
-
-
@@ -299,16 +282,33 @@
- -
-
+
-
+
- -
-
+
-
+
Count:
+ -
+
+
+ Qt::LeftArrow
+
+
+
+ -
+
+
+ Hotkey &4:
+
+
+
+ -
+
+
-
@@ -419,6 +419,13 @@
+ -
+
+
+ Count:
+
+
+
-
@@ -426,20 +433,20 @@
- -
-
+
-
+
- Hotkey &4:
+ Count:
-
- -
-
-
- Count:
+
-
+
+
+ Qt::LeftArrow
@@ -553,13 +560,6 @@
- -
-
-
- Qt::LeftArrow
-
-
-
-
@@ -567,8 +567,31 @@
- -
-
+
-
+
+
+ Qt::Vertical
+
+
+
+ 20
+ 41
+
+
+
+
+ -
+
+
+ Qt::Vertical
+
+
+
+ 20
+ 41
+
+
+
-
@@ -694,31 +717,8 @@
- -
-
-
- Qt::Vertical
-
-
-
- 20
- 41
-
-
-
-
- -
-
-
- Qt::Vertical
-
-
-
- 20
- 41
-
-
-
+
-
+
-
@@ -733,8 +733,8 @@
- -
-
+
-
+
Qt::Vertical
@@ -746,8 +746,8 @@
- -
-
+
-
+
Qt::Vertical
diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp
index 4086779..dc5ce50 100644
--- a/QHotkey/qhotkey.cpp
+++ b/QHotkey/qhotkey.cpp
@@ -6,6 +6,8 @@
#include
#include
+Q_LOGGING_CATEGORY(logQHotkey, "[QHotkey]")
+
QHotkey::QHotkey(QObject *parent) :
QObject(parent),
key(Qt::Key_unknown),
@@ -65,8 +67,8 @@ bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
if(shortcut.isEmpty()) {
return this->resetShortcut();
} else if(shortcut.count() > 1) {
- qWarning("QHotkey: Keysequences with multiple shortcuts are not allowed! "
- "Only the first shortcut will be used!");
+ qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
+ "Only the first shortcut will be used!");
}
return this->setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
@@ -80,10 +82,8 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
if(autoRegister) {
if(!QHotkeyPrivate::instance()->removeShortcut(this))
return false;
- } else {
- qWarning("QHotkey: Can't change the shortcut while the hotkey is registered");
+ } else
return false;
- }
}
this->key = key;
@@ -95,7 +95,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
else
return true;
} else {
- qWarning() << "QHotkey: Unable to map shortcut to native keys. Key:" << key << "Modifiers:" << modifiers;
+ qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << key << "Modifiers:" << modifiers;
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeShortcut = NativeShortcut();
@@ -107,7 +107,6 @@ bool QHotkey::resetShortcut()
{
if(this->registered &&
!QHotkeyPrivate::instance()->removeShortcut(this)) {
- qWarning("QHotkey: Failed to unregister shortcut");
return false;
}
@@ -144,7 +143,7 @@ QHotkeyPrivate::QHotkeyPrivate() :
QHotkeyPrivate::~QHotkeyPrivate()
{
if(!this->shortcuts.isEmpty())
- qWarning("QHotkey: QHotkeyPrivate destroyed with registered shortcuts!");
+ qCWarning(logQHotkey, "QHotkeyPrivate destroyed with registered shortcuts!");
if(qApp && qApp->eventDispatcher())
qApp->eventDispatcher()->removeNativeEventFilter(this);
}
diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h
index 20b9273..514bb89 100644
--- a/QHotkey/qhotkey.h
+++ b/QHotkey/qhotkey.h
@@ -4,6 +4,7 @@
#include
#include
#include
+#include
class QHotkey : public QObject
{
@@ -48,4 +49,6 @@ private:
bool registered;
};
+Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
+
#endif // QHOTKEY_H
diff --git a/QHotkey/qhotkey_mac.cpp b/QHotkey/qhotkey_mac.cpp
index e3e7622..743df5d 100644
--- a/QHotkey/qhotkey_mac.cpp
+++ b/QHotkey/qhotkey_mac.cpp
@@ -206,8 +206,9 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
GetApplicationEventTarget(),
0,
&eventRef);
- if (status != noErr) {
- qWarning() << "QHotkey: Failed to register hotkey. Error:" << status;
+ if (status != noErr) {
+ qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
+ << status;
return false;
} else {
this->hotkeyRefs.insert(shortcut, eventRef);
@@ -219,8 +220,9 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
{
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
OSStatus status = UnregisterEventHotKey(eventRef);
- if (status != noErr) {
- qWarning() << "QHotkey: Failed to unregister hotkey. Error:" << status;
+ if (status != noErr) {
+ qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
+ << status;
return false;
} else {
this->hotkeyRefs.remove(shortcut);
diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp
index ede787b..64945eb 100644
--- a/QHotkey/qhotkey_win.cpp
+++ b/QHotkey/qhotkey_win.cpp
@@ -228,7 +228,8 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
if(ok)
return true;
else {
- qWarning() << "QHotkey: Failed to register hotkey. Error:" << qPrintable(formatWinError(::GetLastError()));
+ qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
+ << qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
return false;
}
}
@@ -239,7 +240,8 @@ bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
if(ok)
return true;
else {
- qWarning() << "QHotkey: Failed to unregister hotkey. Error:" << qPrintable(formatWinError(::GetLastError()));
+ qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
+ << qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
return false;
}
}
diff --git a/QHotkey/qhotkey_x11.cpp b/QHotkey/qhotkey_x11.cpp
index dc2e4c6..cebab34 100644
--- a/QHotkey/qhotkey_x11.cpp
+++ b/QHotkey/qhotkey_x11.cpp
@@ -30,8 +30,8 @@ private:
HotkeyErrorHandler();
~HotkeyErrorHandler();
- static bool hasError;//TODO threading
- static QString errorString;//TODO threading
+ static bool hasError;
+ static QString errorString;
private:
XErrorHandler prevHandler;
@@ -108,7 +108,8 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
}
if(errorHandler.hasError) {
- qWarning() << "QHotkey: Failed to register hotkey. Error:" << qPrintable(errorHandler.errorString);
+ qCWarning(logQHotkey) << "[QHotkey] Failed to register hotkey. Error:"
+ << qPrintable(errorHandler.errorString);
this->unregisterShortcut(shortcut);
return false;
} else
@@ -129,8 +130,9 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
DefaultRootWindow(display));
}
- if(errorHandler.hasError) {
- qWarning() << "QHotkey: Failed to unregister hotkey. Error:" << qPrintable(errorHandler.errorString);
+ if(errorHandler.hasError) {
+ qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
+ << qPrintable(errorHandler.errorString);
this->unregisterShortcut(shortcut);
return false;
} else
diff --git a/README.md b/README.md
index 504c7b4..1ec000d 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ A global shortcut/hotkey for Desktop Qt-Applications.
The QHotkey is a class that can be used to create hotkeys/global shortcuts, aka shortcuts that work everywhere, independent of the application state. This means your application can be active, inactive, minimized or not visible at all and still receive the shortcuts.
## Status
-**Still in development!!!** - But almost done. There are only some small details, like debug output, and documentation left. It's ready to be used.
+**Still in development!!!** - But almost done. There are only some small details and documentation left. It's ready to be used.
## Features
- Works on Windows, Mac and X11
@@ -48,6 +48,13 @@ By running the example in `./HotkeyTest` you can test out the QHotkey class. The
- **Testings:** A list of selected hotkeys. Activate it and try out which ones work for you (*Hint:* Depending on OS and keyboard layout, it's very possible that a few don't work).
- **Threading:** Activate the checkbox to move 2 Hotkeys of the playground to seperate threads. It should work without a difference.
+### Logging
+By default, QHotkey prints some warning messages if something goes wrong (For example, a key that cannot be translated). All messages of QHotkey are grouped into the [`QLoggingCategory`](https://doc.qt.io/qt-5/qloggingcategory.html) "[QHotkey]". If you want to simply disable the logging, call the folling function somewhere in your code:
+```cpp
+QLoggingCategory::setFilterRules(QStringLiteral("[QHotkey].warning=false"));
+```
+This will turn all warnings of QHotkey of (It only uses warnings for now, thats why this is enough). For more information about all the things you can do with the logging categories, check the Qt-Documentation
+
## Thread saftey
The QHotkey class itself is reentrant - wich means you can create as many instances as required on any thread. This allows you to use the QHotkey on all threads. **But** you should never use the QHotkey instance on a thread that is different from the one the instance belongs to! Internally the system uses a singleton instance that handles the hotkey events and distributes them to the QHotkey instances. This internal class is completley threadsafe.