2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 RED.keyboard = (function() {
21 d3.select(window).on("keydown",function() {
22 if (!active) { return; }
23 var handler = handlers[d3.event.keyCode];
24 if (handler && handler.ondown) {
25 if (!handler.modifiers ||
26 ((!handler.modifiers.shift || d3.event.shiftKey) &&
27 (!handler.modifiers.ctrl || d3.event.ctrlKey ) &&
28 (!handler.modifiers.alt || d3.event.altKey ) )) {
33 d3.select(window).on("keyup",function() {
34 if (!active) { return; }
35 var handler = handlers[d3.event.keyCode];
36 if (handler && handler.onup) {
37 if (!handler.modifiers ||
38 ((!handler.modifiers.shift || d3.event.shiftKey) &&
39 (!handler.modifiers.ctrl || d3.event.ctrlKey ) &&
40 (!handler.modifiers.alt || d3.event.altKey ) )) {
45 function addHandler(key,modifiers,ondown,onup) {
50 if (typeof modifiers == "function") {
55 handlers[key] = {modifiers:mod, ondown:cbdown, onup:cbup};
57 function removeHandler(key) {
63 remove: removeHandler,
64 disable: function(){ active = false;},
65 enable: function(){ active = true; }