๐Ÿ–ฑ️ 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

  1. ๐ŸŽฏ Introduction to Hammerspoon and Its Use Cases
  2. ⚙️ Setting Up Hammerspoon on Your Mac
  3. ๐Ÿ” Writing the Script to Switch Cursor Displays
  4. ๐Ÿงช Testing and Fine-Tuning Your Shortcut
  5. ๐Ÿ› ️ Extra Tips and Customization Options
  6. ✅ 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

  1. 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).
  2. Unzip the file and drag the app to /Applications.
  3. Launch Hammerspoon. It may ask for accessibility permissions—grant them under System Settings > Privacy & Security > Accessibility.

Create the init.lua Configuration File

  1. Open Terminal and run:
  2. mkdir -p ~/.hammerspoon  
    cd ~/.hammerspoon
    touch init.lua
  3. Use a code editor (like VS Code or nano) to open init.lua. Example:
  4. code init.lua



    3. ๐Ÿ” Writing the Script to Switch Cursor Displays

    Paste the following Lua code into your init.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:
    • 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.

    ๐Ÿš€ Test Your Shortcut

    1. Click any app window.
    2. Press Cmd + Shift + 2.
    3. The cursor should jump from one screen to the other—centered and smooth.
    This works even if your screens are mismatched in size or orientation.

    5. ๐Ÿ› ️ Extra Tips and Customization Options

    ๐Ÿ” Change Key Bindings

    Customize this:
    hs.hotkey.bind({"cmd", "shift"}, "2", toggleScreens)
    To something like:
    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

Popular posts from this blog

How to Recover Deleted Files in Linux: A Step-by-Step Guide | recover deleted files | Linux file recovery tools | restore deleted files from trash | recover files from Linux recycle bin | TestDisk Linux | PhotoRec Linux | recover deleted partitions Linux | Extundelete tutorial | R-Linux file recovery | BleachBit for Linux recovery

Best Free macOS Apps to Control External Displays and Their Resolutions | Best free macOS app for external display | change resolution macOS | free display manager for Mac | control external display resolution | macOS external display management tools | adjust resolution macOS

How to Use ChatGPT API in Your Code: A Simple Step-by-Step Guide | ChatGPT API integration | use ChatGPT in code | OpenAI API tutorial | Python ChatGPT API | JavaScript ChatGPT API | how to use OpenAI API | ChatGPT API key setup | API response handling