Skip to main content

Karabiner

Karabiner is an app that functions as a keyboard remapper.

If you would like to view my goku document to use as a reference, download it here.


Getting Started

At the top of the Goku keybind config document, copy and paste the following comment block. This will serve as a quick reference for knowing the proper key codes to use to implement modifiers. It also provides links to a few solid examples of other Goku scripts.

;; !  | means mandatory -   modifier(s) alone when pressend change behavior
;; # | means optional - modifiers are optional (but atleast one necessary)

;; :!Ca is keycode :a and prefix a with !C

;; C | left_command
;; T | left_control
;; O | left_option
;; S | left_shift
;; F | fn
;; Q | right_command
;; W | right_control
;; E | right_option
;; R | right_shift

;; ## | optional any
;; !! | command + control + optional + shift (hyper)

;; Install via brew: `brew install yqrashawn/goku/goku`

;; karabiner docs: ;; https://pqrs.org/osx/karabiner/json.html#complex_modifications-manipulator-definition
;; <other options> includes ~to_if_alone~, ~to_if_held_down~,~to_after_key_up~, ~to_delayed_action~ and ~parameters~.

;; (custom variables) & modifiers -> Advanced
;; https://github.com/yqrashawn/GokuRakuJoudo/blob/master/examples.org#custom-variable

;; Examples: https://github.com/yqrashawn/GokuRakuJoudo/blob/master/examples.org
;; https://gist.github.com/kaushikgopal/ff7a92bbc887e59699c804b59074a126
;; https://github.com/kchen0x/k-goku/blob/master/karabiner.edn

Immediately following the the comment block, we have our Templates block. These provide the script with useful shortcuts that tell Karabiner how to handle your commands when you append one of these predefined prefixes to the command. More on that soon.

:templates {
:alfred "osascript -e 'tell application id \"com.runningwithcrayons.Alfred\" to run trigger \"%s\" in workflow \"%s\" with argument \"%s\"'"
:km "osascript -e 'tell application \"Keyboard Maestro Engine\" to do script \"%s\"'"
:open "open \"%s\""
:beep "osascript -e 'beep'" ;; Play default beep
:purr "afplay /System/Library/Sounds/Purr.aiff" ;; Play the Purr sound
:tink "afplay /System/Library/Sounds/Tink.aiff" ;; Play the Tink sound
}

Let's break this apart.


:alfred "osascript -e 'tell application id \"com.runningwithcrayons.Alfred\" to run trigger \"%s\" in workflow \"%s\" with argument \"%s\"'"

The first word following the colon (in this case, alfred) serves as the identifier I will be able to use throughout my script. Everything else that follows that keyword is the bash script that will be run whenever it sees our keyword.

osascript - e - tells Terminal to run whatever you have in the single quotes following it as an Applescript. Is Applescript totally necessary for this? No. But I was learning it the time and I'm not going to bother with changing it now.

Applescript is a very weird programming language that aims to be as readable as possible, so it is mostly self explanatory, but the key elements are as follows:

  • com.runningiwithcrayons.Alfred - This is the Bundle Identifier for the application Alfred. To obtain the Bundle ID of any app, run the following bash command.


    mdls -name kMDItemCFBundleIdentifier -r SomeApp.app mdls -name kMDItemCFBundleIdentifier -r Alfred\ 5.app

    Of course, substitute the name of your app at the end. If the name of the app includes any spaces, you'll need to escape them with the ("\") before the space (as seen above), or by putting the name of the application in quotation marks ("Alfred 5.app").

  • %s - Each of these represent a piece of text that gets passed into the script.

    • Here is an example:


      [:f [:alfred "fan" "com.aaron.fancontrol"]]

      We'll explore this more soon, but you can see the :alfred is followed by two parameters--"fan" and "com.aaron.fancontrol".

      Here is what the premade :alfred command looks like when it is populated with the supplied parameters.

      tell application id \"com.runningwithcrayons.Alfred\" to run trigger \"fan\" in workflow \"com.aaron.fancontrol\" with argument \"nil\"

      Note that the final %s was populated with nil. This is because that particular command does not require an argument. Perhaps if I had multiple fans that I wanted to control with this, I would make the Alfred script itself accept an argument for "bedroom", "living room", or "kitchen".


Notes

  • For adding to wiki

    W + = : Create folder in docs

    W + - : Create markdown file in folder