GageSec

Jon Gage | March 31, 2023

Using AHK to Work More Efficiently (Beginner)

Working in IT can require many professionals to access several systems to troubleshoot hardware and configure settings in order to complete a task. In many cases, there are several redundant tasks that are time consuming. I’m sure you have asked yourself “If only there was a way to automate a lot of this?”. The good news is, There is! AutoHotKey is a program that allows you to automate just about anything that can be done on a Windows PC. Most people that are aware of the existence of AHK think it is mainly used to autofill text instead of having to type it in manually, which AHK does, but it can do much more.

In this tutorial, you will learn how to make basic AHK scripts that autofill redundant text typing.

Step 1: Download AHK
https://www.autohotkey.com/

Step 2: Create your first script

You can right-click anywhere on the desktop > New > AutoHotKey Script

Alternatively, you can open Notepad, Notepad++, or your preferred text editor and type in your script and save it as a “.ahk”

^Q::
;Makes text being typed out faster
SetKeyDelay, 0
Send, Hello World!
return

“Send,” Whatever is typed after the comma will print out when CTRL+Q is pressed. In this example “Hello World!” will be typed out.

Alternatively, you can auto clipboard text instead of printing it out.

^W::
clipboard =
(
Name:
Phone:
Email:
)
return

Anything typed within the paranthesis will be copied and all you have to do is paste it where you need it.

Step 3: Expand script

You can also automate more when auto printing text.

^E::

SetKeyDelay, 0
Send, Name:{SPACE}{ENTER}
Send, Phone:{SPACE}{ENTER}
Send, Email:{SPACE}{UP}{UP}
return

Slightly more advanced example code:

This script allows you to highlight text and will open a new tab and search the highlighted text when you press the desired hotkey

^Q:: ; change to preferred hotkey

clip := clipboard
send, ^c
url := "https://www.google.com/search?q="
is_it_an_url := SubStr(clipboard, 1 , 8)
if (is_it_an_url = "https://") 
{
	; if it starts with "https://" go to, rather than search in google search
	run, %clipboard%
}
	else 
{ 
	;search using google search
	joined_url = %url%%clipboard%
	run, %joined_url%
}
return

Source: https://www.autohotkey.com/boards/viewtopic.php?t=93891

Formatting and organization:

When you put a “;” at the beginning of a line, anything after will be ignored. This is useful for adding notes to your script so you can refer back to it later and know what everything is doing. I also like to add separators as well. You can also put all of your hotkeys in one script, it is important to note to add “return” at the end of each hotkey or script execution in most situations.

;---------------------------------------------
^Q::
;Makes text being typed out faster
SetKeyDelay, 0
Send, Hello World!
return
;---------------------------------------------
^W::
clipboard =
(
Name:
Phone:
Email:
)
return
;---------------------------------------------
^E::
SetKeyDelay, 0
Send, Name:{SPACE}{ENTER}
Send, Phone:{SPACE}{ENTER}
Send, Email:{SPACE}{UP}{UP}
return
;---------------------------------------------
^R:: ; change to preferred hotkey
clip := clipboard
send, ^c
url := "https://www.google.com/search?q="
is_it_an_url := SubStr(clipboard, 1 , 8)
if (is_it_an_url = "https://") 
{
    ; if it starts with "https://" go to, rather than search in google search
    run, %clipboard%
}
    else 
{ 
    ;search using google search
    joined_url = %url%%clipboard%
    run, %joined_url%
}
return

Additional Information:

AHK has a community forum where you can get help writing scripts to perform the tasks you need. I will make a more advanced tutorial in the near future where you can learn to automate more complex tasks.

Documentation: https://www.autohotkey.com/docs/v2/index.htm

AHK Community: https://www.autohotkey.com/boards/