Autoit3 -> AHK?

02/05/2010 03:46 Jacqueline#1
I've noticed more people use Autoit3 for scripting, but I personally prefer AHK. But does Autoit3 scripting work the same way as AHK?

I've seen some useful scripts here, especially for weaving (which is what I need) but I really don't like the fact that I can't pause the script (even if I press the pausing hotkey, it just doesn't stop for some reason).

Let's say I'm using this Autoit3 script to weave my cobwebs:

Would this work for AHK as well, or do I need to make some changes for it to work for AHK? Any help is appreciated, thanks.
02/05/2010 03:56 Theri#2
The syntax is different for ahk, do a google search on converting au3 to ahk, pretty sure there's a really simple utility to do it for you.

imo au3 syntax is much nicer and better to work with than ahk but that's neither here nor there.

Why would you sleep for 999999 <.<
02/05/2010 04:11 Jacqueline#3
Actually, I'll give Autoit3 one more try. But my current issue is that I get an error when trying to start the script above:

AutoIt Error
"Line 16 (file blah blah blah blah\auto_weave.au3):

HotKeySet("{PAUSE},"TogglePause")

Error: Unknown function name.

^What's wrong with that? I don't see errors in the script. Besides, I have the other pickup cobweb script with the same hotkey line, yet it runs perfectly?
02/05/2010 04:16 Theri#4
Code:
HotKeySet("{PAUSE},"TogglePause")
Errors because you didn't create a function called TogglePause.
02/05/2010 04:28 Jacqueline#5
Thanks, I never noticed that, I only noticed to put a function called Stop.

Now I got it to run, but the script doesn't help me in anyway :( I don't know whether I need to make new coordinates to have it work on my resolution or not, but I've also noticed that it doesn't seem to click properly either. I fail at scripting.

Edit: I'm using PNG as the image files, could that be a problem to this?
02/05/2010 04:35 Theri#6
Not really sure how its behaving versus what you are expecting to be able to help you.
02/05/2010 04:43 Jacqueline#7
I don't know, but for some reason the scripts don't work properly when I use a transparented background image, whereas if I use a normal one (with background), it works better :s I changed the transparented backgrounds from PNG to GIF now, still doesn't work as well as the JPG.

And I still can't pause/stop the script even if I press ESC, I have to stop the script manually which is really annoying.
02/05/2010 05:02 Theri#8
You should stick to bmp or gif file formats, jpg are ok. Don't use anything else. Alpha transparency doesn't work as far as I can tell.. Read the imagesearch documentation for ahk, its the same for autoit. If for instance you want to match a word you could paint the non font areas to a solid color, like black, and set transparency to transblack in the imagesearch function call. (This is the only form of "transparency" I've noticed to work through my testing).

See my post in the sub forum or the imagesearch documentation.

The code you posted only has one hotkey and that is esc which is calling a function called stop. I'm not sure if that is the hotkey that isn't working, try setting it to use a-z instead. I set a hotkey to x and it works fine.

Edit:
HotKeySet("!{ESC}", "Stop") is incorrect.

Take out the !.
02/05/2010 05:09 Jacqueline#9
Hm, I just tried using X as my hotkey, still doesn't stop the script. I think that's mainly the problem for me right now, since if I were to reduce sleeping time to 1000 or less, it'd be almost impossible to stop the script manually.

What I've noticed though is that while running the script, it disables my keyboard (Can't type, can't use any mabi hotkeys, etc). What could be the problem :s?
02/05/2010 05:29 Jacqueline#10
Quote:
Originally Posted by Theri View Post
Edit:
HotKeySet("!{ESC}", "Stop") is incorrect.

Take out the !.
As much as I hoped for that to work, it didn't T_T Looks like I'll have to manually stop the script all the time. Gosh, Autoit3 hates me.

I greatly appreciate the help though, thanks.
02/05/2010 06:39 Infylos#11
Quote:
Originally Posted by Jacqueline View Post
I've noticed more people use Autoit3 for scripting, but I personally prefer AHK. But does Autoit3 scripting work the same way as AHK?

I've seen some useful scripts here, especially for weaving (which is what I need) but I really don't like the fact that I can't pause the script (even if I press the pausing hotkey, it just doesn't stop for some reason).

Let's say I'm using this Autoit3 script to weave my cobwebs:

Would this work for AHK as well, or do I need to make some changes for it to work for AHK? Any help is appreciated, thanks.
Do you actually use this script or was that just an example?
02/05/2010 18:58 Theri#12
Partial placeholder, I'll post code to resolve the pause issue when I've had a time to grab lunch and work it out.

Update:

Looks like you have two options. You can either include misc.au3 in your scripts and use the _IsPressed function to check for the key presses and then run the proper code. This could probably work out ok depending on the script.

The other involves setting up like a virtual keyboard and then works in the expected way. Ill update again in a bit.

Update 2:

Here's the examples I found of how to do this. First is u sing misc.au3 and its _IsPressed() function and the second sort of hooks into the keyboard. I'm not sure why hotkeys work as expected in AHK and not AutoIt.

Code:
#include <Misc.au3>
$dll = DllOpen("user32.dll")

While 1
    If _IsPressed("1b", $dll) Then
        MsgBox(0,"Pressed","Esc")
        ExitLoop
    EndIf
WEnd
DllClose($dll)
Second example.
Code:
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Const $VK_DELETE = 0x2E
Global Const $VK_ESCAPE = 0x1B

Global $hFunc, $pFunc
Global $hHook
Global $hMod
Global $fPauseBot = False

$hFunc = DllCallbackRegister('_KeyboardHook', 'lresult', 'int;uint;uint')
$pFunc = DllCallbackGetPtr($hFunc)

$hMod = _WinAPI_GetModuleHandle(0)
$hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, $pFunc, $hMod)

While 1
    If $fPauseBot Then
        _UnsetHotKeys()
        While $fPauseBot
            Sleep(20)
        WEnd
        _ResetHotKeys()
    EndIf
    Sleep(20)
WEnd


Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hFunc)
EndFunc


Func _UnsetHotKeys()
    HotKeySet("+!d")
EndFunc


Func _ResetHotKeys()
    HotKeySet("+!d", "ShowMessage");Shift-Alt-d
EndFunc


Func ShowMessage()
    MsgBox(0x40, 'Title', 'Text')
EndFunc


Func _KeyboardHook($iCode, $iwParam, $ilParam)
    Local $tKBDLLHS = DllStructCreate($tagKBDLLHOOKSTRUCT, $ilParam)
    Local $iVkCode
    
    If $iCode > -1 Then
        $iVkCode = DllStructGetData($tKBDLLHS, 'vkCode')
        If $iwParam = $WM_KEYUP Then
            Switch $iVkCode
                Case $VK_ESCAPE
                    Exit
                Case $VK_DELETE
                    $fPauseBot = Not $fPauseBot ; Was $fPauseBot = True.
            EndSwitch
        EndIf
    EndIf
    
    Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)
EndFunc
This code is not mine, both come from the AutoIt forums. I will probably rewrite and include a modified version of the second in my bot functions at a later date.
02/06/2010 01:12 Theri#13
Hate to double post but I'm sure we can deal.

Here's what you need from the 2nd example code.

Code:
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Const $VK_DELETE = 0x2E
Global Const $VK_ESCAPE = 0x1B

Global $hFunc,$pFunc,$hHook,$hMod

$hFunc = DllCallbackRegister('_KeyboardHook', 'lresult', 'int;uint;uint')
$pFunc = DllCallbackGetPtr($hFunc)

$hMod = _WinAPI_GetModuleHandle(0)
$hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, $pFunc, $hMod)

Func _KeyboardHook($iCode, $iwParam, $ilParam)
    Local $tKBDLLHS = DllStructCreate($tagKBDLLHOOKSTRUCT, $ilParam)
    Local $iVkCode
    
    If $iCode > -1 Then
        $iVkCode = DllStructGetData($tKBDLLHS, 'vkCode')
        If $iwParam = $WM_KEYUP Then
            Switch $iVkCode
                Case $VK_ESCAPE
			; code to do on escape press
                Case $VK_DELETE
			; code to do on pause.
            EndSwitch
        EndIf
    EndIf
    
    Return _WinAPI_CallNextHookEx($hHook, $iCode, $iwParam, $ilParam)
EndFunc
You cant do while loops but you can do whatever so you can get a sleep in to pause for whatever length, just not indefinitely.

Codes for the keys.