Open Gum-Joe opened 5 years ago
AHK doesn't appear to support returning functions. As such, separte methods must be used:
reserveCallback(id)
callback(err) {
resolveCallback()
}
Function(callback) {
callback(null)
}
Function(callback)
Edit: Added this to issues
This is the feature tracker/list of features for the 2Keys helper libraries for AHK to replace functions that don't work with 2Keys (i.e. GetKeyState) and add helpers for i.e. cross script variables
What are the 2Keys helper libraries?
Some AHK functions, such as
GetKeyState
obviously won't work with 2Keys as the keyboards are physically plugged into a separte device. The aim of the 2Keys helper libraries is to add support for these functions, whether that be through the use of:In addition, the helper library would also provide some function to help with execution of scripts, script communcation and the development of scripts.
Ideally, it would also be possible to extend the helpers with your own scripts, alllowing for custom code to be used between detector and scripts.
Submitting a proposal
Include:
The root import is
#Include <2Keys>
, which is the dir which contains all the helpers in their categoriesCategories
#Include <2Keys/core>
#Include <2Keys/vars>
#Include <2Keys/keyboards>
Helpers
Prelude
The prelude is the section of AHK code executed before the user's specified function is executed.
Global variables
These are variables, defined in the prelude, that are accessible by all scripts (including ones imported with
#Include
) being executed.TWOKEYS_CWD
: Defines the current working directory of the 2Keys process. ✅ Added by 2f1e45bCore helpers
2Keys.core.callbacks
Designed for use by runner to make calback functions so 2Keys native addons stops execution once the script is done. Ideally, a callback generator would be used, but AHK doesn't appear to support returning of functions from functions. As such, methods are used for now to use an INI file with statuses
pending
,resolved
(script done) orrejected
(script errored).Needed due to #3.
2Keys.core.callbacks.reserveCallbackID(id)
Where
id
is a unique 64 bit ID used to identify the execution (so that the correct callback stops execution of the correct program).How it works
NOTE: Ideally sockets or IPC would be used, but this solution is simpler; a 'bodge' is ok here.
On execution of this function, an entry is added to a
callback.ini
file in~/.2Keys/ini/callback.ini
:2Keys.core.callbacks.defaultCallback(id, err)
Where
id
is a unique 64 bit ID used to identify the execution (so that the correct callback stops execution of the correct program).Where
err
is an error string or a 2Keys AHK error object.How it works
On execution of this function, the
callback.ini
file is edited:If there is an error in the script, provided as
err
:The executor then sees this and invokes a nodejs callback function, supplying the error if necessary.
enabled
is also set to false.Note: how function would be executed using callbacks:
If the user wanted to pass the callback to another function for use, such as a callback for a GUI, the AutoHotkey code would be as follows:
NB: Since the addon may be blocking, Nodejs thread/workers may be needed.
Keyboard helpers
2Keys.keyboards.getKeyState(keyboard, key, callback)
Where
key
is the name of the key to lookup (seedetector/util/keyboard_map.py
) Wherekeyboard
is the keyboard to lookupkey
in Wherecallback
is a function to run when the state is received.Returns either
up
ordown
, ortrue
(toggled on)/false
(toggles off) for keys such as Numpad and Caps Lock.How it works
This function allows a user to fetch the state of a key from any keyboard attached to the detector It works like so:
npm
or viasocket.io
receives the request and forwards it viasocket.io
to the detectorsocket.io
to the serverSample syntax