Skip to content

Instantly share code, notes, and snippets.

@gerwld
Last active November 16, 2025 18:02
Show Gist options
  • Select an option

  • Save gerwld/e23a0dd1d1a18e92bee91132e9484a69 to your computer and use it in GitHub Desktop.

Select an option

Save gerwld/e23a0dd1d1a18e92bee91132e9484a69 to your computer and use it in GitHub Desktop.
MacOS HammerSpoon config for brightness / volume / etc keys on win keyboard
-- arr to track timers for repeating keys
local repeatTimers = {}
-- function to start repeating an action
local function startRepeating(key, action)
-- run action immediately on press
action()
-- if already running, skip
if repeatTimers[key] then return end
-- Start repeating every 0.1 seconds
repeatTimers[key] = hs.timer.new(0.1, function()
action()
end)
repeatTimers[key]:start()
end
-- Function to stop repeating an action
local function stopRepeating(key)
if repeatTimers[key] then
repeatTimers[key]:stop()
repeatTimers[key] = nil
end
end
-- Function to press system keys (brightness, volume, media, etc.)
local function pressSystemKey(key)
hs.eventtap.event.newSystemKeyEvent(key, true):post()
hs.eventtap.event.newSystemKeyEvent(key, false):post()
end
-- Function to type text
local function typeText(text)
hs.eventtap.keyStrokes(text)
end
-- Key bindings (F1–F7)
local keyBindings = {
{key = "f1", action = function() pressSystemKey("BRIGHTNESS_DOWN") end}, -- 144
{key = "f2", action = function() pressSystemKey("BRIGHTNESS_UP") end}, -- 145
{key = "f3", action = function() pressSystemKey("SOUND_DOWN") end},
{key = "f4", action = function() pressSystemKey("SOUND_UP") end},
{key = "f5", action = function() pressSystemKey("PLAY") end},
{key = "f6", action = function() pressSystemKey("PREVIOUS") end},
{key = "f7", action = function() pressSystemKey("NEXT") end},
}
-- Bind F1–F7 keys
for _, binding in ipairs(keyBindings) do
hs.hotkey.bind({}, binding.key,
function() startRepeating(binding.key, binding.action) end,
function() stopRepeating(binding.key) end
)
end
-- Bind Insert key (keycode 114) to copy password to clipboard -- modals only
local insertPassword = "pwd123"
local insertEvent = hs.eventtap.new({hs.eventtap.event.types.keyDown}, function(event)
if event:getKeyCode() == 114 then
hs.pasteboard.setContents(insertPassword)
hs.alert.show("pwd copied to clipboard")
return true -- block original Insert behavior
end
return false
end)
insertEvent:start()
-- Remap backslash "\" key to Enter/Return
-- Fast remap: "\" → Enter
local tap = hs.eventtap.new({hs.eventtap.event.types.keyDown, hs.eventtap.event.types.keyUp}, function(e)
if e:getKeyCode() == hs.keycodes.map["\\"] then
-- Replace with Return key event at the same phase (down or up)
local isDown = (e:getType() == hs.eventtap.event.types.keyDown)
hs.eventtap.event.newKeyEvent({}, "return", isDown):post()
return true -- block original "\"
end
return false
end)
tap:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment