Android Tutorial Introduction to customizing B4A / B4J with Autohotkey

From time to time there are feature requests and wishes that are too individual to be implemented in the IDE and can easily be done in Autohotkey. For those who do not already know this sometimes weird but outstandingly powerful tool here is a little - all but comprehensive - introduction.

You can get AHK here:http://ahkscript.org/download/
--
AHK is a mixture of Hotkey, Hotstring and Scripting tool. You can very easily modify single keys, key-combinations as well as define per app or system-wide hotstrings (key triggerd strings). AHK provides powerful commands for Window manipulations and you can even write complete GUI apps for Windows. In fact you will find Linux users in their forums to envy AHK for its power. Sounds like the perfect tool and, well it is. On the other hand it's syntax is rather inconsistent and not every command will work with every target. Some trial end error is required but once a certain solution is working I never experienced any instability or unpredictable behaviour.

Autohotkey is a runtime for 32 and 64bit Windows of all flavours and uses plain text files with .ahk suffix as input. Scripts may be compiled to .exe as well. AHK comes with AutoIt Windows Spy, a great source of information. Have a look at it.

A typical script looks like this - you may copy it or use the attached zip as a starting point. Don't forget to reload script after changes (right mouse on traycon):

B4X:
;use semicolon for single line coments

#persistent ; make script resident - in most cases not necessary but never a bad idea
#WinActivateForce ; use more forceful windows activation, not necessary but sometimes better
DetectHiddenWindows, Off ; don't match titles of hidden windows with WinTitle/WinText
SetTitleMatchMode, 2 ; WinTitle may occur anywhere to match


/*
begin multiline comment
Hotkeys are very easily assigned:
Key::TargetKey  e.g.  F8::LWin  lets F8 fire LeftWin
SpecialKeys can be adressed by:
# = WIN-Taste
! = Alt
^ = Ctrl
+ = Shift
Single line Hotkeys are not allways reliable. While Key::AltTab works only as a single line Hotkey many others only work with send which is more reliable e.g:

F8::
send, {LWin}
return

End multiline comment
*/

; some general purpose and B4A examples:
; hold left mouse button and press right button to alt-tab through windows
;~ prevents consuming LButton:

~LButton & RButton::AltTab

;run calculator if NumLock is pressed (and prevent keypad to be deactivated):
NumLock::
ifwinexist, ahk_exe calc.exe
{
winactivate, ahk_exe calc.exe
return
}
else
{
run, calc.exe
return
}
; Ctrl-PageUp/Down: Fullscreen on/off for (allmost) all scalable apps
; removes title bar and boarders and maximizes window
; works with all scalable windows but e.g. Firefox doesn't benefit, its own fullscreen is better
; more screen real-estate in B4A!

^PgUp::
WinSet, style, -0xC40000, A
Send, #{Up}
return
^PgDn::
WinSet, style, +0xC40000, A
Send, #{Down}
return
;now some B4A related:
;======================== B4A ============================
; let these work only when B4A window is activated:
#IfWinActive, ahk_exe Basic4android.exe

; Hotstring defined by :*:trigger::string
; typing .log writes Log(") and positions the cursor after "
:*:.Log::Log("){Left}

; typing .sub sends Sub, blank, new line, End Sub and positions curser after blank
:*:.Sub::Sub {Enter}End Sub{Up}{End}

;.If creates a complete If Then Else structure
:*:.If::If  Then{Enter}{Enter}Else{Tab}{Enter}End If{Up}{Up}{End}{Tab}{Up}{End}{Left}{Left}{Left}{Left}{Left}
:*:.Else If::Else If  Then{Left}{Left}{Left}{Left}{Left}
:*:endif::End If

; mostly useless capslock triggers event-autocomplete
CapsLock::
send, Sub
send, {Space}
send, {Tab}
return

;AppsKey (windows menu key) triggers autocomplete
AppsKey::
Send, ^{Space}
return
;F1 triggers compile in background
F1::
send, !3
return

#IfWinActive ; end B4A only section
;======================== B4A ============================

; have fun!
 

Attachments

  • B4A.zip
    2.6 KB · Views: 461
Last edited:

nikolaus

Member
Licensed User
Longtime User
New Header for full support of the latest AHK version:

B4X:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#persistent
#WinActivateForce
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
DetectHiddenWindows, Off
SetTitleMatchMode, 2


And a little code enhancement:

B4X:
; Fullscreen for (allmost) all scalable Apps
^PgUp::
WinSet, style, -0xC40000, A
; => add the following line to avod problems with windows already maximized:
Winrestore, A
Send, #{Up}
return

^PgDn::
WinSet, style, +0xC40000, A
Send, #{Down}
return
; --
 
Last edited:

nikolaus

Member
Licensed User
Longtime User
Workaround for autocomplete problem #End If - #End Region

Add to B4A section:

B4X:
:*:#endregion::
sendraw, #EndRegion
send, {Left}{Left}{Left}{Left}{Left}{Left}{Space}{End}
return

If you type '#endregion' (or whatever trigger-word you choose) it will type '#End Region' overcomming B4A autocomplete. Erel could probably add a similar function into autocomplete.
 

kiki78

Active Member
Licensed User
Longtime User
Hi nikolaus,

I made lot of B4A library and I need to automatically compil it in good order.
I try to use ahkscript to do that, but I stall on one function.
I need to wait the compil completion when window display "Completed successfully." to exit but never found solution.

This is my script

B4X:
Loop, read, %A_ScriptDir%\CompileOrder.txt
{
    Run, %A_ScriptDir%\%A_LoopReadLine%, , , B4APID
    WinWait ahk_pid %B4APID%
    Send !(

    // Here I want to wait compil completion

    WinClose ahk_pid %B4APID%  // Close Compil window
    WinClose ahk_pid %B4APID%  // Close B4A
}

CompilOrder.txt is text file with one line relative path for each library project.

May you help me ?
Kind Regards
 

kiki78

Active Member
Licensed User
Longtime User
Hi,

Finally I solve my problem.
Since December, Erel add BA4Builder program that simplify automatic compilation.

So my "CompileAll.ahk" script is now :
B4X:
CompilerExe = C:\AppWin32\Basic4android\B4ABuilder.exe
AllOk = 1

Loop, read, %A_ScriptDir%\CompileOrder.txt
{
    RunWait, %CompilerExe% -task=buildlibrary -basefolder=%A_ScriptDir%\%A_LoopReadLine% -optimize=true, , UseErrorLevel
 
    if ErrorLevel {
        MsgBox Compilation error in library %A_LoopReadLine%
        AllOk = 0
        break
    }
}

if AllOk {
    MsgBox All compilations  done.
}

and associated CompilOrder.txt is simple text file with one library directory by line and organize in wanted compilation order.
B4X:
MyLibrary01
MyLibrary02

I just put this 2 files at root of my libraries directory and with just double click I compile all my libraries :)
 
Top