0

My eight-year-old son is left-handed and I am right-handed. I wish to be able to quickly and easily switch primary and secondary mouse buttons from the command line. We use Windows 10. I found this, which provides c and C#* solutions. I found this which provides a solution which does require rebooting, and also provides a link to the Stack Overflow answer. However, it seems to me that this should be possible to do without resorting to a compiled language. I would be happy with a solution using PowerShell, Python, Perl, a nircmd utility, etc., but I'd rather not resort to a compiled language.

Thank you for your help.

Edit: Changed question to add "...or by using a hotkey...", since I ultimately wanted to do this with an Autohotkey hotkey; it turns out that Autohotkey can do the swap itself. I was assuming that the answer would be e.g. a Powershell script, which I would invoke by using an Autohotkey hotkey. See the accepted answer.

  • Would you be ok with an AutoHotkey script. Would be pretty easy to make a toggle function. – Confuzing Aug 24 '17 at 19:59
  • Although this isn't guaranteed to work with all programs. – Confuzing Aug 24 '17 at 20:03
  • I have never done this, and I don't know powershell well, but look at DLLImport and see, for example, ( stackoverflow.com/questions/24391367 ). Put that together with the info from your C# example. I have seen examples of VBS windows API hacks where you automate excel by attaching a temp function, but that is hairy IMO – Yorik Aug 24 '17 at 21:24
  • Confuzing, Autohotkey would be fine. I use it for a lot (for example, while composing this question I modified my normal script so that I can type ";sharp" and it will insert a sharp symbol). I did not know that Autohotkey could do this. I can probably figure it out myself at this point. Thank you very much – ludinom Aug 24 '17 at 21:59
  • Yorick, thank you very much. I will probably go the AHK route. – ludinom Aug 24 '17 at 22:01
3

Here is what I came up with for AHK. Just toggles rebinding the mouse buttons with ctrl+alt+m

swap:
swap=false

^!m::
    swap := !swap 

#if !swap
    RButton::LButton
    LButton::RButton
  • Thanks a lot! I hadn't had a chance to get back to this and write it myself, but you saved me the trouble. Sorry to put you to the trouble, I could have figured it out once you pointed me in the direction of AHK. I dropped this into my standard AHK script which runs at startup, used a different key (I normally use ^#! for my AHK hotkeys, but ^#!m is already used by Remember the Milk, so I used ^#!s, for swap) and it works great. I'm sorry I can't upvote; or rather, I upvoted but it doesn't count, because I have <15 reputation, so my opinion is recorded but not displayed. – ludinom Aug 25 '17 at 20:57
  • @Confuzing, very nice solution. Only one question, it's always started as left handed, do you know how to make it start as right handed by default. I was trying to change swap=false, but that doesn't help. Thanks! – electroid Oct 7 '18 at 15:14
  • 1
    @electroid Changing swap to true should have worked. But you could change the '#if !swap' to '#if swap', or probably even easier to just reverse them in Windows mouse settings. – Confuzing Oct 7 '18 at 15:58
  • @Confuzing, your solution change the #if !swap to #if swap working just fine! Thanks! – electroid Oct 7 '18 at 17:54
1

This is the answer, which was posted on https://superuser.com/a/1357020/790554.

This is the Autohotkey version (modified/based on https://github.com/jNizM/AHK_DllCall_WinAPI/blob/master/src/Mouse%20Input%20Functions/SwapMouseButton.ahk).

; autohotkey code - mapped to F12
F12::
    buttonState := DllCall("user32.dll\SwapMouseButton", "UInt", 1)
    if buttonState <> 0
    {
        buttonState := DllCall("user32.dll\SwapMouseButton", "UInt", 0)
    }

This works fine with all Windows (including Windows 10). I usually map it to a hotkey such as "F12" key on my keyboard (using Autohotkey), and I can toggle between left and right mouse button instantly with press of a key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.