AutoHotkey Guide

CrazyBob's Cops And Robbers game information, help, faqs and guides. If you need help with something in game, this is the place!
Post Reply
User avatar
SEA
CnR Admin
Posts: 1084
Joined: Sun Aug 01, 2010 12:49 am
Gender: Male
Location: Piccadilly Market
Contact:

AutoHotkey Guide

Post by SEA » Thu Dec 11, 2014 4:56 pm

Introduction

This is gonna be a simple guide on how to use AutoHotkey for all your keybinding needs. The main reason I want to do this is because AHK is far superior software compared to those generic SAMP keybinders.

Why? Because of the customization possibilities and features overall. Also the response time compared to most generic binders is way better. It doesn't really have any flaws, except the fact that it requires some time to learn how to actually make things with it.

Downloading & Installing AutoHotkey

Go to the official AutoHotkey website and press the Download button and install it just like any Windows program.

Getting started

The first thing you need to do is create a script file. To do this, open up Notepad or any text editor and make a blank file and go to File -> Save As and save it as <name>.ahk (make sure to set file type to 'All Types').

Image

Writing a script

This is the part where things can become a bit complicated if you don't understand what you are doing. I'd suggest you to take a quick glance AutoHotkey's own tutorial before continuing to read this guide. It'll teach you the very basics and a basic idea how the scripting is done. Believe it or not, but it's very hard to explain these kinds of things to a person who has never worked with any kind of a code, that's why I suggest you to read the AHK's own guide instead of trying to learn the script syntax from this guide.

After you have done that, we can go more detail into scripting binds. So first of all, paste this into the empty .ahk file you created in the previous step:

Code: Select all

#SingleInstance Force
#IfWinActive, ahk_class Grand theft auto San Andreas
These are something you shouldn't really care about, the first one will save you some time and the second one makes it so the binds will only work while in-game.

Then for actually creating binds, I'll show you a very simple example:

Code: Select all

Numpad1:: SendInput t/rape $ply{Enter}
And that's it. So whenever you press Numpad1 with the script running, the script will send the command (/rape $ply) just like any keybinding software would do. The syntax is simple, first you set the key that will be bound (Numpad1) then you put a double colon to tell the script that the following command needs to executed, and then you put the actual command (with a 't' in front of it to open the chat in SAMP to write the command.) and end the line with {Enter} which is same as pressing the Enter key.

For a complete list keys you can bind, visit the Key List page on AutoHotkey's website. To change the key, simply replace the 'Numpad1' from the example with the one you want. As you can see, pretty much every button is bindable. Whileas this might look very confusing, it's actually pretty simple once you figure it out. Start by changing the key (in this case, 'Numpad1') to whatever you want, and then the command to send. Short copy-paste from my script to show how easy it is to make simple binds:

Code: Select all

XButton1:: SendInput t/lk{Enter}
Numpad7:: SendInput t/flowers{Enter}
Delete:: SendInput t/dance{Enter}
Looks simple, right? XButton1 (side button on some mouses) to lock my vehicles, Numpad7 to give flowers and Delete to dance.

If you want to make a bind that sends more than 1 line of text, then you'll need to make it like this:

Code: Select all

Numpad5::
	SendInput tBoom!{Enter}
	SendInput t/fart{Enter}
return
The return part is essential.

Extras


Hotstrings

Hotstrings can be used to automatically replace text you write, so if you'd write "lol" it would automatically turn it into "lots of laugh". It can be used for many purposes, such as making custom commands.
CnR has built-in hotstrings that also work as examples, like $cop is basically the name of the closest police officer. Please refer to AutoHotkey's own hotstring guide for more information.

This is a very basic hotstring:

Code: Select all

::btw::by the way
It's that easy. So when you write "btw", it'll automatically turn it into "by the way". Great for lazy people.

Variables

This is a great example of a thing your generic key binding software can't do most likely. So let's make a /fpslimit bind that will switch between 20 and 90 FPS with single keypress.

