๐ฑ️ How to Move the Cursor Between Displays on a Mac Using a Keyboard Shortcut | Mac cursor shortcut | move mouse between displays Mac | multi-monitor Mac setup
Introduction
Are you a Mac user working with multiple monitors? If you’re constantly dragging your ๐ฑ️ cursor across screens, you’ve probably wondered: “Isn’t there a faster way?” There is! By using a lightweight automation tool called Hammerspoon and a bit of scripting, you can move your cursor between displays using a keyboard shortcut—no paid apps required and no tech wizardry needed.Let’s walk through how to set this up—boosting your productivity and making your dual (or triple) monitor workflow feel frictionless.
๐ Table of Contents
- ๐ฏ Introduction to Hammerspoon and Its Use Cases
- ⚙️ Setting Up Hammerspoon on Your Mac
- ๐ Writing the Script to Switch Cursor Displays
- ๐งช Testing and Fine-Tuning Your Shortcut
- ๐ ️ Extra Tips and Customization Options
- ✅ Conclusion
1. ๐ฏ Introduction to Hammerspoon and Its Use Cases
Hammerspoon is a powerful automation bridge between macOS and the Lua scripting language. It gives you total control over your desktop environment. Whether it’s window snapping, mouse movement, or workflow automation—Hammerspoon can do it all.One frequent problem it solves? Manually dragging your cursor from one screen to another. This is particularly annoying in setups with large or multiple displays. By assigning a shortcut to a cursor move action, you can jump from one screen to the next with a simple keystroke.
2. ⚙️ Setting Up Hammerspoon on Your Mac
Step-by-Step Installation
- Download Hammerspoon from its GitHub Releases page. Scroll down and under the Assets section, download the latest release (download the ZIP folder and unzip it).
- Unzip the file and drag the app to
/Applications
. - Launch Hammerspoon. It may ask for accessibility permissions—grant them under System Settings > Privacy & Security > Accessibility.
Create the init.lua
Configuration File
- Open Terminal and run:
- Use a code editor (like VS Code or
nano
) to openinit.lua
. Example: - Click the ๐จ Hammerspoon menu bar icon on the top-right side of the screen.
- Select Reload Config.
- A notification saying "Cursor Switch Loaded! ๐" should appear.
- Click any app window.
- Press Cmd + Shift + 2.
- The cursor should jump from one screen to the other—centered and smooth.
mkdir -p ~/.hammerspoon
cd ~/.hammerspoon
touch init.lua
code init.lua
3. ๐ Writing the Script to Switch Cursor Displays
Paste the following Lua code into yourinit.lua
file:
-- Function to get center of a screen
function getScreenCenter(screen)
local frame = screen:frame()
return { x = frame.x + frame.w / 2, y = frame.y + frame.h / 2 }
end
-- Identify screens
local screens = hs.screen.allScreens()
local mainScreen = hs.screen.primaryScreen()
local secondaryScreen = (#screens > 1) and screens[2] or nil
-- Function to switch cursor
function toggleScreens()
local currentScreen = hs.mouse.getCurrentScreen()
local targetScreen = (currentScreen:id() == mainScreen:id()) and secondaryScreen or mainScreen
if targetScreen then
local newPos = getScreenCenter(targetScreen)
hs.mouse.setAbsolutePosition(newPos)
else
hs.alert.show("No secondary screen detected! ๐")
end
end
-- Bind to shortcut (Cmd + Shift + 2)
hs.hotkey.bind({"cmd", "shift"}, "2", toggleScreens)
-- Notify when script loads
hs.alert.show("Cursor Switch Loaded! ๐")
๐ Want a different key combo?
Change the shortcut key here:hs.hotkey.bind({"cmd", "shift"}, "M", toggleScreens)
hs.hotkey.bind({"cmd", "shift"}, "M", toggleScreens)
As shown, we've used Cmd + Shift + M as the shortcut.
4. ๐งช Testing and Fine-Tuning Your Shortcut
✅ Reload the Config
After saving, reload the script:๐ Test Your Shortcut
5. ๐ ️ Extra Tips and Customization Options
๐ Change Key Bindings
Customize this:
hs.hotkey.bind({"cmd", "shift"}, "2", toggleScreens)
hs.hotkey.bind({"ctrl", "alt"}, "M", toggleScreens)
Pick keys that won’t interfere with other system functions.
๐งฉ Modularize Your Code
As your config grows, split scripts like this:
-- File: ~/.hammerspoon/cursorControl.lua
-- Require this in init.lua using: require("cursorControl")
This makes it easier to manage other automations like window snapping, volume control, etc.
๐งญ Handle Different Screen Layouts
If you have vertically stacked displays or different resolutions, customize the cursor's new position:
-- Instead of center, move cursor to top-left of target
local newPos = { x = targetScreen:frame().x + 100, y = targetScreen:frame().y + 100 }
Experiment to match your preferences.
6. ✅ Conclusion
Using a keyboard shortcut to move your cursor between monitors might seem like a small improvement—but it adds up over hundreds of interactions each day.With Hammerspoon and a few lines of Lua, you can eliminate one more annoyance and make your multi-monitor setup feel seamless and professional.
๐ Congrats! You've just leveled up your Mac productivity with an automation most users don't even know is possible.
Comments
Post a Comment