electron icon indicating copy to clipboard operation
electron copied to clipboard

globalShortcuts don't work for alternate keyboards on Mac 10.14.6

Open Kilian opened this issue 6 years ago • 12 comments

  • Electron Version:
    • 6.0.2
  • Operating System:
    • mac 10.14.6

Expected Behavior

globalShortcuts work regardless of the chosen keyboard layout

Actual Behavior

On Mac, globalShortcuts seem to default to either qwerty or the keyboards layout (I think? I don't have a dvorak keyboard) rather than the chosen keyboard layout.

To Reproduce

Here's a fiddle that I tested on all three platforms. Windows works perfectly, Linux work with a caveat that it doesn't pick up keyboard changes made while the app is running, and Mac is broken.

https://gist.github.com/Kilian/995123ef2e31d6d15ee147a613831bed

To test this, add dvorak to your keyboard layouts. If set to Dvorak, the l is in the position of the p on a qwerty layout. If you press ctrl + p on a physical qwerty keyboard but are using the dvorak layout that should correspond to ctrl + l and pressing it should give a console.log in fiddle.

Kilian avatar Aug 14 '19 11:08 Kilian

I have the same problem. So I replaced global shortcuts with App-specific shortcuts using a library 'Mousetrap'. Works great for me.

https://github.com/ccampbell/mousetrap

jaga3421 avatar Dec 12 '20 06:12 jaga3421

There are quite a few keys that can't be captured with Mousetrap (like function keys) or other renderer-based libraries, so that's not always an option.

Kilian avatar Dec 23 '20 10:12 Kilian

In case it is useful, one more data point from #26907: 3.1.13 is the last known good version, 4.0.0-beta.1 is the first bad version.

alexdima avatar Dec 23 '20 13:12 alexdima

Alright, so I did a deep dive on this issue. 🧠💥

First, this particular issue did not work in 3.1.13 and never has. Issue #26907 is not exactly the same as this issue, and the two are basically unrelated aside from both being about the codebase assuming a standard US keyboard layout. (I believe the issue #26907 describes possibly did regress between v3 and v4; it's just not the same as this issue, that is all.)

As for this issue, I found that there is pretty much only 1 API on macOS for registering "global hot keys" (aka global keyboard shortcuts)1. That API is named RegisterEventHotKey, it's part of Carbon Events, and it only allows you to register a specific key code (i.e. a physical key, not the character it produces). Further, Carbon was deprecated in 2012 and is pretty much completely unsupported now and this one function just happens to work "well enough" still. I did a lot of searching and compared against libraries in this same domain, and they all rely on essentially this same one API and face the same issue2.

What I'm getting at here is that I don't think this issue can be solved at present. I could not find an API flexible enough to support anything beyond listening for specific key codes (physical keys) everywhere on the system. I think the functionality here is definitely desirable, but I cannot figure out how to make it possible right now. 😕

I didn't search very hard for other apps, but I tested the one other application on my system that has a global keyboard shortcut (it's not Electron-based) and it also suffered this issue.

Edit: anyone who knows their way around macOS APIs is free to @ me and tell me I'm wrong. And I'd love to hear that! I simply did not get anywhere with my own search for a solution here.

Notes
  1. I found only one single other API for this, however it requires accessibility to be enabled for key events.

  2. I found similar open issues in HotKey and in MASShortcut. I also looked at KeyboardShortcuts and although there is not an issue filed that is close to this, I suspect it just hasn't been reported there yet as the source relies on the same Carbon Events API.

clavin avatar Feb 26 '21 01:02 clavin

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

github-actions[bot] avatar Oct 08 '22 02:10 github-actions[bot]

bump still reproducible in the latest version of Electron:

  • use AZERTY layout
  • add a shortcut for Ctrl+W
  • the callback fires for both Ctrl+W and Ctrl+Z

andreiciceu avatar Oct 13 '22 14:10 andreiciceu

How to fix it? KeyCode is deprecated, code return physical key, so it's not working properly. Any ideas?

bmadone avatar Nov 23 '22 13:11 bmadone

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

github-actions[bot] avatar Feb 22 '23 02:02 github-actions[bot]

bump, it's still an issue

andreiciceu avatar Feb 23 '23 08:02 andreiciceu

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

github-actions[bot] avatar May 26 '23 01:05 github-actions[bot]

bump, it's still an issue

MelisUnal avatar Jun 06 '23 05:06 MelisUnal

bump, it's still an issue.

I tried to configure Ctrl+0 as a hotkey on hungarian keyboard layout. If I pressed 0 I got backquote.

I tried to change layout on linux (Ubuntu) with following command:

setxkbmap -layout hu

And now it is working with Ctrl+0

I'm not sure why the app is not using the user's keyboard layout by default...

mgulyas42 avatar May 14 '24 18:05 mgulyas42