MacOS HammerSpoon - 2 Button Script

Hey, I quickly translated the AHK Script from Hadronox / Rezel to a MacOS HammerSpoon Script.

local toggle1 = false
local toggle2 = false

hs.hotkey.bind({}, "1", function()
  toggle1 = not toggle1
  if toggle1 then
    hs.timer.doWhile(function() return toggle1 end, function()
      hs.eventtap.keyStroke({"cmd"}, "1")
      hs.timer.usleep(150 * 1000)
    end)
  end
end)

hs.hotkey.bind({}, "2", function()
  toggle2 = not toggle2
  if toggle2 then
    hs.timer.doWhile(function() return toggle2 end, function()
      hs.eventtap.keyStroke({"cmd"}, "2")
      hs.timer.usleep(150 * 1000)
    end)
  end
end)


1 Like

@Kaehfin this is exactly what I was looking for today. I installed Hammerspoon and did the Hello World so I know it’s working. I tried saving this in the .hammerspoom/Spoons path but couldn’t get it to load.

I put it in the init.lua and reloaded and see:

2023-02-21 07:39:07: – Loading extension: hotkey
2023-02-21 07:39:07: 07:39:07 hotkey: Enabled hotkey 1
2023-02-21 07:39:07: hotkey: Enabled hotkey 2
2023-02-21 07:39:07: – Done.

I tried testing this in a text editor and pressing 1 or cmd 1 doesn’t seem to spam the 1 key. Am I doing something wrong?