HomeApplekeyboard - Unable to Swap cmd with choice with left/proper arrow keys...

keyboard – Unable to Swap cmd with choice with left/proper arrow keys utilizing hammerspoon


I’m making an attempt to create a lua script to work with hammerspoon which intends to swap cmd+left/proper arrow keys to behave like choice+left/proper arrow keys however it would not appear to work. Right here is the script

-- outline the important thing codes for command, choice, left arrow, and proper arrow
-- native cmdKey = 0x37
-- native optKey = 0x3A
-- native leftArrow = 0x7B
-- native rightArrow = 0x7C
native keyCodes = hs.keycodes.map
native cmdKey = keyCodes['cmd']
native optKey = keyCodes['alt']
native leftArrow = keyCodes['left']
native rightArrow = keyCodes['right']

-- outline a variable to maintain observe of whether or not the keys have been swapped
native keysSwapped = false

-- outline a operate to deal with key occasions
operate handleKeyEvent(occasion)
    if occasion:getType() ~= hs.eventtap.occasion.varieties.keyDown then
        return false
    finish
    
    native keyCode = occasion:getKeyCode()
    native flags = occasion:getFlags()
    print("keyCode: " .. keyCode)
    for k, v in pairs(flags) do
      print("    " .. k .. ": " .. tostring(v))
    finish

    -- verify if the left or proper arrow key's pressed together with command or choice
    if (keyCode == leftArrow or keyCode == rightArrow) and flags.cmd then
        -- swap the command and choice flags
        flags.cmd, flags.alt = flags.alt, flags.cmd
        -- replace the variable to point that the keys have been swapped
        keysSwapped = true
    finish
    
    print("keysSwapped: " .. tostring(keysSwapped))
    for k, v in pairs(flags) do
      print("    " .. k .. ": " .. tostring(v))
    finish
    -- if some other key's pressed together with command or choice, revert the swap
    if (keysSwapped and (not flags.cmd or (keyCode ~= leftArrow and keyCode ~= rightArrow))) then
        -- swap the command and choice flags again
        flags.cmd, flags.alt = flags.alt, flags.cmd
        -- reset the variable to point that the keys are now not swapped
        keysSwapped = false
    finish

    -- create a brand new occasion with the modified flags
    -- native newEvent = hs.eventtap.occasion.newKeyEvent(flags, keyCode, occasion:isDown())
    -- native newEvent = hs.eventtap.occasion.newKeyEvent(flags, keyCode, occasion:getType() == hs.eventtap.occasion.varieties.keyDown)
    native newEvent = hs.eventtap.occasion.newKeyEvent(flags, keyCode, occasion:getType() == hs.eventtap.occasion.varieties.keyDown, false)


    -- inject the brand new occasion into the system occasion stream
    return true, {newEvent}
finish

-- create a brand new occasion faucet to seize key occasions
native eventTap = hs.eventtap.new({hs.eventtap.occasion.varieties.keyDown, hs.eventtap.occasion.varieties.keyUp}, handleKeyEvent)

-- begin the occasion faucet
eventTap:begin()

The script above is written by chatgpt(attributable to lack of awareness on lua and hammerspoon) and I’ve modified it with some digging from hammerspoon web site. I’ve printed logs to see whether or not swap is going on and it does appear to occur, however when cmd+arrow keys are clicked collectively the cursor would not skip a phrase.
I’m utilizing lua fairly than some other methodology as a result of ultimately I need to have this swap performance solely when I’m utilizing the shortcut in textual content fields and to my restricted data lua and hammerspoon are acceptable for that.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments