You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.0KB

  1. /** Modified from original Node-Red source, for audio system visualization
  2. * vim: set ts=4:
  3. * Copyright 2013 IBM Corp.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. **/
  17. RED.keyboard = (function() {
  18. var active = true;
  19. var handlers = {};
  20. d3.select(window).on("keydown",function() {
  21. if (!active) { return; }
  22. var handler = handlers[d3.event.keyCode];
  23. if (handler && handler.ondown) {
  24. if (!handler.modifiers ||
  25. ((!handler.modifiers.shift || d3.event.shiftKey) &&
  26. (!handler.modifiers.ctrl || d3.event.ctrlKey ) &&
  27. (!handler.modifiers.alt || d3.event.altKey ) )) {
  28. handler.ondown();
  29. }
  30. }
  31. });
  32. d3.select(window).on("keyup",function() {
  33. if (!active) { return; }
  34. var handler = handlers[d3.event.keyCode];
  35. if (handler && handler.onup) {
  36. if (!handler.modifiers ||
  37. ((!handler.modifiers.shift || d3.event.shiftKey) &&
  38. (!handler.modifiers.ctrl || d3.event.ctrlKey ) &&
  39. (!handler.modifiers.alt || d3.event.altKey ) )) {
  40. handler.onup();
  41. }
  42. }
  43. });
  44. function addHandler(key,modifiers,ondown,onup) {
  45. var mod = modifiers;
  46. var cbdown = ondown;
  47. var cbup = onup;
  48. if (typeof modifiers == "function") {
  49. mod = {};
  50. cbdown = modifiers;
  51. cbup = ondown;
  52. }
  53. handlers[key] = {modifiers:mod, ondown:cbdown, onup:cbup};
  54. }
  55. function removeHandler(key) {
  56. delete handlers[key];
  57. }
  58. return {
  59. add: addHandler,
  60. remove: removeHandler,
  61. disable: function(){ active = false;},
  62. enable: function(){ active = true; }
  63. }
  64. })();