[Mac OS/X] Enable event taps in case it got disabled by timeout

This commit is contained in:
Roland Winklmeier
2016-08-15 23:34:48 +02:00
committed by Mathew Sutcliffe
parent 8210423c44
commit c6bfd3b01d
2 changed files with 15 additions and 4 deletions

View File

@@ -56,6 +56,7 @@ namespace BlackInput
void *refcon);
BlackMisc::Input::CHotkeyCombination m_keyCombination; //!< Current status of pressed keys;
CFMachPortRef m_eventTap;
};
}

View File

@@ -8,6 +8,8 @@
*/
#include "keyboardmac.h"
#include "blackmisc/logmessage.h"
#include <QHash>
#include <QtWidgets/QMessageBox>
#include <CoreFoundation/CoreFoundation.h>
@@ -109,14 +111,14 @@ namespace BlackInput
CGEventMask eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp) | (1 << kCGEventFlagsChanged));
// try creating an event tap just for keypresses. if it fails, we need Universal Access.
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
m_eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
eventMask, myCGEventCallback, this);
CFRunLoopSourceRef source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault,
eventTap, 0);
m_eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CGEventTapEnable(m_eventTap, true);
return true;
}
@@ -210,7 +212,15 @@ namespace BlackInput
{
CKeyboardMac *keyboardMac = static_cast<CKeyboardMac*>(refcon);
keyboardMac->processKeyEvent(type, event);
if (type == kCGEventTapDisabledByTimeout)
{
BlackMisc::CLogMessage(static_cast<CKeyboardMac *>(nullptr)).warning("Event tap got disabled by timeout. Enable it again.");
CGEventTapEnable(keyboardMac->m_eventTap, true);
}
else
{
keyboardMac->processKeyEvent(type, event);
}
// send event to next application
return event;