Skip to content

Instantly share code, notes, and snippets.

@Joaquin6
Forked from chrisdhanaraj/osx.ahk
Last active August 6, 2020 04:39
Show Gist options
  • Save Joaquin6/3d3f37586839d08176ce89982e854f82 to your computer and use it in GitHub Desktop.
Save Joaquin6/3d3f37586839d08176ce89982e854f82 to your computer and use it in GitHub Desktop.
Autohotkey script to bring OSX keybinds to Windows

Mapping your macOS keybinds to Windows

Maybe Brad Frost’s horror story about the new MacBook Pro worried you or maybe #davegoeswindows convinced you — but, long story short, you bit the bullet and went Windows. Hopefully, you found Owen William’s great post to setting up your perfect Windows developer environment, but there’s just… just one thing that’s bothering you.

Who in the world wants to use their pinky finger to use their main modifier key? The Control button is so far away! The Command button placement was perfect! Not to worry, with a combination of Sharp Keys and AutoHotkey you can simulate the same keyboard environment as you’re used to.

TL;DR

  1. Use Sharp Keys to remap Ctrl and Alt
  2. Use AutoHotkey + this starter script to bring over basic macOS keyboard bindings and restore Alt Tab functionality.

Step One: Sharp Keys

Sharp Keys is going to be used to swap the Ctrl and Alt keys on your Windows keyboard.

  1. Download, install, and run Sharp Keys
  2. Hit the ‘Add’ button, you’ll see two columns — one that indicates the button you hit, the other that shows the action you want to take. Scroll to find the Special: Left Alt on the left column and select it. On the right column, scroll to find the Special: Left Ctrl and select it. The screen should look like this

Image for post

Image for post

  1. Hit OK, and then do the same for reverse action. You’ll end up with a screen that looks like this.

Image for post

Image for post

  1. Press Write to Registry and reboot; Alt now maps to Ctrl and vice versa, almost like what happens in macOS!

AutoHotkey

Unfortunately that alone isn’t quite perfect, in macOS we use Command + Tab to tab through active windows and Ctrl + Tab to tab through browser windows; that’s the default scenario on Windows as well, but we just reversed it. Thankfully, AutoHotKey can intercept button presses and execute different actions.

  1. Download and install AutoHotkey. AutoHotkey is an automation scripting language — which means nothing actually happens until we create and run a script. Let’s do that now.
  2. Open the Start Menu in the lower left corner and open the Run dialog. To get to it quickly, simply press Windows + R. Type in shell:startup and hit enter to open up Windows Explorer in the Startup folder. Any script we create here will run on Startup.
  3. Create a new AutoHotKey script by opening the context menu in the folder (right click), press New, and then press AutoHotkey script. You can also create a new blank new file here and save it with a .ahk extension.

Image for post

Image for post

  1. Copy and paste this starter script into your file. https://gist.github.com/Joaquin6/3d3f37586839d08176ce89982e854f82. Save, exit, and run the script.

The trick with AutoHotkey is that, now that you’ve mapped Ctrl to Alt and Alt to Ctrl, any script you write has to function with that in mind. This script brings a couple things over.

  • Alt + Tab is once again rebound to toggle through active Windows, and Ctrl + Tab returns to tab through browser windows.
  • Alt + Up/Down maps to the Home and End keys on Windows to mimic the document scroll manipulation that macOS has. Similarly, Alt + Left/Right brings you to the end and beginning of lines, and all four directional sets function with Shift in mind to select things.
  • Alt + Space opens the Start Menu to mimic Spotlight (the Windows start menu functions pretty much identically to Spotlight now).

And that’s it! There are still a bunch more macOS keyboard bindings that I have yet to bring in, but these are the major ones for me. I use VS Code on Windows (as well as on macOS), and I’ve yet to run into any keyboard bindings that have tripped me up once I’ve done these couple of tweaks.

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
#InstallKeybdHook
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; --------------------------------------------------------------
; Mac-like screenshots in Windows (requires Windows 10 Snip & Sketch)
; --------------------------------------------------------------
#+3::Send #{PrintScreen} ; Capture entire screen with CMD/WIN + SHIFT + 3
#+4::#+s ; Capture portion of the screen with CMD/WIN + SHIFT + 4
; --------------------------------------------------------------
; media/function keys all mapped to the right option key
; --------------------------------------------------------------
LCtrl & Tab::AltTab
!Tab::Send ^{Tab}
!+Tab::Send ^+{Tab}
^Space::Send ^{Esc}
LWin::LCtrl
^Left::
Send {Home}
Return
^Right::
Send {End}
Return
^+Left::
Send +{Home}
Return
^+Right::
Send +{End}
Return
^Up::
Send ^{Home}
Return
^Down::
Send ^{End}
Return
^+Up::
Send ^+{Home}
Return
^+Down::
Send ^+{End}
Return
!Up::
Send {ctrl down}{Up}{ctrl up}
Return
!Down::
Send {ctrl down}{Down}{ctrl up}
Return
; Selection (uses a combination of the above with shift)
<!+Left::
Send {ctrl down}{shift down}{Left}{shift up}{ctrl up}
Return
<!+Right::
Send {ctrl down}{shift down}{Right}{shift up}{ctrl up}
Return
!+Up::
Send {ctrl down}{shift down}{Up}}{shift up}{ctrl up}
Return
!+Down::
Send {ctrl down}{shift down}{Down}}{shift up}{ctrl up}
Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment