Hacker News new | past | comments | ask | show | jobs | submit login
Mac Keyboard with Hidutil (amitp.blogspot.com)
123 points by ibobev on April 13, 2023 | hide | past | favorite | 43 comments



I think Karabiner(-Elements) is way more popular and probably more flexible, but I've had a lot of success with Ukelele for 6-7 years. I exported one layout and just copy it to each new macbook. Specifically it lets me set `option` as a dead key so I can have `opt+i`, `opt+j`, etc... shortcuts in editors.

https://software.sil.org/ukelele/


It is more flexible but it's a bloatware. I had to spend some time to uninstall it completely.

https://karabiner-elements.pqrs.org/docs/manual/operation/un...


> bloatware

That word doesn't mean what you think it means. What those instructions explain is (1) removing macOS permissions, (2) removing logs/temporary files, (3) removing config. These are normal things that some may want to keep after uninstalling the program. Calling it bloatware is misleading and dilutes the meaning for things that are actually bloatware.


The Mac keyboard layout system is good; it's basically a tree of states, so it's both simple and capable. Ukelele is a decent layout editor; the only down side I recall is that it's not easy to create chains analogous to X11 Compose. Editing or generating keyboard layout XML directly is also possible.

Karabiner and hidutil are at a lower level; they modify the key codes that are the input to layout.


Interesting, but if I'm understanding this correctly, this can only do straight remaps. I'll stick with Karabiner Elements for its ability to make a key do one thing when tapped (e.g., send a keystroke like ESC) and a different thing when held (e.g., act as a modifier like CTRL).


Hold-tap is phenomenal in QMK if you want it built in to a keyboard you're building too.

https://qmk.fm


Karabiner has this. I never could use this. I've tried mapping A to control when held down, but there's always false negatives because I hit control too fast. I've also tried many other keys and different timings, but nothing works for me.

Instead, I map a key to one thing when pressed alone and another when pressed with other keys. Works so much better. I can't live without this now. I really wish QMK had this functionality, but I think it's only available in Karabiner.


QMK does have this, unless I’ve misunderstood your use case. My QMK keyboard has CAPS mapped to ESC when tapped, CTRL when pressed with other keys. In my keymap.c file, I’ve put my caps key as just:

MT(MOD_LCTL, KC_ESC)


I ended up putting arrow keys in my 60%, which shifted the layout (PCB limitations) to where I ended up having to sacrifice my / and ? for a shift key (to fit the empty space). So I ended up having to do something similar. Holding it down and pressing other keys makes it function as a shift. Clicking it by itself yields a period, and clicking it by itself with the other shift does a question mark. QMK is freaking awesome.

RSFT_T(KC_SLSH)


Oh! Good to know! And your use-case is exactly what I'm using Karabiner for.


Oh yeah I couldn't make it work with letters. Rather I added tap behavior to modifiers. In QMK my command keys are Esc and Enter when tapped, shifts are parentheses (like the space cadet keyboard), controls (outside of A and ;) are single and double quote, Options are braces, and hypers are angle brackets (which I should probably change to brackets since I don't really write JSX/TSX/HTML much any more)


Yeah, Karabiner seems to be unmatched in terms of customization for power users. The main problem with straight key remapping is that you have to deal with the application layer having different keybindings for the same actions, even when you have the appropriate keys swapped. Same for navigating the OS if you're interested in duplicating Windows navigation functions in Mac ( Command+E to open Finder, Command+S for Spotlight Search, Ctrl+Shft+Esc to open Activity Monitor )

I spent a long time creating a giant karabiner config file for myself, but recently an MDM update at my work broke the virtual keyboard driver that Karabiner requires. I haven't figured out a way to install it. I may have to go begging to IT.


Do you think I can get something like this for windows or libinput/wayland?


PowerToys for Windows has a keyboard remapper. It has nowhere near the power of karabiner, but otoh you don't have to wade through karabiner's config file.


Kmonad or keyd. Keyd is Linux only but Kmonad is cross platform but harder to configure IMO. Both work everywhere in Linux including TTYs.


Do either handle the modifier bits when you remap? So that they are not set unless you want to?


If you do desire "external keyboards, more than one, slightly different configurations for each one", ... then at that point I think it would be worth considering the benefits of a keyboard where the keyboard firmware itself can be customised.

With the customisable keyboard firmware, your tweaks would remain on the keyboard, & wouldn't need any setup on the OS.


Config being saved in firmware I think is probably the most underrated boon of enthusiast keyboards. No shaky hacks or badly written background daemons, just plug it in anywhere there’s USB and the board acts as expected.


The downside of this approach is that you can’t easily do “one size fits all, inclusive of built-in laptop keyboard” mods.

Since my configuration files follow me from computer to computer anyway, it’s easier to just manage all the mods in one place and rely on the daemon. If I was doing support though, I would definitely go the firmware route.


