0

I am novice Swift coder trying to catch mouse events as well as keyboard events. It seems I am only doing the latter. The main goal is to allow for «tap-to-click» with Magic Mouse 2 and avoid its loud clicking sound.

Fully working sample at https://github.com/creasty/Keyboard/blob/master/keyboard/AppDelegate.swift

let eventMask = 
    (1 << CGEventType.keyDown.rawValue) | 
    (1 << CGEventType.keyUp.rawValue) | 
    (1 << CGEventType.leftMouseDown.rawValue) | 
    (1 << CGEventType.leftMouseUp.rawValue)

guard let eventTap = CGEvent.tapCreate(
    tap: .cghidEventTap,
    place: .headInsertEventTap,
    options: .defaultTap,
    eventsOfInterest: CGEventMask(eventMask),
    callback: appComponent.eventTapCallback,
    userInfo: nil
) else {
    fatalError("Failed to create event tap")
}

Keyboard presses are caught alright, but no reaction to mouse clicks. Please advise. Thanks.

Xcode Version 12.5.1 (12E507) on MacOS 11.5.2

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
Andreas F
  • 133
  • 1
  • 8
  • 1
    Where are you writing code? Start with the class name if you are going to post code. – El Tomato Aug 22 '21 at 22:47
  • 1
    Post a [mre] please. – Willeke Aug 23 '21 at 01:19
  • The github code does not tap mouse events. Post a [mre] in the question please, including `eventTapCallback`. Why do you want to catch mouse-down events if you don't want to click? – Willeke Aug 23 '21 at 09:55
  • I want mouse "tap" gestures to be caught and converted to mouse "clicks". The term "tap" has two meanings here: EventTap (the technology) and the tap finger movement (slight touch on mouse without full depress). – Andreas F Aug 23 '21 at 10:53
  • 1
    Have you tried `NSEvent.EventTypeMask.any`? – Willeke Aug 23 '21 at 12:56
  • Thanks @Willeke, I´ve tried `let eventMask = NSEvent.EventTypeMask.any.rawValue` but it does still not react to mouse events. – Andreas F Aug 23 '21 at 15:49
  • 1
    Which mouse events? I tried your code, added `leftMouseDown`, and it works for me. – Willeke Aug 24 '21 at 07:50
  • I must be missing something. I too added `CGEventType.leftMouseDown.rawValue`to the eventMask and a breakpoint at `func handle`which only triggers on key press, not mouse click. – Andreas F Aug 25 '21 at 16:32
  • `func handle`? Have you tried a break on `appComponent.eventTapCallback`? – Willeke Aug 26 '21 at 12:57
  • @Willeke Thanks I missed that part! Working now! – Andreas F Aug 27 '21 at 07:54

1 Answers1

0

Thanks to helpful feedback from @Willeke and others, we arrived at the following solution:

diff --git a/keyboard/AppComponent.swift b/keyboard/AppComponent.swift
index 961a81b..abb2f63 100644
--- a/keyboard/AppComponent.swift
+++ b/keyboard/AppComponent.swift
@@ -20,6 +20,8 @@ final class AppComponent {
             if let manager = _eventManager {
                 return manager.handle(proxy: proxy, cgEvent: event)
             }
+        case .leftMouseDown:
+            print("leftMouseDown")
         default:
             break
         }
diff --git a/keyboard/AppDelegate.swift b/keyboard/AppDelegate.swift
index bc39af6..dc16658 100644
--- a/keyboard/AppDelegate.swift
+++ b/keyboard/AppDelegate.swift
@@ -70,8 +70,8 @@ private extension AppDelegate {
     }
 
     func setupEventTap() {
-        let eventMask = (1 << CGEventType.keyDown.rawValue) | (1 << CGEventType.keyUp.rawValue)
-
+        let eventMask = (1 << CGEventType.keyDown.rawValue) | (1 << CGEventType.keyUp.rawValue) | (1 << CGEventType.leftMouseDown.rawValue)
+        
         guard let eventTap = CGEvent.tapCreate(
             tap: .cghidEventTap,
             place: .headInsertEventTap,
Andreas F
  • 133
  • 1
  • 8