abidibo.net

wordevents documentation

Welcome to the wordevents documentation page. For a quick start guide, take a look at the demo page.

wordevents

This is the class that has to be instantiated in order to get a wordevents object. Let's see the public and useful methods. After constructing an instance, a dictionary has to be created in order to execute callbacks depending on the typed in word, then the object has to be activated in order to start listening to the typed in words.

constructor

Constructs a wordevents instance. The syntax is:

var myinstance = new wordevents(options)

The available options are:

  • target: (string|element). The id attribute or the element itself to which attach the event listener, default document
  • digit_interval: (int). The time interval (ms) between two typed characters in order to be considered belonging to the same word, default 500ms
  • event_type: (string). The keyboard event type, default 'keyup'
  • acceptedCode: (function). a function used to check if the pressed key has to be considered or not. It takes the argument evt which is the event object. The key code of the pressed key is accessible through evt.code, while the target element through evt.target. The target property is useful to avoid listening when user types inside input elements for example. This function must return true or false.

Example:

var myinstance = new wordevents({
  'target': document.body,
  'digit_interval': 400,
  'event_type': 'keydown',
  'acceptedCode': function(evt) {
    return (evt.code > 64 && evt.code < 91) || false; // only [a-z]
  }
});

listen

Inserts one or more words in the dictionary with the relative callbacks. The syntax is:

myinstance.listen(words, callbacks);

  • words: (String|RegExp|Array) a word, a regular expression or an array of words/regular expressions
  • callbacks: (Function|Array) a callback function or an array of callback functions

Example:

myinstance.listen('myword', myfunction); // single arguments
myinstance.listen(['word1', /^me[oa]w/], [callback1, callback2]); // array arguments

unlisten

Removes one or more words/regular expressions from the dictionary. The syntax is:

myinstance.unlisten(words);

  • words: (String|Array) a word or an array of words

Example:

myinstance.unlisten('myword'); // single arguments
myinstance.listen(['word1', 'word2']); // array arguments

activate

Activates the listener, the object starts listening to typed words. The syntax is:

myinstance.activate();

deactivate

Deactivates the listener, the object stops listening to typed words. The syntax is:

myinstance.deactivate();

project page demo download