Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Easydict/objc/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ @interface EZEventMonitor ()
@property (nonatomic, assign) NSInteger currentAlertVolume;

@property (nonatomic, assign) CFMachPortRef eventTap;
@property (nonatomic, assign) CFRunLoopSourceRef eventTapRunLoopSource;

@property (nonatomic, assign) EZTriggerType frontmostAppTriggerType;
@property (nonatomic, assign) BOOL isPopButtonVisible;
Expand Down Expand Up @@ -235,9 +236,9 @@ - (void)monitorCGEventTap {
if (eventTap) {
// eventTap must not be NULL, otherwise it will crash.
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
self.eventTapRunLoopSource = runLoopSource;
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRelease(runLoopSource);
}
}

Expand All @@ -256,9 +257,13 @@ - (void)stopCGEventTap {
// Stop and release the previously created event tap
if (self.eventTap) {
CGEventTapEnable(self.eventTap, false); // Disable the event tap
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, self.eventTap, 0);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CFRelease(runLoopSource);

if (self.eventTapRunLoopSource) {
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), self.eventTapRunLoopSource, kCFRunLoopCommonModes);
CFRelease(self.eventTapRunLoopSource);
self.eventTapRunLoopSource = NULL;
}

CFRelease(self.eventTap);
self.eventTap = NULL;
}
Expand Down