I want to solve Emacs-like key bindings in Office (Word) with Hammerspoon (Lua)

The most important thing I want to do is to realize Emacs-like key bindings in Office (Word), and after a lot of research, using Hamperspoon seems to be easy.

https://qiita.com/swdyh/items/04f7da8c1209a067add5 (Japanese site)

I checked this site and this original cited site and set up the initial configuration as follows (in LUA language, in init.lua).

My question is that the code below does most of what I want it to do, but the Ctrl-k at the end says However, the last line of code, Ctrl-k, does not allow me to delete the cursor and copy the deleted data to the clipboard. It seems that keyCode

('e', {'shift', 'ctrl'})()

is not working.

Any solutions or advice would be greatly appreciated! Thank you in advance.

local function keyCode(key, modifiers)
   modifiers = modifiers or {}
   return function()
      hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), true):post()
      hs.timer.usleep(1000)
      hs.eventtap.event.newKeyEvent(modifiers, string.lower(key), false):post()      
   end
end

local function remapKey(modifiers, key, keyCode)
   hs.hotkey.bind(modifiers, key, keyCode, nil, keyCode)
end

local function disableAllHotkeys()
   for k, v in pairs(hs.hotkey.getHotkeys()) do
      v['_hk']:disable()
   end
end

local function enableAllHotkeys()
   for k, v in pairs(hs.hotkey.getHotkeys()) do
      v['_hk']:enable()
   end
end

local function handleGlobalAppEvent(name, event, app)
   if event == hs.application.watcher.activated then
      -- hs.alert.show(name)
      if name ~= "iTerm2" then
         enableAllHotkeys()
      else
         disableAllHotkeys()
      end
   end
end

appsWatcher = hs.application.watcher.new(handleGlobalAppEvent)
appsWatcher:start()

-- cursor movement
remapKey({'ctrl'}, 'f', keyCode('right'))
remapKey({'ctrl'}, 'b', keyCode('left'))
remapKey({'ctrl'}, 'n', keyCode('down'))
remapKey({'ctrl'}, 'p', keyCode('up'))

-- Text Editing
remapKey({'ctrl'}, 'w', keyCode('x', {'cmd'}))
remapKey({'ctrl'}, 'y', keyCode('v', {'cmd'}))

-- Command
remapKey({'ctrl'}, 's', keyCode('f', {'cmd'}))
remapKey({'ctrl'}, '/', keyCode('z', {'cmd'}))
remapKey({'ctrl'}, 'g', keyCode('escape'))

-- Page scroll
remapKey({'ctrl'}, 'v', keyCode('pagedown'))
remapKey({'alt'}, 'v', keyCode('pageup'))
remapKey({'cmd', 'shift'}, ',', keyCode('home'))
remapKey({'cmd', 'shift'}, '.', keyCode('end'))

-- Delete the area after the cursor and copy it to the clipboard.
local function keyCtrlK()
  keyCode('e', {'shift', 'ctrl'})()
  keyCode('x', {'cmd'})()
end
hs.hotkey.bind({'ctrl'}, 'k', keyCtrlK, nil, keyCtrlK)


Read more here: https://stackoverflow.com/questions/66338572/i-want-to-solve-emacs-like-key-bindings-in-office-word-with-hammerspoon-lua

Content Attribution

This content was originally published by nono3 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: