1

I have a simple app that lives in the menu bar that I'm looking to listen to a return key press when the user is in the browser (or anywhere, right now I just want it to fire). Adding a window to applicationDidFinishLaunching and having that window selected registers the keyDown events but in actual use, the app won't have a UI and will just listen from the menu bar

When I remove the window, the keyDown function never fires.

Unsure of what I'm doing incorrectly here. Would love a hand. Thanks!

func applicationDidFinishLaunching(_ aNotification: Notification) {
    NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: keyDown)
}

func keyDown(event: NSEvent) -> NSEvent {
    NSLog("key down is \(event.keyCode)");
   return event
}

Do I need to add this app to Accessibility settings? If so, I can't find the app to do so after a build and run.

Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151
  • 1
    You are probably looking for `NSEvent.addGlobalMonitorForEvents(matching: .keyDown) {` and yes you need to go to your security and privacy settings > Accessibility and add your app – Leo Dabus Oct 15 '20 at 01:38
  • To find your app just go to apple menu and command click recent items – Leo Dabus Oct 15 '20 at 01:40
  • possible duplicate of https://stackoverflow.com/questions/58386870/cannot-read-property-keycode-character-assertion-failure-when-detect-modifie/58405058#58405058 – Leo Dabus Oct 15 '20 at 01:40
  • Thanks Leo. Yeah, I was definitely using the wrong method here. When I cmd-click Recent Items, should Xcode have built a version of this app to /Applications or to the project's folder? I can't find the executable – Zack Shapiro Oct 15 '20 at 01:45
  • it should point to your user > Library > Developer > Xcode > deriveddata > yourAPP > build > products > debug – Leo Dabus Oct 15 '20 at 01:49
  • 1
    Ah, in derived data. Of course. Thank you Leo! – Zack Shapiro Oct 15 '20 at 01:52

0 Answers0