mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
private updates on mac
This commit is contained in:
@@ -6,8 +6,8 @@
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>424</width>
|
<width>520</width>
|
||||||
<height>373</height>
|
<height>425</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
|
|||||||
+32
-20
@@ -3,19 +3,31 @@
|
|||||||
#include <Carbon/Carbon.h>
|
#include <Carbon/Carbon.h>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
class QHotkeyPrivateNative
|
class QHotkeyPrivateMac : public QHotkeyPrivate
|
||||||
{
|
{
|
||||||
public:
|
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 hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// QHotkeyPrivate interface
|
||||||
|
quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
|
||||||
|
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
|
||||||
|
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
|
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
|
private:
|
||||||
static bool isHotkeyHandlerRegistered;
|
static bool isHotkeyHandlerRegistered;
|
||||||
static QHash<NativeShortcut, EventHotKeyRef> hotkeyRefs;
|
static QHash<QHotkey::NativeShortcut, EventHotKeyRef> hotkeyRefs;
|
||||||
};
|
};
|
||||||
|
NATIVE_INSTANCE(QHotkeyPrivateMac)
|
||||||
|
|
||||||
bool QHotkeyPrivateNative::isHotkeyHandlerRegistered = false;
|
bool QHotkeyPrivateMac::isHotkeyHandlerRegistered = false;
|
||||||
QHash<NativeShortcut, EventHotKeyRef> QHotkeyPrivateNative::hotkeyRefs;
|
QHash<QHotkey::NativeShortcut, EventHotKeyRef> QHotkeyPrivateMac::hotkeyRefs;
|
||||||
|
|
||||||
bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
|
bool QHotkeyPrivateMac::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
|
||||||
{
|
{
|
||||||
Q_UNUSED(eventType);
|
Q_UNUSED(eventType);
|
||||||
Q_UNUSED(message);
|
Q_UNUSED(message);
|
||||||
@@ -23,7 +35,7 @@ bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *messag
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode)
|
quint32 QHotkeyPrivateMac::nativeKeycode(Qt::Key keycode)
|
||||||
{
|
{
|
||||||
// Constants found in NSEvent.h from AppKit.framework
|
// Constants found in NSEvent.h from AppKit.framework
|
||||||
switch (keycode) {
|
switch (keycode) {
|
||||||
@@ -158,7 +170,7 @@ quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
quint32 QHotkeyPrivateMac::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
||||||
{
|
{
|
||||||
quint32 nMods = 0;
|
quint32 nMods = 0;
|
||||||
if (modifiers & Qt::ShiftModifier)
|
if (modifiers & Qt::ShiftModifier)
|
||||||
@@ -174,23 +186,23 @@ quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
|
|||||||
return nMods;
|
return nMods;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkeyPrivate::registerShortcut(NativeShortcut shortcut)
|
bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||||
{
|
{
|
||||||
if (!QHotkeyPrivateNative::isHotkeyHandlerRegistered)
|
if (!this->isHotkeyHandlerRegistered)
|
||||||
{
|
{
|
||||||
EventTypeSpec eventSpec;
|
EventTypeSpec eventSpec;
|
||||||
eventSpec.eventClass = kEventClassKeyboard;
|
eventSpec.eventClass = kEventClassKeyboard;
|
||||||
eventSpec.eventKind = kEventHotKeyPressed;
|
eventSpec.eventKind = kEventHotKeyPressed;
|
||||||
InstallApplicationEventHandler(&QHotkeyPrivateNative::hotkeyEventHandler, 1, &eventSpec, NULL, NULL);
|
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyEventHandler, 1, &eventSpec, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
EventHotKeyID hkeyID;
|
EventHotKeyID hkeyID;
|
||||||
hkeyID.signature = shortcut.key;
|
hkeyID.signature = shortcut.first;
|
||||||
hkeyID.id = shortcut.mods;
|
hkeyID.id = shortcut.second;
|
||||||
|
|
||||||
EventHotKeyRef eventRef = 0;
|
EventHotKeyRef eventRef = 0;
|
||||||
OSStatus status = RegisterEventHotKey(shortcut.key,
|
OSStatus status = RegisterEventHotKey(shortcut.first,
|
||||||
shortcut.mods,
|
shortcut.second,
|
||||||
hkeyID,
|
hkeyID,
|
||||||
GetApplicationEventTarget(),
|
GetApplicationEventTarget(),
|
||||||
0,
|
0,
|
||||||
@@ -199,25 +211,25 @@ bool QHotkeyPrivate::registerShortcut(NativeShortcut shortcut)
|
|||||||
qWarning() << "QHotkey: Failed to register hotkey with error:" << status;
|
qWarning() << "QHotkey: Failed to register hotkey with error:" << status;
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
QHotkeyPrivateNative::hotkeyRefs.insert(shortcut, eventRef);
|
this->hotkeyRefs.insert(shortcut, eventRef);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkeyPrivate::unregisterShortcut(NativeShortcut shortcut)
|
bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
||||||
{
|
{
|
||||||
EventHotKeyRef eventRef = QHotkeyPrivateNative::hotkeyRefs.value(shortcut);
|
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
|
||||||
OSStatus status = UnregisterEventHotKey(eventRef);
|
OSStatus status = UnregisterEventHotKey(eventRef);
|
||||||
if (status != noErr) {
|
if (status != noErr) {
|
||||||
qWarning() << "QHotkey: Failed to unregister hotkey with error:" << status;
|
qWarning() << "QHotkey: Failed to unregister hotkey with error:" << status;
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
QHotkeyPrivateNative::hotkeyRefs.remove(shortcut);
|
this->hotkeyRefs.remove(shortcut);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OSStatus QHotkeyPrivateNative::hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data)
|
OSStatus QHotkeyPrivateMac::hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data)
|
||||||
{
|
{
|
||||||
Q_UNUSED(nextHandler);
|
Q_UNUSED(nextHandler);
|
||||||
Q_UNUSED(data);
|
Q_UNUSED(data);
|
||||||
@@ -232,7 +244,7 @@ OSStatus QHotkeyPrivateNative::hotkeyEventHandler(EventHandlerCallRef nextHandle
|
|||||||
sizeof(EventHotKeyID),
|
sizeof(EventHotKeyID),
|
||||||
NULL,
|
NULL,
|
||||||
&hkeyID);
|
&hkeyID);
|
||||||
QHotkeyPrivate::instance()->activateShortcut({hkeyID.signature, hkeyID.id});
|
hotkeyPrivate->activateShortcut({hkeyID.signature, hkeyID.id});
|
||||||
}
|
}
|
||||||
|
|
||||||
return noErr;
|
return noErr;
|
||||||
|
|||||||
Reference in New Issue
Block a user