This is a library that makes it easy to add user-configurable hotkeys and settings to your script.
It's basically a cut-down version of UCR (See Signature). Sometimes I just want something simpler than full-blown UCR, and also I am starting to move towards implementing UCR in C# (But still allowing AHK plugins, of course...), so I thought it would be cool to distill down what UCR does into as simple a script as possible, so people can easily understand and improve the basic techniques I used.
The only external library is CoCo's JSON library, used for reading and writing settings
It has the following features:
Custom Hotkey GuiControl that accepts:
- Any legal AHK hotkey combination
- Normal options available for AHK hotkeys (Block ~, Wild *)
- "Suppress Repeats" option to suppress repeat down events for keyboard keys
- Bind joystick POV (Hat) directions as if they were buttons
- Accurate notification of release of joystick buttons
Code for this example:
Code: Select all
#SingleInstance force
#NoEnv
#Include AppFactory.ahk
factory := new AppFactory()
factory.AddInputButton("HK1", "w200", Func("InputEvent").Bind("1"))
factory.AddInputButton("HK2", "xm w200", Func("InputEvent").Bind("2"))
factory.AddControl("MyEdit", "Edit", "xm w200", "Default Value", Func("GuiEvent").Bind("MyEdit"))
factory.AddControl("MyDDL", "DDL", "xm w200 AltSubmit", "One||Two|Three", Func("GuiEvent").Bind("MyDDL"))
Gui, Show, x0 y0
return
InputEvent(ctrl, state){
Global factory
Tooltip % "Input " ctrl " changed state to: " state " after " A_TickCount " ticks while MyEdit was '" factory.GuiControls.MyEdit.Get() "'"
}
GuiEvent(ctrl, state){
Tooltip % "GuiControl " ctrl " changed state to: '" state "' after " A_TickCount " ticks"
}
^Esc::
GuiClose:
ExitApp
AppFactory comprises of 3 main parts (Classes):
AppFactory
This is the main class. It handles injecting the custom GuiControls, and spins up AHK_H threads for the other two classes
BindModeThread
Handles "Bind Mode". When the user wishes to select a new hotkey, they select Bind Mode, a dialog appears, and they hit the button they wish to use.
Declares a hotkey to every keyboard key, mouse button, joystick button and watches joystick hats
InputThread
Handles input detection for all Hotkey controls in the script.
Normal hotkeys, plus fixes and enhancements to default AHK functionality, ie:
Up events for joystick buttons now fire accurately
POV hat directions (Up, Down, Left, Right) can be mapped as if they were a button.
Multiple threads are used as AHK does not support "Hotkey Sets" - ie there is no way to quickly turn on or off lots of hotkeys.
Seeing as bind mode needs to bind to every key on the keyboard, entering bind mode without being able to just Suspend, Off is much slower.
Also, were the input handler in the same thread as the bind handler, to enter bind mode you would have to redeclare all hotkeys (For Bind Mode), then undeclare them all, then re-declare all the user's hotkeys.
The InputThread could conceivably be in the main thread in this incarnation (In UCR, you can switch profiles, so I have one InputThread per profile), but this would make debugging of users scripts annoying if you step in, as the timers used for hat and button release detection would get hit.
Compilation
Place the following two lines at the start of your script
Code: Select all
FileInstall, ..\Source\BindModeThread.ahk, BindModeThread.ahk
FileInstall, ..\Source\InputThread.ahk, InputThread.ahk
If you download the included zip and your script is in the same folder as Example.ahk, this code should work as-is