Clay Bridges
Clay Bridges

Reputation: 11880

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.

Upvotes: 0

Views: 785

Answers (2)

Caroline
Caroline

Reputation: 4970

macOS 14.0 and iOS 17.0 added onKeyPress(keys:phases:action:).

So for example, you can handle the up arrow being pressed:

SwiftUIView()
.onKeyPress(
  keys: [.upArrow],
  phases: [.down]) { _ in
    print("key press")
    return .handled
}

If you return .ignored, which is the default for the keys you haven't handled, you'll get the beep.

Upvotes: 1

Clay Bridges
Clay Bridges

Reputation: 11880

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.

Upvotes: 2

Related Questions