pqrs-org / Karabiner-Elements

Karabiner-Elements is a powerful utility for keyboard customization on macOS Sierra (10.12) or later.
https://pqrs.org/osx/karabiner/
The Unlicense
18.59k stars 835 forks source link

Wrong keycodes issued #3048

Open adamal opened 2 years ago

adamal commented 2 years ago

System: Macbook Air M1, macOS 12.2.1 Keyboard with Danish layout

For some output keys, the wrong output is posted.

Example: Make a simple remap of "7" to "/". Pressing physical "7" key on keyboard produces a hyphen ("-").

Background: When first enabling the karabiner_grabber and karabiner_observer, macOS prompted me that an unidentified keyboard had been attached, and that it would not work until it was identified. To identify it, I was asked to press the key to the right og the left shift. This is "<" on my keyboard. I am guessing, that karabiner works by emulating a US-keyboard, and that the key names used by Karabiner maps to corresponding physical keycodes from a US keyboard. When I did the identification process, this process malfunctioned.

I have tried uninstalling and reinstalling Karabiner, but the misconfiguration remains.

1) This looks like a bug (caused by a not-helpfull macOS helper-wizard). Can it be worked around?

2) How do I get to a proper configuration from here?

MuhammedZakir commented 2 years ago

This is probably related to a long-standing bug when using non-US keyboard layout. You can see lots of similiar issues when searching [1]. Current working solution (until this is fixed) is to find the correct keycode using EventViewer and use that.

[1] https://github.com/pqrs-org/Karabiner-Elements/search?q=swapped+keys&type=issues.

adamal commented 2 years ago

The workaround doesn't work for my need, I think, because the key I am trying to send (/) doesn't exist without modifier on my keyboard, so there is no way to type it directly.

I did find another workaround that avoids Karabiner Elements altogether, using Shortcuts.app instead.

I created a Shortcut containing a single "Run Javasscript..." action, containing this

function run(input, parameters) {
  Application("System Events").keystroke("/")
}

I then bound this (via settings icon, upper right corner of app) to shift+7 (displays as shift+/). For the microscript to work, Shortcuts.app needs to be allowed in System Preferences > Security and Privacy > Privacy (tab) > Accessibility

MuhammedZakir commented 2 years ago

The workaround doesn't work for my need, I think, because the key I am trying to send (/) doesn't exist without modifier on my keyboard, so there is no way to type it directly.

If the modifier is shift, you can use complex modification. If it is any other modifiers (e.g. option), then it's better not to use it as it may interfere with system's or applications' keyboard shortcuts.

I did find another workaround that avoids Karabiner Elements altogether, using Shortcuts.app instead. --snip--

You can do the same in KE (but not directly):

{
  "description": "Shift+7 => /",
  "manipulators": [{
    "from": {
      "key_code": "7",
      "modifiers": {
        "mandatory": [ "shift" ]
      }
    },
    "to": [{
      "shell_command": "osascript -e 'tell application \"System Events\" to keystroke \"/\"' "
    }]
  }]
}

[1] https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/to/shell-command/ [2] If you are more familiar with JSX, you can pass -l JavaScript to osascript. E.g.

osascript -l JavaScript -e 'Application("System Events").keystorke("/")'

Don't forget to escape " when using them in shell_command.