nix-darwin icon indicating copy to clipboard operation
nix-darwin copied to clipboard

Possible to configure custom keybindings?

Open skogsbrus opened this issue 1 year ago • 2 comments

👋 First off, thanks for the awesome work! It's been a joy to be able to reuse most of my existing NixOS configurations on my new Mac.

One thing I've got configured on Linux systems which I love, is custom keybindings. I often write Swedish, but I use a US layout. Custom keybindings let me keep a US layout, while also supporting the Swedish characters å,ä,ö. See below for how I've got this configured in NixOS.

On Mac, I haven't been able to set up anything similar so I'm stuck with using Ctrl-Space to switch between US & Swedish layouts. Anyone have any ideas if something equivalent could be possible?

The most similar thing that I've found is https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/TextDefaultsBindings/TextDefaultsBindings.html, but IIRC it's not a system level keybinding but only works for apps that support Cocoa.

# sys/keyboard/symbols/us-swedish
xkb_symbols "us-swedish" {
    name[Group1]="English (US, with åäö)";

    include "us(basic)"
    include "level3(ralt_switch)"

    //           default               shift            altgr            shift altgr
    key <AD11> {[bracketleft,          braceleft,       aring,           Aring ]};
    key <AC10> {[semicolon,            colon,           odiaeresis,      Odiaeresis ]};
    key <AC11> {[apostrophe,           quotedbl,        adiaeresis,      Adiaeresis ]};
};
# configuration.nix

  services.xserver = {
     enable = true;

      # Add custom keyboard layout
      extraLayouts.us-swedish = {
        description = "US layout with alt-gr Swedish";
        languages = [ "eng" ];
        symbolsFile = ./keyboard/symbols/us-swedish;
      };

      # Configure keymap in X11
      layout = "us-swedish";
      xkbOptions = "caps:escape";
  };

skogsbrus avatar Jun 21 '23 09:06 skogsbrus

See https://daiderd.com/nix-darwin/manual/index.html#opt-system.keyboard.enableKeyMapping, and the linked keyboard.nix file. These might help you out if you choose to go down the hidutil path:

  • https://rakhesh.com/mac/using-hidutil-to-map-macos-keyboard-keys/
  • https://hidutil-generator.netlify.app/
  • https://developer.apple.com/library/archive/technotes/tn2450/_index.html

Alternatively, you can use Ukelele to create a custom keymap, but I'm not sure how you could integrate that with nix.

kothavade avatar Jul 31 '23 01:07 kothavade

On macOS, if you hold press and hold a key, it should show all the accented variants of that key, so possibly you could keep the US layout on by default:

https://support.apple.com/en-au/guide/mac-help/mh27474/13.0/mac/13.0

Otherwise, if this is not sufficient for you, I would suggest looking into Karabiner-Elements which as far as I know is the best in class macOS app for any kind of input/keyboard remapping.

https://daiderd.com/nix-darwin/manual/index.html#opt-services.karabiner-elements.enable https://karabiner-elements.pqrs.org/

nix-darwin can install and run Karabiner-Elements for you, but currently it doesn't manage any of the configuration which can be managed in the app.

Enzime avatar Jul 31 '23 01:07 Enzime