6

I need a custom keyboard shortcut for back slash \.

I have created ~/Library/KeyBindings/DefaultKeyBinding.dict and put this in it:

{
    "@ö" = ("insertText:", "\\");
}

It is supposed to trigger back slash when pressing + ö (Yes, I have an "ö" character on my keyboard).

When trying to source the file, I get this error:

-bash: DefaultKeyBinding.dict: line 2: syntax error near unexpected token `('
-bash: DefaultKeyBinding.dict: line 2: `    "@ö" = ("insertText:", "\\");'

What's wrong with the syntax? Is there a better way?

2
  • 3
    From a brief web search for "DefaultKeyBinding.dict", according to xahlee.info/kbd/osx_keybinding.html: "only application that [use] Cocoa text system will support this." This is not a shell file, and will not work when 'source'ed via bash.
    – mtklr
    Dec 17, 2015 at 0:31
  • @mtklr You're probably right. It's strange because a number of answers on StackExchange recommended this method for creating custom key bindings in OS X – that's why I got involved with it in the first place.
    – P A N
    Dec 21, 2015 at 11:55

3 Answers 3

3
+50

To enable CmdÖ -> \ just remap the key combo with Karabiner. The shortcut you need isn't pre-defined so you have to write your own:

  1. Download, install and open Karabiner
  2. Open in the menulet Karabiner -> Preferences
  3. Open the tab Misc & Uninstall
  4. Hit the button Open private.xml
  5. Open the file private.xml with an appropriate editor
  6. Enter the customized keycode

    Example 1 (cmdÖ -> \):

    <?xml version="1.0"?>
    <root>
        <item>
          <name>Command-Ö to Backslash</name>
          <appendix>(Change Command-Ö to \)</appendix>
          <identifier>remap.command_ö_to_backslash</identifier>
          <autogen>__KeyToKey__ KeyCode::GERMAN_O_UMLAUT, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_COMMAND, KeyCode::KEY_7, ModifierFlag::SHIFT_L | ModifierFlag::OPTION_L</autogen>
        </item>
    </root>
    

    Example 2 (cmd_RÖ -> \ cmd_R is the right command key only, cmd_LÖ doesn't have an output!):

    <?xml version="1.0"?>
    <root>
        <item>
          <name>Command_R-Ö to Backslash</name>
          <appendix>(Change Command_R-Ö to \)</appendix>
          <identifier>remap.command_r_ö_to_backslash</identifier>
          <autogen>__KeyToKey__ KeyCode::GERMAN_O_UMLAUT, ModifierFlag::COMMAND_R, KeyCode::KEY_7, ModifierFlag::SHIFT_L | ModifierFlag::OPTION_L</autogen>
        </item>
    </root>
    

    Example 3 (cmd_R -> \ you can't use the right command key for any other purpose then):

    <?xml version="1.0"?>
    <root>
        <item>
          <name>Command_R to Backslash</name>
          <appendix>(Change Command_R to \)</appendix>
          <identifier>remap.command_r_to_backslash</identifier>
          <autogen>__KeyToKey__ KeyCode::COMMAND_R, KeyCode::KEY_7, ModifierFlag::SHIFT_L | ModifierFlag::OPTION_L</autogen>
        </item>
    </root>
    

    Example 4 (cmd_R -> \ similar to example 3 but you can use the right command key as normal command key-> hitting cmd_R yields \ but cmd_RS saves files):

    <?xml version="1.0"?>
    <root>
        <item>
          <name>When you type Command_R only, send Backslash</name>
          <appendix>(Change Command_R lazy to \)</appendix>
          <identifier>remap.command_r_lazy_to_backslash</identifier>
          <autogen>__KeyOverlaidModifier__ KeyCode::COMMAND_R, KeyCode::VK_LAZY_COMMAND_R, KeyCode::KEY_7, ModifierFlag::SHIFT_L | ModifierFlag::OPTION_L</autogen>
        </item>
    </root>
    
  7. Save the file

  8. Open in the menulet Karabiner -> Preferences the tab Change Key
  9. Hit the ReloadXML button
  10. Enable the remapping. It should be listed at the beginning.
3
  • Thanks very much for this! I especially found it powerful to bind cmd_R per your suggestion.
    – P A N
    Dec 22, 2015 at 9:21
  • @Winterflags By accident I made a forward_slash instead of a back_slash in all of the appendix sections. I amended that. It's only a cosmetic problem though.
    – klanomath
    Dec 22, 2015 at 9:36
  • This stopped working for me – I haven't changed the XML and it worked in the past. Just curious if there's some obvious reason for it not working (e.g. the syntax changed)?
    – P A N
    Jun 7, 2018 at 17:24
1

Why don't you use Karabiner ?

A powerful and stable keyboard customizer for OS X.

  • You can easily customize from prepared settings.

  • You can also add your own settings by XML.

https://pqrs.org/osx/karabiner/

3
  • 1
    Note that Karabiner doesn't work on macOS Sierra, and the authors have indicated that substantial changes will be necessary before it will work on that OS.
    – Peter
    Oct 15, 2016 at 0:05
  • @Peter "answered Dec 21 '15" - Sierra September 16
    – StrawHara
    Oct 16, 2016 at 16:09
  • Thanks MrMojoRinsin - I've always wondered what these "date" things were. Seriously though, I was pointing this out so that anyone today (or in the near future) who might stumble across this post would know that your answer doesn't apply for macOS Sierra (yet).
    – Peter
    Oct 17, 2016 at 20:55
1

From a brief web search for DefaultKeyBinding.dict, according to this page:

Only application that [use] Cocoa text system will support this.

This is not a shell file, and will not work when 'source'ed via bash.

Source: @mtklr comment.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .