Mandrew

AutoHotKey Example Script

Jun 9th, 2012
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Single-line comments begin with a semicolon.
  2.  
  3. ; Every AHK script will execute from the beginning until it sees the word Return.
  4. ; This is called the auto-execute portion of the script.
  5.  
  6. #SingleInstance Force  ; This line disallows two or more copies of the script.
  7. Tries = 12             ; The = operator is used to assign values to variables.
  8. Repeats := 10          ; := is used for assigning expressions, which are more flexible. Get used to using it
  9. f := Tries + Repeats   ; This is an expression, so := is necessary.
  10. Repeats += f           ; += is shorthand for "x = x + value"
  11. MsgBox %Repeats%       ; This will display the value of Repeats in a Message Box.
  12. ; Because MsgBox does not use an expression format, variables must be enclosed in percent signs.
  13. ; You can force such commands to accept an expression using "% " (percent an a space):
  14. MsgBox % (f << 16) ** Repeats    ; don't worry about what these operators do yet
  15.  
  16. ; We've had enough fun, so we're going to end the auto-exception
  17. Return                 ; Return stops the execution of the script.
  18. ; If this script didn't have hotkeys or hotscripts, it would die unless it found the line #Persistent
  19. ; in the auto-execute portion of the script
  20.  
  21. Tries -= Repeats       ; This line never executes because it is after the Return
  22. ; Like +=, you can use -= as shorthand for "x = x - value".
  23.  
  24. /*
  25. This is a multi-line comment.
  26. This script maps . to Alt+Plus.
  27. */
  28. !+::     ; Hotkey format is (command)::
  29. Send .   ; This emulates a period keystroke. I recommend SendInput . though for speed.
  30.  
  31. /*
  32. THIS IS A BAD CHOICE OF KEY COMBO TO USE!
  33.  
  34. I wrote this script on a netbook, and you CANNOT hit the + key without also holding down Shift. Therefore,
  35. !+ is effectively !+= (do not confuse the + in the second example, it represents the Shift modifier).
  36.  
  37. I would HIGHLY consider you choose another key combo to fix your broken period key.
  38. */
  39.  
  40.  
  41. ; Note that you can also define custom key combos which do not use the typical Ctrl/Shift/Alt/Win
  42. ; modifiers using the & key, limited to two keystrokes. Do not confuse with Boolean AND!
  43. g & q::
  44. SendInput .
  45. ; This makes the G + Q keycombo send a . keystroke. Obviously not a great idea in practice.
  46.  
  47.  
  48. ; A better idea might be this custom key combo:
  49. AppsKey + /::
  50. SendPlay . ; SendPlay is like Send, but uses games
  51. ; This takes the infrequently-used but usually accessible AppsKey and uses a key close to the . key to
  52. ; to
  53.  
  54. ; One last goodie: loops and variables. This maps the key combo % + ] to send multiple periods.
  55. % & ]::
  56. Loop, %Repeats% {
  57.    ; Loops must be enclosed in {braces}. They are compatible with the One True Brace format,
  58.    ; which places the starting brace on the same line as the Loop command and the closing
  59.    ; brace on its own line.
  60.    Send .
  61. }
  62. ; Can you guess how many times the loop will execute?
Advertisement
Add Comment
Please, Sign In to add comment