First, you have to create your variable, and give it a value (in this case I'll set it to 90, which is my default /fpslimit value)

Code: Select all

FPS = 90
Then for making the actual keybind:

Code: Select all

Numpad9::
if(FPS = 90)
{
	SendInput t/fpslimit 20{Enter}
	FPS = 20
}
else if(FPS = 20)
{
	SendInput t/fpslimit 90{Enter}
	FPS = 90
}
So this is as simple as it looks. What it does, is basically checking if the variable 'FPS' is 90, and if it is, then set the variable 'FPS' to 20 and set the /fpslimit to 20 as well. So next time you press the button, it'll skip the first bit because the variable 'FPS' equals 20, and this time it sets the both values to '90' and so on.

Random

Another thing your generic keybinder can't do. 'Random' will generate a random number that you can use in your script. This example script will give you a random lotto number with the press of a single button:

Code: Select all

Numpad8::
	Random lotto, 1, 150
	SendInput t/lotto %lotto%{Enter}
return
Pretty simple as well, so it generates a random number between 1 and 150, then uses the number generated in the /lotto command to play a random number. (I keep winning with this all the time, just saying..)

Or perhaps if you want to get a random colored car..

Code: Select all

RCtrl::
	Random color, 0, 255
	Random color2, 0, 255
	SendInput t/vehc %color% %color2%{Enter}
return
(Donators Only)

Loops & Sleep

This is where things go a bit advanced, but give you lots of possibilities. With loops you can create automated scripts, such as automated fishing if you don't like smashing that '2' all day long. 'Sleep' is a AHK function which makes the script wait a specified time before continuing.

A very simple loop example:

Code: Select all

Numpad6::
Loop, 10
{
	SendInput t/fish{Enter}
	Sleep, 5000
}
return
This is a simple loop, that will loop 10 times with 5 seconds (= 5000 milliseconds) between each loop. So basically it'll type out /fish, wait 5 seconds, type /fish again and so on 10 times. Play around with the numbers to get a feeling how it works.

Saving & Running

To save your script, just simply go to File -> Save just like any document. Then just find the file and double click and if no errors pop up, it should be working. Please note that if you added the 'IfWinActive' part, the script will only function while in-game.


Example script

A very simple example script that I personally use. Should be pretty self-explanatory what everything does. This is what the .ahk file should contain.

Code: Select all

#SingleInstance Force
#IfWinActive, ahk_class Grand theft auto San Andreas

XButton1:: SendInput t/lk{Enter}
XButton2:: SendInput t/sit 8{Enter}

Numpad1:: SendInput t/mis{Enter}
Numpad4:: SendInput t/i $civ{Enter}
Numpad5:: SendInput t/i $law{Enter}
Numpad6:: SendInput t/i $war{Enter}
Numpad7:: SendInput t/flowers{Enter}
Numpad8:: SendInput t/lotto rand{Enter}


+1:: SendInput t/war{Enter}
+2:: SendInput t/ar $war{Enter}

!1:: SendInput t/buy{Enter}

§:: SendInput t/foff $ply{Enter}

F12::
SendInput {T]
SendInput {Up}
SendInput {Enter}
return

More Info

Full list of bindable keys
The Official AutoHotkey Documentation

Updated 2/12/2017
Last edited by SEA on Thu Apr 30, 2015 9:25 am, edited 10 times in total.
Blow my brain to fragments on an empty canvas - that's my last work of art for this fucked up planet

User avatar
Renegade334
Silly Admin
Posts: 1088
Joined: Sun Aug 01, 2010 4:05 pm
Personal Saying: iter non destinatum fruere
Location: United Kingdom
Contact:

Re: AutoHotkey Guide

Post by Renegade334 » Thu Dec 11, 2014 5:58 pm

Single-action hotkeys can be scripted as inline commands. For example, instead of

Code: Select all

NumpadPgDn::
	SendInput t/td 25{enter}
return
you can simply use

Code: Select all

NumpadPgDn::	SendInput t/td 25{enter}
Image
[ CnR toolbox: api | signatures | stats ]

Salt
Commissioner
Posts: 3330
Joined: Mon Oct 07, 2013 9:17 am

Re: AutoHotkey Guide

Post by Salt » Thu Dec 11, 2014 7:10 pm

2ez but I'd like to find out how to make it read chatlogs
Image

User avatar
i0sa
Captain
Posts: 577
Joined: Fri Sep 13, 2013 12:27 pm
Contact:

Re: AutoHotkey Guide

Post by i0sa » Thu Dec 11, 2014 7:31 pm

Salt wrote:2ez but I'd like to find out how to make it read chatlogs
fishlog ahk version

User avatar
SEA
CnR Admin
Posts: 1084
Joined: Sun Aug 01, 2010 12:49 am
Gender: Male
Location: Piccadilly Market
Contact:

Re: AutoHotkey Guide

Post by SEA » Fri Dec 12, 2014 3:49 am

Renegade334 wrote:Single-action hotkeys can be scripted as inline commands.
Didn't actually know about this, seems like a good way to save space and make the script to look tidier. Thanks!

Salt wrote:2ez but I'd like to find out how to make it read chatlogs
This is what I've been using, probably not the most efficient but it works. Simple "auto lotto" script.

Code: Select all

Loop
{
    FileGetSize, Size, C:\Users\User\Documents\GTA San Andreas User Files\SAMP\chatlog.txt
    If (Size > FileSize)
    {
        FileSize := Size
        Loop
        {
            FileReadLine, Read, C:\Users\User\Documents\GTA San Andreas User Files\SAMP\chatlog.txt, % A_Index + LineIndex
            if ErrorLevel
            {
                LineIndex += A_Index - 1
                break
            }
            NewLines .= Read "`n"
        }
        if NewLines ~= "CnR Lottery   Today's Number:"
		{
			Random lotto, 1, 100
			SendInput t/lotto %lotto%{Enter}
		}
    }
    NewLines := ""
	Sleep, 1000
}
Just change the path to chatlog.txt to yours.
Blow my brain to fragments on an empty canvas - that's my last work of art for this fucked up planet

User avatar
Snorkel
CnR Admin
Posts: 449
Joined: Mon Aug 02, 2010 12:50 pm
Gender: Male

Re: AutoHotkey Guide

Post by Snorkel » Sat Apr 04, 2015 4:21 pm

Well done SEA!

A great guide, includes the most basic script commands that every SAMP player should know, very explanatory and easy to apply.
Image

User avatar
Papi
Captain
Posts: 509
Joined: Fri Mar 23, 2012 6:58 am

Re: AutoHotkey Guide

Post by Papi » Thu Apr 30, 2015 4:59 am

For e.g if my 'z' key is for /sit 8.
If i'm typing a sentence and i use z in chatbox, will that z alphabet be replaced by /sit 8 command?
Image
(12:15) <~CrazyBob> so are andropork and Papi like boyfriend-girlfriend?
(12:16) <@John877> AndroPapi
(12:16) <%Myth> I can confirm this 

User avatar
SEA
CnR Admin
Posts: 1084
Joined: Sun Aug 01, 2010 12:49 am
Gender: Male
Location: Piccadilly Market
Contact:

Re: AutoHotkey Guide

Post by SEA » Thu Apr 30, 2015 6:43 am

Papi wrote:For e.g if my 'z' key is for /sit 8.
If i'm typing a sentence and i use z in chatbox, will that z alphabet be replaced by /sit 8 command?
Yes, because AHK doesn't detect whether the chat box is open or not. I think there might be workarounds for this, but I haven't tried anything similar yet. I would just use something like CTRL + Z, much easier to do.
Blow my brain to fragments on an empty canvas - that's my last work of art for this fucked up planet

User avatar
Papi
Captain
Posts: 509
Joined: Fri Mar 23, 2012 6:58 am

Re: AutoHotkey Guide

Post by Papi » Thu Apr 30, 2015 8:41 am

I found a workaround for this. Thanks!
Image
(12:15) <~CrazyBob> so are andropork and Papi like boyfriend-girlfriend?
(12:16) <@John877> AndroPapi
(12:16) <%Myth> I can confirm this 

Evangelist
Commissioner
Posts: 3008
Joined: Fri Jan 06, 2012 4:52 am

Re: AutoHotkey Guide

Post by Evangelist » Mon May 04, 2015 6:53 pm

Papi wrote:For e.g if my 'z' key is for /sit 8.
If i'm typing a sentence and i use z in chatbox, will that z alphabet be replaced by /sit 8 command?
Hello. I've been using autohotkey as keybinder since the beginning on cnr, so I can help if needed.
Now about this, you can use functions "Suspend" and "Suspend off" to activate your whole script. Opposite to that, you can use "Suspend On" to pause (actually suspend) your script.
If players (and SEA as the owner of this guide) want, I can post part of the code for this, but since it literally takes 2 lines, you can look up for that on autohotkey's website. Also, DO NOT FORGET return at the end of each command/bind, that's essential.

0) In my case, I've made it work like this:
You press KEY1, your script is suspended (none of binds work), so you can type in chat without any problems (unless you press the KEY1 again, but that's why you bind this to some useless key);
You press KEY2, your script is activated again, your binds are working. In this case, opening a chat box, and pressing a key which has some command binded to it, will make you type that command in chat, but that's pretty self-explanatory;

However, similar effect can be achieved in other ways:
1) binding activate/pause to a single key only, so it works like:
You press KEY1 first time, script is activated;
You press it again, script it deactivated (suspended);
But I find this way a little bit more complicated and less effective.

2) triggering a timer while you press T/F6, and stopping/resetting the timer when you press Enter/Esc
I'm not for second option, and in my opinion option 0) is the best.
If you don't find an effective workaround for this, feel free to PM me or something, and I'll ask SEA to update this topic with that piece of code. I'd write that piece of code if I weren't on the mobile now, so it's kind of slow.

And on topic, nice guide SEA. Simple and efficient!

EDIT: will post some features I find useful.
.

Post Reply