Yeah that's fair, and not all daemons are equal — something like Karabiner for instance is manageable, but the software required for the various functions on big name (e.g. Logitech) peripherals can be downright horrendous.


Agreed. I do have that, but I also want to use the same keyboard with Mac, Linux, Windows. So far I've been switching keyboard configurations on the keyboard every time I use a different OS, but I'd like to move that to the OS so that it's automatic.


Hi, I'm able to successfully do key remappings with a com.local.KeyRemapping.plist file in the LaunchAgents folder. But I want to do a "one-to-many" remap, not just a 1-1 remap. I.E remap "a" to "abc". Is there a way to do that? I haven't stumbled across a way to output a string of characters in the "HIDKeyboardModifierMappingDst": construct. Is there a way to do that? Thanks,


OK, I finally figured out how to do this with Better Touch Tool. At first I couldn't get it to work, it was passing the original keystroke through and then trying to delete it, which doesn't work in all apps I need. What I was doing wrong in BetterTouchTool: I was using the "Keyboard Sequences" instead of the "Keyboard Shortcuts" construct in the configuration editor. The "Keyboard Shortcuts" in BetterTouchTool does exactly what I want. I blame COVID brain fog on struggling with this for so long.


I wish you could get an Hyper key with built-in utilities without needing to install anything else. Unfortunately hidutil can't map multiple keys to a single key.


I wrote a pretty simple wrapper around this that gives the keys easy names and can store the configuration in a human readable format: https://github.com/ivangreene/keymap

Was unaware of the --matching option at the time, could be an interesting extension but I personally have no need for it


Thank you! That looks great.


I have a script I use with Starcraft2 (on Mac I should know better..)

hidutil property --set '{ "UserKeyMapping": [{ "HIDKeyboardModifierMappingSrc": 0x70000002B, "HIDKeyboardModifierMappingDst": 0x7000000E1 }, { "HIDKeyboardModifierMappingSrc": 0x700000039, "HIDKeyboardModifierMappingDst": 0x70000002B }, { "HIDKeyboardModifierMappingSrc": 0x7000000E1, "HIDKeyboardModifierMappingDst": 0x7000000E2 }] }'

It remaps tab->L.shift, capslock->tab, L.shift->L.alt Why? Using TheCoreLite, so I can hit tab/caps/shift labelled keys with pinky and cmd with thumb and reach a good number of hotkeys including function keys.

On Windows, the PowerToys keyboard manager can do the same with a nice config UI.


Oh, man, thanks for this. I've been lamenting that Cmd is essentially dead without remapping it, and this is the last blocker for me adopting TheCore.


On Windows, you use AHK to do that and way more. You know about Caps being Esc alone and Control when chorded?

Likewise, AHK can turn the Alt keys in Home/End when pressed alone, and the Shift keys in PageUp/PageDown.

One handed Alt-PageUp on the left hand side of the keyboard becomes pressing and holding the left Alt then pressing and releasing the left Shift.


I just happened to make a post about this today, too, in the form of a quick tutorial with Automator to turn the hidutil commands into an app that can be configured in launch on login system settings, if desired:

https://medium.com/ryan-hanson/key-remapping-built-into-maco...


"On the laptop keyboard, some media keys should be function keys but other media keys should stay media keys. When using fn I want to get the other version of the key."

Wow, this is exactly how it should be! Who cares about F1 and F2, let them be brightness controls! A button for "full screen" on F5? No thank you, that should always be "refresh".

Btw, I switch a lot between MacOS and Linux, and my Logitech MX master can be Mac or Windows/Linux. It's maddening that the thing has a "super" and and "cmd" key. They should have been the same key. Alt and Opt should also have been the same.


Most laptops I've used have an "Fn Lock" key or mechanism. That's one of the first "out of the box" items I enable. If it lacked one, that'd be a "back in the box" experience for me.


Sure, but it does not let you combine brightness controls with F5. It’s either or, the functions or the F keys. This let’s you mix.


Yes, good stuff. Also useful if you have an external PC keyboard and want "INS(ERT)" (0x700000049) to do something sensible...


Hiduntil gets job done for remapping keys. The only downside for me is that configuration lost after keyboard was disconnected.


I haven't tried this yet but the configuration on https://www.digihunch.com/2022/11/key-mapping-on-external-pc... looks like it will run when the keyboard is connected.


Not to be confused with `hdiutil`.



I map my right option key to ctrl, because I use ctrl + arrows way more than option


The first comment on the post includes the comment: “Also, how lost are you when you use someone else's laptop or keyboard and it doesn't act like yours?” which is a big part of the reason I don’t do fancy stuff remapping keyboards and the like. The only real customization I do is to turn off caps lock because only psychopaths leave caps lock enabled.¹

1. From which we can deduce that the person who set the default for the caps lock key being enabled is a psychopath.


It should have 2019 in the title.


It's a blog post from yesterday.


You are right. I confused it with a linked blog post.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: