Search code examples
macosswiftuikeyboardbeep

macOS & SwiftUI 2: simplest way to turn off beep on keystroke


The following trivial macOS app is written in SwiftUI 2.0.

import SwiftUI

@main
struct TempApp: App {
  var body: some Scene {
    WindowGroup { ContentView() }
  }
}

struct ContentView: View {
  var body: some View {
    Text("Hello, beep!").padding()
  }
}

When in the foreground, this app will emit an error beep on certain keystrokes (like "a"). What's the simplest way to suppress this beep?


An Xcode project illustrating this (and the answer) can be found here.


There are many older related questions on SO, but none of these are specifically about doing this in SwiftUI 2.0.


Solution

  • You can suppress the beep by adding a local monitor for the .keyDown event at the top level. This can be done simply in ContentView.init(), like so:

    struct ContentView: View {
      var body: some View {
        Text("Hello, silence!").padding()
      }
    
      init() {
        NSEvent.addLocalMonitorForEvents(matching: .keyDown) { _ in return nil }
      }
    }
    

    This technique was inspired by this answer.