|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
-
- RED.keyboard = (function() {
-
- var active = true;
- var handlers = {};
-
- d3.select(window).on("keydown",function() {
- if (!active) { return; }
- var handler = handlers[d3.event.keyCode];
- if (handler && handler.ondown) {
- if (!handler.modifiers ||
- ((!handler.modifiers.shift || d3.event.shiftKey) &&
- (!handler.modifiers.ctrl || d3.event.ctrlKey ) &&
- (!handler.modifiers.alt || d3.event.altKey ) )) {
- handler.ondown();
- }
- }
- });
- d3.select(window).on("keyup",function() {
- if (!active) { return; }
- var handler = handlers[d3.event.keyCode];
- if (handler && handler.onup) {
- if (!handler.modifiers ||
- ((!handler.modifiers.shift || d3.event.shiftKey) &&
- (!handler.modifiers.ctrl || d3.event.ctrlKey ) &&
- (!handler.modifiers.alt || d3.event.altKey ) )) {
- handler.onup();
- }
- }
- });
- function addHandler(key,modifiers,ondown,onup) {
- var mod = modifiers;
- var cbdown = ondown;
- var cbup = onup;
-
- if (typeof modifiers == "function") {
- mod = {};
- cbdown = modifiers;
- cbup = ondown;
- }
- handlers[key] = {modifiers:mod, ondown:cbdown, onup:cbup};
- }
- function removeHandler(key) {
- delete handlers[key];
- }
-
- return {
- add: addHandler,
- remove: removeHandler,
- disable: function(){ active = false;},
- enable: function(){ active = true; }
- }
-
- })();
|