init commit

This commit is contained in:
Skycoder42
2016-02-05 22:03:36 +01:00
parent 4fa20f9e2d
commit 4d71726f03
11 changed files with 707 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
#-------------------------------------------------
#
# Project created by QtCreator 2016-02-05T22:01:03
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = HotkeyTest
TEMPLATE = app
include(../QHotkey/qhotkey.pri)
SOURCES += main.cpp\
hottestwidget.cpp
HEADERS += hottestwidget.h
FORMS += hottestwidget.ui
+14
View File
@@ -0,0 +1,14 @@
#include "hottestwidget.h"
#include "ui_hottestwidget.h"
HotTestWidget::HotTestWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::HotTestWidget)
{
ui->setupUi(this);
}
HotTestWidget::~HotTestWidget()
{
delete ui;
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef HOTTESTWIDGET_H
#define HOTTESTWIDGET_H
#include <QWidget>
namespace Ui {
class HotTestWidget;
}
class HotTestWidget : public QWidget
{
Q_OBJECT
public:
explicit HotTestWidget(QWidget *parent = 0);
~HotTestWidget();
private:
Ui::HotTestWidget *ui;
};
#endif // HOTTESTWIDGET_H
+20
View File
@@ -0,0 +1,20 @@
<ui version="4.0">
<class>HotTestWidget</class>
<widget class="QWidget" name="HotTestWidget" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>HotTestWidget</string>
</property>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
+11
View File
@@ -0,0 +1,11 @@
#include "hottestwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HotTestWidget w;
w.show();
return a.exec();
}
+4
View File
@@ -0,0 +1,4 @@
TEMPLATE = subdirs
SUBDIRS += \
HotkeyTest
+195
View File
@@ -0,0 +1,195 @@
#include "qhotkey.h"
#include "qhotkey_p.h"
#include <QGlobalStatic>
#include <QCoreApplication>
#include <QAbstractEventDispatcher>
#include <QMetaMethod>
Q_GLOBAL_STATIC(QHotkeyPrivate, hotkeyPrivate)
#define LOCKER QMutexLocker locker(&this->mutex)
QHotkey::QHotkey(QObject *parent) :
QObject(parent),
enabled(true),
key(Qt::Key_unknown),
mods(Qt::NoModifier),
nativeShortcut(),
registered(false)
{}
QHotkey::QHotkey(const QKeySequence &sequence, QObject *parent) :
QHotkey(parent)
{
this->setShortcut(sequence);
}
QHotkey::~QHotkey()
{
if(this->registered)
hotkeyPrivate->removeShortcut(this);
}
QKeySequence QHotkey::shortcut() const
{
if(this->key == Qt::Key_unknown)
return QKeySequence();
else
return QKeySequence(this->key | this->mods);
}
bool QHotkey::setShortcut(const QKeySequence &shortcut)
{
if(this->registered) {
if(!hotkeyPrivate->removeShortcut(this)) {
qWarning("QHotkey: Failed to unset previous shortcut. Cannot set new one.");
return false;
}
}
if(shortcut.isEmpty()) {
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeShortcut = NativeShortcut();
return true;
} else if(shortcut.count() > 1) {
qWarning("QHotkey: Keysequences with multiple shortcuts are not allowed! "
"Only the first shortcut will be used!");
}
this->key = Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask);
this->mods = Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask);
this->nativeShortcut = hotkeyPrivate->nativeShortcut(this->key, this->mods);
if(this->nativeShortcut.isValid())
return hotkeyPrivate->addShortcut(this);
else {
qWarning("QHotkey: Unable to map shortcut to native keys.");
this->key = Qt::Key_unknown;
this->mods = Qt::NoModifier;
this->nativeShortcut = NativeShortcut();
return false;
}
}
bool QHotkey::isEnabled() const
{
return this->enabled;
}
bool QHotkey::isRegistered() const
{
return this->registered;
}
void QHotkey::setEnabled(bool enabled)
{
this->enabled = enabled;
}
// ---------- QHotkey::NativeShortcut implementation ----------
QHotkey::NativeShortcut::NativeShortcut() :
key(0),
mods(0)
{}
QHotkey::NativeShortcut::NativeShortcut(const QHotkey::NativeShortcut &other) :
key(other.key),
mods(other.mods)
{}
QHotkey::NativeShortcut &QHotkey::NativeShortcut::operator =(const QHotkey::NativeShortcut &other)
{
this->key = other.key;
this->mods = other.mods;
return (*this);
}
bool QHotkey::NativeShortcut::isValid() const
{
return (this->key != 0);
}
bool QHotkey::NativeShortcut::operator==(const QHotkey::NativeShortcut &other) const
{
return (this->key == other.key && this->mods == other.mods);
}
uint qHash(QHotkey::NativeShortcut key, uint seed)
{
return qHash(key.key ^ key.mods, seed);
}
// ---------- QHotkeyPrivate implementation ----------
QHotkeyPrivate::QHotkeyPrivate() :
mutex(QMutex::NonRecursive),
shortcuts()
{
Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated");
qApp->eventDispatcher()->installNativeEventFilter(this);
}
QHotkeyPrivate::~QHotkeyPrivate()
{
if(!this->shortcuts.isEmpty()) {
qWarning("QHotkey: QHotkeyPrivate destroyed with registered shortcuts!");
for(QHotkey::NativeShortcut shortcut : this->shortcuts.keys())
this->unregisterShortcut(shortcut);
}
if(qApp && qApp->eventDispatcher())
qApp->eventDispatcher()->removeNativeEventFilter(this);
}
QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
QHotkey::NativeShortcut shortcut;
shortcut.key = nativeKeycode(keycode);
shortcut.mods = nativeModifiers(modifiers);
return shortcut;
}
bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
{
if(hotkey->registered)
return false;
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
LOCKER;
if(!this->shortcuts.contains(shortcut)) {
if(!this->registerShortcut(shortcut))
return false;
}
this->shortcuts.insert(shortcut, hotkey);
hotkey->registered = true;
return true;
}
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
{
if(!hotkey->registered)
return false;
QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut;
LOCKER;
if(this->shortcuts.remove(shortcut, hotkey) == 0)
return false;
hotkey->registered = false;
if(this->shortcuts.count(shortcut) == 0)
return this->unregisterShortcut(shortcut);
else
return true;
}
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
{
LOCKER;
QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::activated);
for(QHotkey *hkey : this->shortcuts.values(shortcut)) {
if(hkey->isEnabled())
signal.invoke(hkey, Qt::QueuedConnection);
}
}
+63
View File
@@ -0,0 +1,63 @@
#ifndef QHOTKEY_H
#define QHOTKEY_H
#include <QObject>
#include <QKeySequence>
#include <QPair>
class QHotkey : public QObject
{
Q_OBJECT
friend class QHotkeyPrivate;
Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET reset)
public:
struct NativeShortcut {
quint32 key;
quint32 mods;
NativeShortcut();
NativeShortcut(const NativeShortcut &other);
bool isValid() const;
NativeShortcut &operator =(const NativeShortcut &other);
bool operator==(const NativeShortcut &other) const;
};
explicit QHotkey(QObject *parent = 0);
explicit QHotkey(const QKeySequence &shortcut, QObject *parent = 0);
~QHotkey();
QKeySequence shortcut() const;
bool setShortcut(const QKeySequence& shortcut);
bool isEnabled() const;
bool isRegistered() const;
public slots:
void setEnabled(bool enabled = true);
inline void setDisabled(bool disabled = true) {
this->setEnabled(!disabled);
}
inline void reset() {
this->setShortcut(QKeySequence());
}
signals:
void activated();
private:
bool enabled;
Qt::Key key;
Qt::KeyboardModifiers mods;
NativeShortcut nativeShortcut;
bool registered;
};
uint qHash(QHotkey::NativeShortcut key, uint seed = 0);
#endif // QHOTKEY_H
+20
View File
@@ -0,0 +1,20 @@
CONFIG += C++11
HEADERS += $$PWD/qhotkey.h \
$$PWD/qhotkey_p.h
SOURCES += $$PWD/qhotkey.cpp
mac {
SOURCES +=
LIBS += -framework Carbon
} else:win32 {
SOURCES += $$PWD/qhotkey_win.cpp
LIBS += -lUser32
} else:unix {
SOURCES +=
QT += x11extras
LIBS += -lX11
}
INCLUDEPATH += $$PWD
+36
View File
@@ -0,0 +1,36 @@
#ifndef QHOTKEY_P_H
#define QHOTKEY_P_H
#include "qhotkey.h"
#include <QAbstractNativeEventFilter>
#include <QMultiHash>
#include <QMutex>
class QHotkeyPrivate : public QAbstractNativeEventFilter
{
public:
QHotkeyPrivate();//singleton!!!
~QHotkeyPrivate();
// QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool addShortcut(QHotkey *hotkey);
bool removeShortcut(QHotkey *hotkey);//platform implement
private:
QMutex mutex;
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
void activateShortcut(QHotkey::NativeShortcut shortcut);
quint32 nativeKeycode(Qt::Key keycode);//platform implement
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers);//platform implement
bool registerShortcut(QHotkey::NativeShortcut shortcut);//platform implement
bool unregisterShortcut(QHotkey::NativeShortcut shortcut);//platform implement
};
#endif // QHOTKEY_P_H
+301
View File
@@ -0,0 +1,301 @@
#include "qhotkey.h"
#include "qhotkey_p.h"
#include <qt_windows.h>
#include <QDebug>
static QString formatWinError(DWORD winError);
bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(eventType);
Q_UNUSED(result);
MSG* msg = static_cast<MSG*>(message);
if(msg->message == WM_HOTKEY) {
QHotkey::NativeShortcut shortcut;
shortcut.key = HIWORD(msg->lParam);
shortcut.mods = LOWORD(msg->lParam);
this->activateShortcut(shortcut);
}
return false;
}
quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode)
{
if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character"
const SHORT vk = VkKeyScanW(keycode);
if(vk > -1)
return LOBYTE(vk);
}
//find key from "switch/case" --> Only finds a very small subset of keys
switch (keycode)
{
case Qt::Key_Escape:
return VK_ESCAPE;
case Qt::Key_Tab:
case Qt::Key_Backtab:
return VK_TAB;
case Qt::Key_Backspace:
return VK_BACK;
case Qt::Key_Return:
case Qt::Key_Enter:
return VK_RETURN;
case Qt::Key_Insert:
return VK_INSERT;
case Qt::Key_Delete:
return VK_DELETE;
case Qt::Key_Pause:
return VK_PAUSE;
case Qt::Key_Print:
return VK_PRINT;
case Qt::Key_Clear:
return VK_CLEAR;
case Qt::Key_Home:
return VK_HOME;
case Qt::Key_End:
return VK_END;
case Qt::Key_Left:
return VK_LEFT;
case Qt::Key_Up:
return VK_UP;
case Qt::Key_Right:
return VK_RIGHT;
case Qt::Key_Down:
return VK_DOWN;
case Qt::Key_PageUp:
return VK_PRIOR;
case Qt::Key_PageDown:
return VK_NEXT;
case Qt::Key_CapsLock:
return VK_CAPITAL;
case Qt::Key_NumLock:
return VK_NUMLOCK;
case Qt::Key_ScrollLock:
return VK_SCROLL;
case Qt::Key_F1:
return VK_F1;
case Qt::Key_F2:
return VK_F2;
case Qt::Key_F3:
return VK_F3;
case Qt::Key_F4:
return VK_F4;
case Qt::Key_F5:
return VK_F5;
case Qt::Key_F6:
return VK_F6;
case Qt::Key_F7:
return VK_F7;
case Qt::Key_F8:
return VK_F8;
case Qt::Key_F9:
return VK_F9;
case Qt::Key_F10:
return VK_F10;
case Qt::Key_F11:
return VK_F11;
case Qt::Key_F12:
return VK_F12;
case Qt::Key_F13:
return VK_F13;
case Qt::Key_F14:
return VK_F14;
case Qt::Key_F15:
return VK_F15;
case Qt::Key_F16:
return VK_F16;
case Qt::Key_F17:
return VK_F17;
case Qt::Key_F18:
return VK_F18;
case Qt::Key_F19:
return VK_F19;
case Qt::Key_F20:
return VK_F20;
case Qt::Key_F21:
return VK_F21;
case Qt::Key_F22:
return VK_F22;
case Qt::Key_F23:
return VK_F23;
case Qt::Key_F24:
return VK_F24;
case Qt::Key_Menu:
return VK_APPS;
case Qt::Key_Help:
return VK_HELP;
case Qt::Key_Space:
return VK_SPACE;
case Qt::Key_Asterisk:
return VK_MULTIPLY;
case Qt::Key_Plus:
return VK_ADD;
case Qt::Key_Comma:
return VK_SEPARATOR;
case Qt::Key_Minus:
return VK_SUBTRACT;
case Qt::Key_Slash:
return VK_DIVIDE;
case Qt::Key_MediaNext:
return VK_MEDIA_NEXT_TRACK;
case Qt::Key_MediaPrevious:
return VK_MEDIA_PREV_TRACK;
case Qt::Key_MediaPlay:
return VK_MEDIA_PLAY_PAUSE;
case Qt::Key_MediaStop:
return VK_MEDIA_STOP;
case Qt::Key_VolumeDown:
return VK_VOLUME_DOWN;
case Qt::Key_VolumeUp:
return VK_VOLUME_UP;
case Qt::Key_VolumeMute:
return VK_VOLUME_MUTE;
case Qt::Key_Mode_switch:
return VK_MODECHANGE;
case Qt::Key_Select:
return VK_SELECT;
case Qt::Key_Printer:
return VK_PRINT;
case Qt::Key_Execute:
return VK_EXECUTE;
case Qt::Key_Sleep:
return VK_SLEEP;
case Qt::Key_Period:
return VK_DECIMAL;
case Qt::Key_Play:
return VK_PLAY;
case Qt::Key_Cancel:
return VK_CANCEL;
case Qt::Key_Forward:
return VK_BROWSER_FORWARD;
case Qt::Key_Refresh:
return VK_BROWSER_REFRESH;
case Qt::Key_Stop:
return VK_BROWSER_STOP;
case Qt::Key_Search:
return VK_BROWSER_SEARCH;
case Qt::Key_Favorites:
return VK_BROWSER_FAVORITES;
case Qt::Key_HomePage:
return VK_BROWSER_HOME;
case Qt::Key_LaunchMail:
return VK_LAUNCH_MAIL;
case Qt::Key_LaunchMedia:
return VK_LAUNCH_MEDIA_SELECT;
case Qt::Key_Launch0:
return VK_LAUNCH_APP1;
case Qt::Key_Launch1:
return VK_LAUNCH_APP2;
case Qt::Key_Massyo:
return VK_OEM_FJ_MASSHOU;
case Qt::Key_Touroku:
return VK_OEM_FJ_TOUROKU;
// numbers
case Qt::Key_0:
case Qt::Key_1:
case Qt::Key_2:
case Qt::Key_3:
case Qt::Key_4:
case Qt::Key_5:
case Qt::Key_6:
case Qt::Key_7:
case Qt::Key_8:
case Qt::Key_9:
return keycode;
// letters
case Qt::Key_A:
case Qt::Key_B:
case Qt::Key_C:
case Qt::Key_D:
case Qt::Key_E:
case Qt::Key_F:
case Qt::Key_G:
case Qt::Key_H:
case Qt::Key_I:
case Qt::Key_J:
case Qt::Key_K:
case Qt::Key_L:
case Qt::Key_M:
case Qt::Key_N:
case Qt::Key_O:
case Qt::Key_P:
case Qt::Key_Q:
case Qt::Key_R:
case Qt::Key_S:
case Qt::Key_T:
case Qt::Key_U:
case Qt::Key_V:
case Qt::Key_W:
case Qt::Key_X:
case Qt::Key_Y:
case Qt::Key_Z:
return keycode;
default:
return 0;
}
}
quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
{
quint32 native = 0;
if (modifiers & Qt::ShiftModifier)
native |= MOD_SHIFT;
if (modifiers & Qt::ControlModifier)
native |= MOD_CONTROL;
if (modifiers & Qt::AltModifier)
native |= MOD_ALT;
if (modifiers & Qt::MetaModifier)
native |= MOD_WIN;
return native;
}
bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
{
BOOL ok = RegisterHotKey(NULL,
shortcut.key ^ shortcut.mods,
shortcut.mods,
shortcut.key);
if(ok)
return true;
else {
qWarning() << "Failed to register hotkey. Error:" << formatWinError(::GetLastError());
return false;
}
}
bool QHotkeyPrivate::unregisterShortcut(QHotkey::NativeShortcut shortcut)
{
BOOL ok = UnregisterHotKey(NULL, shortcut.key ^ shortcut.mods);
if(ok)
return true;
else {
qWarning() << "Failed to unregister hotkey. Error:" << formatWinError(::GetLastError());
return false;
}
}
static QString formatWinError(DWORD winError)
{
wchar_t *buffer = NULL;
DWORD num = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
winError,
0,
(LPWSTR)&buffer,
0,
NULL);
if(buffer) {
QString res = QString::fromWCharArray(buffer, num);
LocalFree(buffer);
return res;
} else
return QString();
}