wmii keyboard events handler for node.js. Depends on wmiir.js and wmii_events.js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// wmii keybindings and keyboard event module | |
// Adam R. Nelson | |
// August 2013 | |
var wmiir = require('./wmiir.js'); | |
var events = require('./wmii_events.js'); | |
var globalKeys = {}; | |
var modeKeys = {}; | |
var currentMode = null; | |
var modes = {}; | |
function rewriteKeyFile(callback) { | |
var keyData = Object.keys(modeKeys).concat(Object.keys(globalKeys)).join('\n'); | |
wmiir.write('/keys', keyData, callback); | |
} | |
events.on('Key', function(key) { | |
var handler = modeKeys[key] || globalKeys[key]; | |
if (handler) handler(); | |
}); | |
// Binds the key `key` to the function `keyAction`, globally. | |
// If `callback` is passed, it will be called after the keybinding is written | |
// to wmii's filesystem. | |
exports.bindKey = function(key, keyAction, callback) { | |
globalKeys[key] = keyAction; | |
rewriteKeyFile(callback); | |
}; | |
// Takes an object (`keyBindings`) that maps key names to callback functions. | |
// Each key will be bound, globally, to its value (which should be a function). | |
// If `callback` is passed, it will be called after all keybindings are written | |
// to wmii's filesystem. | |
exports.bindKeys = function(keyBindings, callback) { | |
for (var key in keyBindings) { | |
globalKeys[key] = keyBindings[key]; | |
} | |
rewriteKeyFile(callback); | |
}; | |
// Removes the global keybinding for `key`, if one exists. Keybindings | |
// belonging to modes will be unaffected. | |
// If `callback` is passed, it will be called after the keybinding changes are | |
// written to wmii's filesystem. | |
exports.unbindKey = function(key, callback) { | |
globalKeys[key] = undefined; | |
rewriteKeyFile(callback); | |
}; | |
// Removes all global keybindings. Keybindings belonging to modes will be | |
// unaffected. | |
// If `callback` is passed, it will be called after the keybinding changes are | |
// written to wmii's filesystem. | |
exports.unbindAll = function(callback) { | |
globalKeys = {}; | |
rewriteKeyFile(callback); | |
}; | |
// Defines a mode, with the name `modeName`. `keyBindings` should be an object | |
// that maps key names to callback functions. Within the new mode, all of the | |
// keys in `keyBindings` will be bound to their valies (which should be | |
// functions). | |
// If a mode named `modeName` already exists, this mode will replace it. | |
exports.defineMode = function(modeName, keyBindings) { | |
modes[modeName] = keyBindings; | |
}; | |
// Returns the name of the currently-active mode. | |
exports.getMode = function() { | |
return currentMode; | |
}; | |
// Sets the current mode to `modeName`, if a mode with that name exists. | |
// Unbinds all keys bound in the current mode, and binds all keys belonging to | |
// the new mode. | |
// Emits a 'ModeChanged' event if the mode was changed successfully, or an | |
// 'Error' event if it was not. | |
// If `callback` is passed, it will be called after the keybinding changes are | |
// written to wmii's filesystem. It will be passed one argument, a boolean, | |
// indicating whether the mode change succeeded. | |
exports.setMode = function(modeName, callback) { | |
try { | |
var mode = modes[modeName]; | |
if (!mode) { | |
events.emit('Error', 'No such mode: ' + modeName); | |
if (callback) callback(false); | |
return; | |
} | |
currentMode = modeName; | |
modeKeys = mode; | |
rewriteKeyFile(function(err) { | |
if (err) { | |
events.emit('Error', err.message || err); | |
} else { | |
events.emit('ModeChanged', modeName); | |
} | |
if (callback) callback(true); | |
}); | |
} catch (ex) { | |
console.log(ex); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment