Create Custom Keyboard Shortcuts with AutoHotkey - WindowsTips.net - Windows Tips and Tricks with Geek

Sunday, August 15, 2021

Create Custom Keyboard Shortcuts with AutoHotkey

 

How to Install AutoHotkey

Head to AutoHotkey’s website to download the program. On the main page is a big green button that says “Download” on it. Clicking on that will take you to the download page. Here you can on a teal download button to get the latest version.

NOTE: Some antivirus programs will flag AutoHotkey as malware. This is a false positive. AutoHotkey is extremely powerful, and while it isn’t dangerous on its own, it is a scripting language—which means you could create malware with it if you had the desire to. But don’t worry about downloading the base AutoHotkey program itself; it will not harm your computer.

 

Once the installation file has downloaded, double click on it to begin installing AutoHotkey. Nearly all users will want to use the Express Installation button. Custom installation gives you options around default behaviors and install location. Leaving the defaults in place is best.

 

Once it’s installed, you’re ready to get to the fun stuff: writing your first script.

How to Create Your First AutoHotkey Script

Running the AutoHotkey application now won’t actually do anything but launch its help page. To get started, you need to have a script that tells AutoHotkey about your custom keyboard shortcuts. So let’s start by creating one.

Right-click on your Desktop (or any other folder) and choose New > AutoHotkey script. This will create a new file with the .ahk extension in that folder. Name the file whatever you want, then right-click on it and open it in Notepad. (or a more code-friendly program like Notepad++, if you have it). There will be some text in the file. For simple scripts like were demonstrating here, it can be removed. As you get more advanced, you may want to leave it in.

You will be given a nearly-blank slate for you to create the keyboard shortcuts of your dreams. Here are a couple examples.

Let’s start with a real simple character insertion script. I have a script I use every day to allow me to type common characters from German that aren’t on my English keyboard. Let’s say I want to type the ß character whenever I press Alt+Shift+S on my keyboard. In AutoHotkey, that would look like this:

!+s:: Send, ß

Let’s break that bit of text down:

  • ! is the symbol for the Alt key
  • + is the symbol for the Shift key
  • s stands for (obviously) the S key
  • :: denotes what you want the preceeding keys to run when pressed together
  • Send, is a command that types the proceeding text
  • ß is the text we want the command to type.

Essentially, this command says “When Alt, Shift, and S are pressed at the same time, type ß.”

You can add other modifiers, too. For example, if you add the < symbol before your hotkey (so it reads <!+s:: Send, ß , you can tell AutoHotkey to only run the command if the Left Alt key is used.

My entire German-symbol-hotkey script looks like this:

<!a:: Send, ä
<!o:: Send, ö
<!u:: Send, ü
<!+a:: Send, Ä
<!+o:: Send, Ö
<!+u:: Send, Ü
<!+s:: Send, ß
<!+$:: Send, €

If you know the name of the character you wish to add to your script, searching for it in Google is probably the quickest way to find it. If not, you can look for it on an ASCII or Unicode table.

You can take this further than just individual characters, too. If you find yourself regularly struggling to translate complex, obnoxious, or just plain long character strings from your brain to your fingers, AutoHotkey is your new best friend. In my other job, I often have to reach out to individuals at other institutions to discuss security items on projects without any introduction from the people I work with. This requires I explain who I am and why I’m contacting them. Instead of typing out this whole message I use a hotstring in AHK. The script looks like this:

The :*: at the beginning tells AHK to watch for the sting that follows it. In this case that string is ncm (short for “new cold message” in my head). So any time I type the letters ncm in a box, it will swap them with the string of text that follows the :: in the script. Not only have I turned a paragraph worth of typing into three keystrokes, I know it’s going to be right every time.

This could be accomplished with a hotkey instead of hotscript too. You could replace :*:ncm in the script with !+n and have the same string of text show up with you pressed Alt+N on your keyboard.

AutoHotkey also has the ability to pull basic info from your computer. For example, it can get today’s date. So if you’re someone who is entering the date into a lot of fields, this script could be a lifesaver.

If you run this script, AutoHotkey will drop the current date in wherever your cursor is. You can play with things like formatting ( dd/MM/yyyy versus MM/dd/yyyy for example) in the script as well.

No comments:

Post a Comment