BalfKeyBind is a pure JavaScript library that binds key presses.
Getting started
BalfKeyBind is extremely lightweight yet powerful, coming in at just under half a kilobyte in size when minified. Despite this, it works using a simple method that means that it doesn't add multiple event listeners - just one that deals with every key binding.
BalfKeyBind only features two functions: BalfKeyBind.Bind
and
BalfKeyBind.EnableTest
.
The Bind
function takes three
arguments, namely: keyCode
(number or string);
modifiers
(array) and func
(function).
The keyCode
is obviously the number of the key that is to be pressed.
modifiers
array ([]
) is a list of all modifiers as strings
(ctrl, alt, shift) and the func
is the function callback.
The EnableTest
function is designed to enable test mode, basically any key
presses are logged on the console so that it's easy to figure out the code. The function takes
just one argument: t
which is given a boolean value that states whether or
not test mode is enabled.
//The use of the $ in front of BalfKeyBind is to prevent conflict with the global BalfKeyBind function window.$BalfKeyBind = new BalfKeyBind();
Running the following code will enable test mode.
window.$BalfKeyBind.EnableTest(true);
Running the following script will then add a binding without any modifiers.
//This will produce a string of all cookies in use on this website var key = prompt("Insert a key code now."); window.$BalfKeyBind.Bind(key, [], function(e){ alert("The key code string " + e + " was pressed."); });
Running the following script will then add a binding with the control modifiers.
//This will produce a string of all cookies in use on this website var key = prompt("Insert a key code now."); window.$BalfKeyBind.Bind(key, ["ctrl"], function(e){ alert("The key code string " + e + " was pressed."); });
The following is the signature of the Bind function: