4

I have a WKWebView in my App which contains input elements and textareas. Also it supports some key events, let's say A B C. When I press these key without focusing an input element I can hear the funk system sound of macOS – the WebApp also triggers the command by the key.

When I try to disable the funk sound my using a localMonitor in Swift an "catch" the keypresses on these keys, they don't "funk" anymore, but also they do not work when typing in input fields or textareas. So they were completely disabled.

How can I prevent the funk sound (maybe completely for all keydowns) but also use these keys (all keys) in my web view?

Here is what I have tested:

   override func viewDidLoad() {
      super.viewDidLoad()
      // ...
      NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
         if self. keyDownPressed(with: $0) {
            return nil
         } else {
            return $0
         }
      }
   }


    func keyDownPressed(with event: NSEvent) -> Bool {

        print("caught a key down: \(event.keyCode)")

        if event.keyCode == 48 { return true }
        if event.keyCode == 34 { return true }
        if event.keyCode == 38 { return true }
        if event.keyCode == 40 { return true }
        if event.keyCode == 4 { return true }
        if event.keyCode == 1 { return true }
        if event.keyCode == 3 { return true }
        if event.keyCode == 32 { return true }

        return false
    }
Kevin Lieser
  • 951
  • 1
  • 9
  • 25

1 Answers1

1

Have you tried just the regular keyDown? Input fields should still work fine.

override func keyDown(with event: NSEvent) {
    if [48, 34, 40, 4, 1, 3, 32].contains(event.keyCode) {
        // no funk
    } else {
        super.keyDown(with: event)
    }
}
Loengard
  • 401
  • 2
  • 7