tauri icon indicating copy to clipboard operation
tauri copied to clipboard

MacOS: Keypress on non-input element triggers unsupported key feedback sound.

Open nruhe opened this issue 4 years ago • 16 comments

Describe the bug

When a keypress event occurs on a non-content editable DOM element, the unsupported key feedback sound.

To Reproduce

Steps to reproduce the behavior:

  1. Launch a Tauri window with a Webview that contains no content-editable DOM elements (i.e. no input).
  2. Click the window to bring it into focus.
  3. Press any key on your keyboard
  4. You should hear a light beeping sound with each keystroke.

Expected behavior

No unsupported key feedback sound effect should play.

Screenshots

Not really applicable

Platform and Versions (required):

Operating System - Mac OS, version 11.5.2 X64

Node.js environment
  Node.js - 16.0.0
  @tauri-apps/cli - 1.0.0-beta.10
  @tauri-apps/api - 1.0.0-beta.8

Global packages
  npm - 7.10.0
  yarn - 1.22.10

Rust environment
  rustc - 1.51.0
  cargo - 1.51.0

App directory structure
/dist
/node_modules
/src-tauri
/src

App
  tauri.rs - 1.0.0-beta.8
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../dist
  devPath - http://localhost:9000
  framework - React
  bundler - Webpack

Additional context

The error can be stopped with e.preventDefault(), but this will block input interaction.

Tauri Conf:

{
  "package": {
    "productName": "complex",
    "version": "0.1.0"
  },
  "build": {
    "distDir": "../dist",
    "devPath": "http://localhost:9000",
    "beforeDevCommand": "",
    "beforeBuildCommand": ""
  },
  "tauri": {
    "bundle": {
      "active": true,
      "targets": "all",
      "identifier": "com.tauri.dev",
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/[email protected]",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "resources": [],
      "externalBin": [],
      "copyright": "",
      "category": "DeveloperTool",
      "shortDescription": "",
      "longDescription": "",
      "deb": {
        "depends": [],
        "useBootstrapper": false
      },
      "macOS": {
        "frameworks": [],
        "minimumSystemVersion": "",
        "useBootstrapper": false,
        "exceptionDomain": "localhost",
        "signingIdentity": null,
        "entitlements": null
      },
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": ""
      }
    },
    "updater": {
      "active": false
    },
    "allowlist": {
      "all": true
    },
    "windows": [
      {
        "title": "Complex",
        "width": 316,
        "height": 600,
        "resizable": false,
        "fullscreen": false,
        "transparent": false,
        "decorations": true
      }
    ],
    "security": {
      "csp": "default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'"
    }
  }
}

Stack Trace

Not applicable.

nruhe avatar Sep 22 '21 03:09 nruhe

+1, I'm also seeing this.

From a cursory googling, it seems like the solution involves changes to the NSWindow/NSView to allow it to become a first responder[0][1] and/or adding handling to the NSViewController for NSEvent.addLocalMonitorForEvents(matching: .keyDown) and returning nil/not calling the superclass methods[2][3].

It looks like the low-level MacOS window setup like this is actually done in this file in the tauri-apps/tao sibling repo, so it's probably worth cross-posting this issue over there.

[0]: StackOverflow Prevent 'not allowed' beep after keystroke in NSView [1]: Apple docs NSResponder [2]: StackOverflow Unwanted beep when a key is hit [3]: Someone's Gist How to handle keyDown in NSResponder

mszu avatar Oct 04 '21 10:10 mszu

+1, I also facing this. It seems like tauri-apps/tao already setup functions like acceptsFirstResponder , but still have problems.

KusStar avatar Oct 19 '21 07:10 KusStar

/upstream tauri-apps/wry

lucasfernog avatar Nov 13 '21 18:11 lucasfernog

Will be fixed on the next release. Thanks to @KusStar for pointing me to the solution. The problem is that wry adds a new view on top of tao, and it needed the same setup.

lucasfernog avatar Nov 13 '21 18:11 lucasfernog

Reopen this because we found the input will no longer work if we don't actually handle all events. One way is adding our wkwebview as subview like before, but this will bring all focus regressions back. The other way is we add all NSView methods we defined in tao, but it will be a huge task IMHO. Unless someone finds another solution, the second one is the only approach and it might not happens before 1.0 release.

wusyong avatar Nov 16 '21 07:11 wusyong

+1, I also facing this.

OceanBelongsToMe avatar Jan 01 '22 03:01 OceanBelongsToMe

Sorry to bump this. I understand setting the NSView as first responder did not work, but what about the addLocalMonitorForEvents solution? Did anyone try?

farfromrefug avatar Feb 03 '22 21:02 farfromrefug

Is there any "user land" workaround available for this issue until it gets fixed?

schickling avatar Feb 16 '22 11:02 schickling

Stop default on the JS side:

addEventListener(
  'keydown',
  (event) => {
    if (isTauriMacApp && event.key !== 'Tab') {
      const ele = event.composedPath()[0];
      const isInput = ele instanceof HTMLInputElement || ele instanceof HTMLTextAreaElement;
      if (!ele || !isInput || event.key === 'Escape') {
        event.preventDefault();
      }
    }
  },
  { capture: true },
);

mantou132 avatar Jun 09 '22 13:06 mantou132

This feels very wrong to have to fix it in JS. We should manage this in tao/wry.

Can we look into this again @lucasfernog @wusyong

nothingismagick avatar Jul 16 '22 13:07 nothingismagick

Stop default on the JS side:

addEventListener('keypress', (event) => {
  const ele = event.composedPath()[0];
  if (ele instanceof HTMLInputElement || ele instanceof HTMLAreaElement) return;
  event.preventDefault();
});

Worked for me.

ghost avatar Aug 21 '22 15:08 ghost

Also running into this issue for a music player app that's tied to various keyboard events. I can somewhat hack it by using a hidden input field that catches the input, but it's fairly unreliable and would way rather have the ability to turn off the alert sound all together.

Any updates on a fix in tao/wry?

thejawker avatar Aug 27 '22 15:08 thejawker

Yeah, I am also relying on the JS hack that I would not want to do long term? Hoping for an upstream push here?

sualehasif avatar Aug 27 '22 17:08 sualehasif

Workaround does unfortunately not work for me :/

Schr3da avatar Sep 01 '22 17:09 Schr3da

I tested with winit 0.27 with addSubview and there's no sound. I think we'll resolve this together with switching back to winit.

wusyong avatar Sep 05 '22 05:09 wusyong

I'm also facing the same situation. I'm implementing some keyboard shortcuts to non-editable content and I think workaround isn't working for this case? 😢

daun-io avatar Sep 19 '22 10:09 daun-io

How do we implement this @amrbashir || @lucasfernog

ParthJadhav avatar Nov 07 '22 09:11 ParthJadhav

@ParthJadhav We fixed this in https://github.com/tauri-apps/tao/pull/614 It should be available in next tauri release.

wusyong avatar Nov 07 '22 10:11 wusyong

Hey @wusyong , Fixed in latest release 1.2.0 🚀

ParthJadhav avatar Nov 08 '22 17:11 ParthJadhav

I am still getting the unsupported key feedback sound.

[build-dependencies]
tauri-build = { version = "1.2.0", features = ["config-toml"] }

[dependencies]
tauri = { version = "1.2.0", features = ["fs-all", "os-all", "shell-open"] }

Vehmloewff avatar Nov 18 '22 05:11 Vehmloewff

This is a regression from https://github.com/tauri-apps/tao/pull/623. I'll look into it

wusyong avatar Nov 18 '22 10:11 wusyong