macoshammerspoonkarabiner

How to call kill and yank function from Hammerspoon or Karabiner


OSX default kill(ctrl + k) and yank(ctrl + y) function doesn't work when using Thai or Korean input mode. Other key bindings for cursor movements are also disabled.

So I'm trying to emulate the function by using Hammerspoon. But I couldn't find out how to call the kill and yank function from hammerspoon.

I could only implemente for cursor movements by this code.

local EmacsKeyMap = hs.hotkey.modal.new()

local function sendKey(mods, key)
   return function()
      hs.eventtap.keyStroke(mods, key, 10000)
   end
end

local function bindToEmacsKeyMap(mods, key, func)
   EmacsKeyMap:bind(mods, key, func, nil, func)
end

bindToEmacsKeyMap({'ctrl'}, 0, sendKey({'command'}, 'left')) -- C-a
bindToEmacsKeyMap({'ctrl'}, 14, sendKey({'command'}, 'right')) -- C-e
bindToEmacsKeyMap({'ctrl'}, 11, sendKey({}, 'left')) -- C-b
bindToEmacsKeyMap({'ctrl'}, 3, sendKey({}, 'right')) -- C-f
bindToEmacsKeyMap({'ctrl'}, 45, sendKey({}, 'down')) -- C-n
bindToEmacsKeyMap({'ctrl'}, 35, sendKey({}, 'up')) -- C-p
bindToEmacsKeyMap({'ctrl'}, 4, sendKey({}, 'delete')) -- C-h

EmacsKeyMap:enter()

I googled some hammerspoon scripts for emacs emulation, but they are just using key strokes for the function.

Is there a way to call the kill and yank directly from a Hammerspoon script?

Edit 2019/02/21

If Karabiner or other applications can accomplish same behavior, I'm going to use it.


Solution

  • I couldn't figure out how to make it with Hammerspoon, but by following explanation of this website I edited ~/Library/KeyBindings/DefaultKeyBinding.dict. Then all text key binding works as I expected.

    {
        "^ฟ" = "moveToBeginningOfParagraph:"; /* ctrl-a */
        "^ฤ" = "moveToBeginningOfParagraphAndModifySelection:"; /* ctrl-A */
        "^ิ" = "moveBackward:"; /* ctrl-b */
        "^ฺ" = "moveBackwardAndModifySelection:"; /* ctrl-B */
        "^ก" = "deleteForward:"; /* ctrl-d */
        "^ำ" = "moveToEndOfParagraph:"; /* ctrl-e */
        "^ฎ" = "moveToEndOfParagraphAndModifySelection:"; /* ctrl-E */
        "^ด" = "moveForward:"; /* ctrl-f */
        "^โ" = "moveForwardAndModifySelection:"; /* ctrl-F */
        "^้" = "deleteBackward:"; /* ctrl-h */
        "^า" = "deleteToEndOfParagraph:"; /* ctrl-k */
        "^ส" = "centerSelectionInVisibleArea:"; /* ctrl-l */
        "^ื" = "moveDown:"; /* ctrl-n */
        "^์" = "moveDownAndModifySelection:"; /* ctrl-N */
        "^น" = (
            "insertNewlineIgnoringFieldEditor:",
            "moveBackward:",
        ); /* ctrl-o */
        "^ย" = "moveUp:"; /* ctrl-p */
        "^ญ" = "moveUpAndModifySelection:"; /* ctrl-P */
        "^ะ" = "transpose:"; /* ctrl-t */
        "^ฮ" = "pageDownAndModifySelection:"; /* ctrl-V */
        "^อ" = "pageDown:"; /* ctrl-v */
        "^ั" = "yank:"; /* ctrl-y */
    }