Skip to content

Instantly share code, notes, and snippets.

@fjarlq
Last active January 21, 2025 09:47
Show Gist options
  • Select an option

  • Save fjarlq/d82e86fd61638f7519c6d21c1c5d3f19 to your computer and use it in GitHub Desktop.

Select an option

Save fjarlq/d82e86fd61638f7519c6d21c1c5d3f19 to your computer and use it in GitHub Desktop.
Hammerspoon script that fixes Ctrl-PgUp/Ctrl-PgDn, Ctrl-Left/Ctrl-Right, and Home/End in Safari
-- Remaps macOS keyboard shortcuts as follows. I need this mainly
-- because I'm so used to these keys in Chrome, but they are not built
-- into Safari and I have not found a builtin way in macOS to add
-- these keyboard shortcuts to Safari.
--
-- Intercepted shortcut Sends instead Desired function
-- -------------------- ------------- ----------------
-- Ctrl-PageUp Ctrl-Shift-Tab Show Previous Tab
-- Ctrl-PageDown Ctrl-Tab Show Next Tab
-- Ctrl-LeftArrow Ctrl-Shift-Tab Show Previous Tab
-- Ctrl-RightArrow Ctrl-Tab Show Next Tab
-- Home Cmd-UpArrow Scroll to beginning
-- End Cmd-DownArrow Scroll to end
local function bindHotkeyToKeyEvent(fromKeyMods, fromKey, toKeyMods, toKey, isrepeatable)
local eventDown = hs.eventtap.event.newKeyEvent(toKeyMods, toKey, true)
local eventUp = hs.eventtap.event.newKeyEvent(toKeyMods, toKey, false)
local pressedfn = function() eventDown:post() end
local releasedfn = function() eventUp:post() end
local repeatfn = nil
if isrepeatable then
repeatfn = function()
pressedfn()
hs.timer.usleep(hs.eventtap.keyRepeatInterval())
releasedfn()
end
end
hs.hotkey.bind(fromKeyMods, fromKey, pressedfn, releasedfn, repeatfn)
end
bindHotkeyToKeyEvent({'ctrl'}, 'pageup', {'ctrl', 'shift'}, 'tab', true)
bindHotkeyToKeyEvent({'ctrl'}, 'pagedown', {'ctrl'}, 'tab', true)
bindHotkeyToKeyEvent({'ctrl'}, 'left', {'ctrl', 'shift'}, 'tab', true)
bindHotkeyToKeyEvent({'ctrl'}, 'right', {'ctrl'}, 'tab', true)
bindHotkeyToKeyEvent({}, 'home', {'cmd'}, 'up', false)
bindHotkeyToKeyEvent({}, 'end', {'cmd'}, 'down', false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment