How to Assign a Macro to a Mouse Button

VBA & macros · Updated November 22, 2026

Assigning a macro to a mouse button lets you trigger a sequence of actions—like a series of keystrokes, a text snippet, or a complex Excel routine—with a single click. This guide covers both mice with built-in macro software (like Logitech, Razer, or Corsair) and the built-in Windows method that works with any mouse that has extra buttons. You’ll need your mouse’s configuration software installed, or you’ll use a free tool like AutoHotkey for a universal solution.

What You’ll Need

  • A mouse with at least one programmable button (side buttons, scroll wheel click, or DPI button).
  • For the software method: the manufacturer’s software (e.g., Logitech G HUB, Razer Synapse, Corsair iCUE).
  • For the universal method: AutoHotkey (free, Windows only) installed from autohotkey.com.
  • A macro already created (e.g., in Excel, or a series of keystrokes you want to automate).

Step-by-Step

Method 1: Using Mouse Manufacturer Software (Recommended)

  1. Install the software. Download and install the latest version of your mouse’s configuration software from the manufacturer’s website. For Logitech, that’s G HUB; for Razer, Synapse; for Corsair, iCUE. If your mouse is a generic brand, check if it has its own software—often called “Mouse Manager” or similar.
  2. Open the software and select your mouse. Launch the software. It should automatically detect your mouse. If not, plug it in and restart the software.
  3. Find the button assignment panel. Look for a tab or section labeled “Assignments,” “Buttons,” or “Customize.” In G HUB, it’s under the mouse’s icon; in Synapse, it’s under “Customize.”
  4. Choose the button you want to assign. In the software’s visual representation of your mouse, click on the button you want to change. It will highlight.
  5. Select “Macro” as the action. In the dropdown menu of possible actions (like “Keystroke,” “Media,” “Launch App”), choose “Macro.”
  6. Create or select a macro. If you haven’t created a macro yet, click “New Macro” and give it a name. Then record your actions: click “Record,” perform the keystrokes or clicks you want, and click “Stop.” You can also edit the macro manually, adding delays or text. If you already have a macro, select it from the list.
  7. Set the macro to “Assign.” After selecting the macro, confirm the assignment. The button should now be linked to that macro.
  8. Test it. Click the assigned button. The macro should execute. If it doesn’t, check that the software is running in the background (it often needs to be open for macros to work).

Method 2: Using AutoHotkey (Universal, Windows)

  1. Install AutoHotkey. Go to autohotkey.com and download the installer. Run it and follow the prompts. After installation, you’ll have a program that can run scripts.
  2. Identify your mouse button’s code. AutoHotkey uses button names like XButton1 (usually the forward side button) and XButton2 (usually the back side button). The middle mouse button is MButton. To find out which is which, you can use the AutoHotkey Window Spy tool (included with the installation). Open it, press your mouse buttons, and note the button name that appears.
  3. Create a script file. Right-click on your desktop or in a folder, select New > AutoHotkey Script. Name it something like MouseMacros.ahk.
  4. Edit the script. Right-click the file and choose “Edit Script.” Notepad will open. Delete the default content and type your macro. For example, to make the forward side button type “Hello, world!” and press Enter:
    XButton1::
    Send, Hello, world!{Enter}
    return

    To run an Excel macro, you’d use a more complex script that sends the shortcut key combination you assigned to that macro (e.g., Send, ^+m for Ctrl+Shift+M).

  5. Save and run the script. Save the file (Ctrl+S) and double-click it. A green icon with an “H” should appear in your system tray. Test the button. The macro should now work.
  6. Make it start with Windows (optional). To have the script run automatically at startup, press Win+R, type shell:startup, and press Enter. Copy your .ahk file (or a shortcut to it) into that folder.

Common Mistakes

  • Not keeping the software running. Mouse software often needs to be running in the background for macros to work. If you close it, the button may revert to default.
  • Assigning to the wrong button. In the software, it’s easy to click the wrong button on the visual diagram. Always double-check which button is highlighted before assigning.
  • Macro recording includes unintended actions. When recording, if you move the mouse or click extra buttons, those get recorded. Keep the recording clean.
  • Using the wrong AutoHotkey button name. If you use XButton1 when your button is actually XButton2, the macro won’t trigger.
  • Forgetting to save the AutoHotkey script after editing. If you edit the script and don’t save, the old version runs.

Tips

  • Use delays in macros if you’re automating a sequence that requires the computer to process steps (e.g., waiting for a program to open). In manufacturer software, add a delay between actions; in AutoHotkey, use Sleep, 500 (for 500 milliseconds).
  • Create a macro for text expansion. Assign a button to type your email address or a common phrase—it saves time.
  • For Excel macros, assign a keyboard shortcut first. In Excel, go to Developer > Macros, select your macro, click Options, and assign a shortcut like Ctrl+Shift+M. Then in your mouse software, assign that keystroke to the button.
  • Keep a backup of your mouse profile. Most software lets you export your settings. Do this so you can restore them after a reinstall.
  • If a macro doesn’t work in a specific game or program, try running the mouse software or AutoHotkey as administrator. Right-click the software icon and select “Run as administrator.”

Troubleshooting

  • Macro doesn’t execute at all. Check that the software is running. If using AutoHotkey, check the system tray for the green H icon. If it’s not there, double-click the script file to run it. Also, ensure the button is actually programmable—some mice have buttons that can’t be remapped.
  • Macro works in some apps but not others. Some applications (especially games with anti-cheat) block simulated input from macros. Try running the software as administrator. If it still fails, the app may be blocking it intentionally.
  • The button triggers the macro but also does its default action. This can happen if you assigned the macro to a button that also has a system function (like the scroll wheel click). In your mouse software, look for an option to “disable” the default action or set it to “unassigned” before assigning the macro.
  • AutoHotkey script errors. If you see a pop-up error, read it carefully. Common issues: missing return at the end of a hotkey, or using a variable that isn’t defined. Double-check your script against the examples.
  • After rebooting, the macro stops working. For manufacturer software, make sure it’s set to start on login (usually in its settings). For AutoHotkey, you need to place a shortcut in the startup folder as described above.

Frequently Asked Questions

  • Can I assign a macro to the left or right mouse button? Yes, but be careful: the left button is used for clicking. If you reassign it, you’ll lose normal clicking functionality. Most software allows it, but it’s not recommended unless you have a specific need.
  • Will macros work in games? Many games allow macros, but some online multiplayer games consider them cheating and may ban you. Check the game’s terms of service. For single-player games, they generally work fine.
  • Can I use the same macro on multiple mice? If you use manufacturer software, profiles are tied to the specific mouse model. However, you can often export and import profiles. With AutoHotkey, the script works with any mouse that has the same button codes.
  • How do I record a macro that includes mouse movement? In most mouse software, you can record mouse movement as part of the macro. In AutoHotkey, you can use commands like MouseMove to move the cursor to specific coordinates. However, be aware that screen coordinates vary, so this is less reliable.
  • Is it possible to assign a macro to a keyboard key instead? Yes, the same software allows you to remap keyboard keys. The principle is the same: select the key, choose “Macro,” and assign.
  • Can I make a macro that runs an Excel macro directly? Yes. In Excel, assign a shortcut key to your macro (Developer > Macros > Options). Then in your mouse software, assign that shortcut (e.g., Ctrl+Shift+M) to the mouse button. Or, in AutoHotkey, use Send, ^+m.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.