("EDITED" July, 5, 2013 just for giving it a more expressive title for google!)With this little Intro and Lesson 1, you'll have set your first key remappings in less than an hour, promised!
Why a scripting language?"Hotkeys" (as they are called in AHK)
Most people wanting to shift characters or functions from one key (or key combination like shift-somekey, control-somekey, etc.) of their keyboard to another key or key combination, look out for (free or paid) proprietary macro tools, which means they get some additional application for their computer with which then they will be stuck for a long time, because of the INVESTMENT they will have put into it, some money perhaps, but first of all, many, many hours of "putting things into it" - that's why the switch, later on, to a full-grown scripting language like AHK, becomes more and more "psychologically impossible", all the more so since most of these proprietary macro tools will NOT allow for any "export" of your macros but force you to view them on screen and then recoup them, by hand, into your script (or any other competing proprietary tool, perhaps more "powerful" than the first one).
"Hotstrings" (as they are called in AHK)
Thus, macro tool users "learn" to live with the numerous limitations of most of such tools, and by the way, this also applies to so-called "text expander" tools: Most of them have many limitations, and many of them don't even allow for exporting your hotstrings into another tool. So what are hotstrings? If you use Microsoft Word, for example, you can "expand" some short "strings" (some characters devoid of meaning, not forming a real word in your language), plus a trigger (for example a punctuation character), into real (but much longer or difficult to spell / type) words, or even into texts of some length, including line breaks, for example your letterhead, complimentary closes, and many more.
So this is possible in Word and some other "text processors", but not many applications come with such functionality, so some people buy a "text expander" on top of their macro tool, in order to have available the same set of hotstrings in more than one application, "system-wide" (just a hint here: AHK even allows for different sets of hotstrings, for example if you write in more than one language, or if you have general texts and then legal texts, or whatever your needs are, AHK's possibilities are endless!).
Another aspect of interest here: Both your macro tool and your text expander tool will persistently monitor your system and every key you'll ever press, in order to detect when they are expected to do "something real", and this does not only make for "double overhead", but also, many such macro tools do not function well with many such text expander tools, so you would have to check out carefully your possible combinations. AHK, on the other hand, will also monitor your system, but for hotkeys and hotstrings at the same time, so this is "minimally invasive", and no interference of two tools disturbing each other will occur.
So why people get the above tools, instead of a scripting language?Because the very first steps with a scripting language are not (made) evident, all the less so with the fact that most people would like to start their "macroing experience" with reassigning not some normal character keys (since those are needed for typing), but some "special keys", and here, in a macro tool, you just press the key in question in order to reassign it, but in a scripting language, you will need the "name" of that key, and worse, you will probably even need the "scan code" of that key - DON'T STOP READING HERE, THOUGH, there's no need to worry!!!
So you see, with a scripting language, some (imaginary) "real problems" seem to get into your way, right on your very first steps: Let's call this "very unfortunate", all the more so since there was no quick help easily available. But now, there is, and knowing how to do it is knowing how extremely easy it is! (If you really need to sneak beforehand: it's point 6 below!)
Lesson 1Step 1What do you need for your very first script? You need a script, so you need an editor: If you don't have any particular one, just open the "Editor" that comes with Windows. Then, copy the script below: As you can read in the script, it comprises two parts: An autoexec section, which will be executed once, just after "loading" the script, and a "persistently running", with all your hotkey and hotstring assignments; after some months, this could be several hundred such assignments (or even thousands, especially if you work with "scope" later on, meaning you'll have different assignments for different applications (or different hotstrings for different languages), but then, it's perfectly possible to just limit yourself on doing it straight, never doing anything else than what you find in this lesson 1).
As you can see, the script here contains some assignments to start, in order for you to see that it's running; afterwards, you will "edit" this script, in your editor, and then "reload" it.
But first, you must SAVE this script in a way that it becomes available for AHK. Save it to .txt if you want, then rename it to .ahk, or save it to .ahk in the first place, which is preferable - even so, it stays a plain text file, let's say you save it under the name of first.ahk.
This script contains some hotkeys and hotstrings; trigger them in order to see what they do, and you will quickly replace them by those hotkeys and hotstrings that will be helpful in your daily work, from now on.
Step 2You need AHK. So you go to
www.autohotkey.com, and there, you click the orange button "Download AutoHotkey". Of interest here, the download dialog will show you you're downloading AHK_L, from the site
http://l.autohotkey.net ! No, that's not a trick to get you malware from somewhere, but AHK_L is the AHK version in active development, and in order to get resources for the additional goodies of this most recent version, but it's recommended to refer, for help, to the principal site, autohotkey.com - you will not need anything in autohotkey.net for your start.
You install this AHK_L; the installer will ask you for the version you want to install, don't install the ANSI version (which is only there for historical reasons), but the Unicode version, and the right one for your system (32bit or 64bit).
Step 3Now, with AHK_L installed, you RUN that file "first.ahk" (or whatever name you will have given to it, but always with the ".ahk" suffix).
You'll do this by double-clicking on it in your file manager (if you don't have any file manager, install xplorer2 lite, which is free, or if you insist on doing it all within that one here I promised you above, use that Windows thing that pretends to "replace" a file manager (and of which I don't even know the name: never used it in my life!) later on, you will put this into your autostart folder), so this script is running (there is NO intermediate step for "compiling" here!).
Then, you press shift-control-m, in order to see that your first AHK script is running indeed!
Step 4Also, you will see that there is now a new symbol / icon for this file, appearing in the "notification area" / "system tray" of your screen (the thing to the right of your task bar, right in the bottom right hand screen corner). Right-click on this icon, in order to have a look at the little right-click menu there. Of immediate interest here: "Help", of course (and you will also have seen the "AHK forum" link on the autohotkey.com site in step 2), "Edit this script" (which will open the script in your editor) and then "Reload this script", but also "Open", for step 6.
So this is the "traymenu".
Step 5Edit your script: Add some new hotkey and / or hotstring assignments to it! Let's say some shift-control-h for "Hello aunt Birdie" or tyl for "Thank you very much for your letter that I received well" - everything you want, just play around with it some minutes in order to see how easy it all is.
After each editing, do a control-s for saving (but it's not necessary to close your script or your editor!), THEN do traymenu-"reload this script", and try your new hotkeys and hotstrings right in that same editor, in that same first.ahk file!
Before the next script changes,
; "out-comment" any changes your hotkeys / hotstrings will have made to your script, by a leading semicolon, or see this:
a::
send b
; before the semicolon, it's a command, and this here is a comment, or see this:
/*
this is a comment
spanning
several
or multiple
lines
*/
or simply delete what your first macros do within your first script; if you miss this step, your "reload this script" will get you an error message, and the "new version" is not loaded. Then, delete those text passages, save (^s), and reload again; it goes without saying that afterwards, you'll do really useful hotkeys and hotstrings that you will trigger not in your script, but in your real applications, but why not just minimize your script file after each addition / change / save / reload (also, see the general observations in step 6 for this!), to have it immediately ready for any further addition or change, during those very first weeks where you'll make a lot of such edits!
HERE'S WHAT YOU NEED TO KNOW FOR STEP 5 (You might print this out for handy reference)1
Over and over, you'll need the SEND command; you'll find the official help here, together with the names of many special keys, so you'll access this site many times:
http://www.autohotke...cs/commands/Send.htmThe official list for triggers is here:
http://www.autohotke...com/docs/KeyList.htm but I only use the "send" command help before, since it's all there what I need, and anyway, you must understand the following:
As the key TO WHICH you assign a command (the SEND key), the name of the key is without braces (be it normal character key or a special key with a special name), but for the key TO BE SENT (the SENT key), the ordinary character keys are without braces, but all those special keys are with the same names but WITH braces:
home::
send {end} ; send key: no braces, but sent key: braces!
This also applies to special keys identified by their respective scan code (step 6):
Also with so-called "modifiers", +, ^, !, # (shift, control alt, winkey), and which you can also use in combination, even in any order, but I strictly observe the order +^!#, because when you have hundreds of such hotkeys, some standardization will help with quickly identifying all these keys and key combinations. Note, all modifiers before the braces, not within; examples:
^a::
send !b
; here, control-a sends an alt-b +!a::
send ^!b
; here, shift-alt-a sends a control-alt-b !#F10::
send +#
{F11} ; here, alt-win-F10 sends shift-win-F11 (send key: no braces, but sent key: braces!)+^SC023:: ^!{SC029}
For an example of bolded and italicized text, see the script, but note that formatting will not be shown in your editor (but should, for example in Word or another text processor). Finally, the send command is with, or without a comma; do it the way you like:
+^a::
send b
; or this way:
2
Above, we've always sent single keys, but you also can send strings and special characters that need a so-called "exception character" before them; in this example the accent grave is the exception character for the n, which makes a carriage return, and also, you need the exception character for special characters like +^!# since, as we have seen, AHK uses them as "modifiers", normally. So, this:
+^a::
send Dear Sir
, `n`nThank you very much for your letter `
+ fax`!
; will make:
Dear Sir,
Thank you very much for your letter + fax!
As you see, the send command does NOT use any "" for strings (and would enclose variables (but that would be a further lession!) in p.c. signs: send somestring with %somevariable% and some other string and %someothervariable% perhaps).
Finally, you also can combine such strings with special keys; the following example would type the string into your text, but then not leave the cursor at the end of that text, but shift the cursor to position one, before the "D":
+^b::
send Dear Sir
, thank you very much
{home}
3
In the same way, you'll build real macros, spanning several lines of code (and thus be ended by a so-called "return" (which has nothing to do with the return key on your keyboard)), but that will be a further lesson; here just an example which also shows the very important sleep and msgbox commands (look them up within the commands list linked above):
+^c::
send ^{left} ; control-leftarrowkey sleep, 1500 ; for your visual checking send (and only
){space} ; here, a space not within text (as above), but (before or) after text send {end}`nOh
, second line here`!
{home} ; send {up}{end}{left, 3} ; sends the leftarrowkey 3 times in a row msgbox, See where the cursor is now!
; exception characters in send strings, but not needed in msgbox strings! return
Remember, for just key reassignments, nothing of such is needed, just do it one-line style as the examples above.
Step 6You will want to reassign special keys, too, which cannot be identified by their "names" as in step 5, hence your need the respective scan codes of such keys.
If you do NOT want to do this at this time (but don't be afraid of this step, it's so easy!), out-comment the special line in the script, and also, BEFORE doing step 6, please check that the special line is NOT out-commented! And do NOT use any other external tool for identfying these scan codes, since they might be totally different from those you get (and need) here! (Don't ask me why this is so, but don't complain if your wrong scan codes won't work!)
AND, GENERAL OBSERVATION HERE: Check that the special line is not only NOT out-commented (meaning, it must NOT have the leading semicolon), AND CHECK THAT THIS NEWEST VERSION IS THE RUNNING ONE! This means, save the script, and reload it - often, you miss one of these steps, inadvertantly, and then you don't get why your script doesn't work as expected, when simply the running version is not the one you see before your eyes!
Ok, now for the oh-so-difficult part of it all!
Do "traymenu" (rightclick on the "H") and select "Open" there. In that new window then you select the menu "View - Key history and script info".
By this, AND WITH THAT SPECIAL SETTING IN THE SCRIPT that you later will "out-comment", by a semicolon, any key pressed will be listed in that window, and in order to have all this in just some minutes, just press the key "a", then a special key, then key "b", then the next special key you want to get the scancode of, etc. (why not beginning top left on your keyboard (except for everything that's not strictly a...z keys), then going down to bottom right?), and note all these a, b, c (which only serve to clearly distinguish what you then will read in the list on screen: in this list you will see which special keys would also be accessible by their name, and which keys must be addressed by their respective scan code in order to become available in AHK), AND the special keys pressed in-between (not their official "names", just some indications for yourself to identify these keys later on), on a sheet of paper. Then look into the list you got on screen, and note the corresponding scan codes, it's as simple as that. (Of course, you could do it one by one, but why should it take hours, right?)
Well, that's all! You've seen above how to use these scancodes in your script, and whenever you'll have finished with all this scan code identifying, you'll outcomment the "special line" in the screen, to avoid the overhead that line, when active, causes on your system.
And now the basic script, the text to put into your very first AHK script file (note the hotkeys / hotstrings, and try them out):; This auto execute section of the script is executed ONCE, immediately after loading the script:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; Recommended for catching common errors.
#Hotstring EndChars
-()[]{}:
;"/\,.?!`n `t ; different from the default setting, I excluded the ' character here
SetTitleMatchMode, 2 ; = to identify applications by just PARTS of their caption, file title or such not interfering with this! /*
which means that's for SCOPE, for application scope to begin with, but you will quickly get to context scope (depending
on variables, or on "controls", meaning the same key will trigger different commands if you are in the tree, or in the list
field of your file manager for example: again "trymenu", then "Window Spy", in order to identify those elements):
AHK is just too powerful in order to not profit from these fine features! And yes, feel free to delete all my comments!
*/
SendMode Input ; Recommended for new scripts due to its superior speed and reliability. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. CoordMode, Mouse
, Screen
; here "Screen" or "Relative"
; #Persistent : not necessary here since this script is persistent anyway because it contains hotkey assignments
#InstallKeybdHook ; THIS IS FOR DETECTING THESE SCAN CODES ; outcomment when not needed anymore!
; Here starts the persistently-running part of the script (AHK knows this because now assignments follow)
+^m::
msgbox, Message
- Yes
, this script is running indeed
. ; no return needed since single-line
+^n::
send This is
^b
^ijust example
text^b
^i
, and in a
text processor
, it'd even appear formatted`!
::mmm:: After hotstrings, don't forget the trigger space or trigger "enter" or such`!
::nnn::: Just another hotstring, with ^utext formatting^u (invisible in your editor, though)...
+!m:: ; and the example from above:
send ^{left} ; control-leftarrowkey sleep, 1500 ; for your visual checking send (and only
){space} ; here, a space not within text (as above), but (before or) after text send {end}`nOh
, second line here`!
{home} ; send {up}{end}{left, 3} ; sends the leftarrowkey 3 times in a row msgbox, See where the cursor is now!
; exception characters in send strings, but not needed in msgbox strings! return
; and here, your code:
; ...
That's all! Ok, I lied to you, I know it's 90 min. later now, but then, 60 min. of them were spent not with AHK, but with all this awful reading you had to do! ;-)
And now, you can stop learning here, and will have a versatile (though free!) macro tool and text expander, or you'll spend some time with the complete commands list, and have a look at the application scope command, "if (WinActive)" / IfWinActive (also in the form #IfWinActive - those "#" commands are at the end of the alphabetical list of commands, don't overlook them), at the ClipWait command (which will enhance all the ^c and ^x that you will soon put into your macros), and many more, just browse them and read "into" those that might be of interest.
The next big step then will be the introduction of "logical constructs", which means, as soon as you'll discover "if / else" and such, you'll have a big problem to understand how you ever did pc work without having these at your disposal: a fantastic, new world will open up to you (and you'll have got a fantastic, new hobby for your week-ends, that's the bad news for your family here)!
So, here's the command list (just the parent page of the send command above):
http://www.autohotke...om/docs/commands.htm(and that's just those for AHK, not incl. the additional AHK_L ones!), and then look here, for example:
http://www.autohotke...m/docs/Variables.htmand well, there will be strictly no end, since AHK's possibilities are stricly unlimited! (Have a look what a real, top-notch AHK expert is able to do with it (and with a lot of expertise, that is), here:
http://skwire.dcmemb...om/fp/?page=software .)
Bye for today!