Move mouse to active window via keyboard shortcut

16 Feb 2026

Position mouse (using Kinesis + Alfred)

Goal

Move the mouse cursor to the center of the currently focused window after switching apps (e.g. via Cmd+Tab), using a keyboard shortcut.
Particularly needed when operating across 4 monitors!

Using:

Steps

Step 1 — Hammerspoon function

Open ~/.hammerspoon/init.lua and add:

-- =========================
-- Mouse warp to focused window
-- =========================
function warpToFocusedWindow()
  local win = hs.window.focusedWindow()
  if not win then
    hs.alert.show("No focused window")
    return
  end
  local f = win:frame()
  hs.alert.show("Mouse Warped")
  hs.mouse.absolutePosition({
    x = f.x + f.w / 2,
    y = f.y + f.h / 2
  })
end

Reload Hammerspoon config.

Step 2 — Alfred workflow

  1. Open Alfred Preferences
  2. Go to Workflows → New Workflow
  3. Name it something like: “Warp Mouse to Window”

Add a trigger:

  • Hotkey Trigger
  • Shortcut: Cmd 3
  • Set it to “Pass through to workflow”

Add an action:
- Run Script
- Language: /bin/bash
- Script:

/usr/bin/osascript -e 'tell application "Hammerspoon" to execute lua code "warpToFocusedWindow()"'

Now, Hotkey → Run Script

links

social