Advertisement
nuit

wKeybindings

Sep 6th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. wKeyBindings V0.2
  3.  
  4. Copyright (C) 2008  Jan Hoersch
  5. */
  6.  
  7. // MOOTOOLS REQUIRED http://mootools.net
  8. //  At Least: V1.11
  9.  
  10. Array.implement({
  11.  
  12.     /**
  13.      * compare - Returns true for equal Arrays
  14.      *
  15.      * @example
  16.      *  [1,4,5,2,5].compare([4,5,1,5,2]) == true
  17.      *  [1,4,5,2,5].compare([1,4,5,2,5},true) == true
  18.      *  [1,4,5,2,5].compare([4,1,2,5,5},true) == false
  19.      *  [1,3,2].compare([2,4,1]) == false
  20.      *
  21.      * @return  {Boolean}
  22.      * @param   {Object} Array
  23.      *      {Boolean} Position Sensitiv
  24.      */
  25.     compare: function(array,position) {
  26.         if(this.length != array.length) return false;
  27.  
  28.         if(position) {
  29.             for(i = 0; i < this.length; i++) {
  30.                 if(this[i] != array[i]) return false;
  31.             }
  32.         } else {   
  33.             var contain = false;
  34.             for(i = 0; i < this.length; i++) {
  35.                 for(y = 0; y < array.length; y++) {
  36.                     if(this[i] == array[y]) {
  37.                         array = array.unset(y);
  38.                         contain = true;
  39.                         break;
  40.                     }
  41.                 }
  42.                 if(!contain) {
  43.                     return false;
  44.                 } else {
  45.                     contain = false;
  46.                 }
  47.             }
  48.         }
  49.         return true;
  50.     },
  51.  
  52.  
  53.     /**
  54.      * unset - Unsetting a element in the array by there Index
  55.      *
  56.      * @example
  57.      *  [1,5,3].unset(1) == [1,3]
  58.      *
  59.      * @return  {Object} Array
  60.      * @param   {Object} Array
  61.      */
  62.     unset: function(key) {
  63.         var buffer = new Array();
  64.         this.each(function(item,index){
  65.             if(key != index) {
  66.                 buffer[buffer.length] = item;
  67.             }
  68.         });
  69.         return buffer;
  70.     },
  71. });
  72.  
  73. wKeybindings = new Class({
  74.     options: {
  75.         pressedKey: [],
  76.         actions: [],
  77.         keys: {
  78.             'tab' : 9,
  79.             'enter' : 13,
  80.             'shift' : 16,
  81.             'lstrg' : 17,
  82.             'lalt' : 18,
  83.             'capslock' : 20,
  84.  
  85.             'space' : 32,
  86.  
  87.             'left' : 37,
  88.             'up' : 38,
  89.             'right' : 39,
  90.             'down' : 40,
  91.  
  92.             '0' : 48,
  93.             '1' : 49,
  94.             '2' : 50,
  95.             '3' : 51,
  96.             '4' : 52,
  97.             '5' : 53,
  98.             '6' : 54,
  99.             '7' : 55,
  100.             '8' : 56,
  101.             '9' : 57,
  102.  
  103.             'a' : 65,
  104.             'b' : 66,
  105.             'c' : 67,
  106.             'd' : 68,
  107.             'e' : 69,
  108.             'f' : 70,
  109.             'g' : 71,
  110.             'h' : 72,
  111.             'i' : 73,
  112.             'j' : 74,
  113.             'k' : 75,
  114.             'l' : 76,
  115.             'm' : 77,
  116.             'n' : 78,
  117.             'o' : 79,
  118.             'p' : 80,
  119.             'q' : 81,
  120.             'r' : 82,
  121.             's' : 83,
  122.             't' : 84,
  123.             'u' : 85,
  124.             'v' : 86,
  125.             'w' : 87,
  126.             'x' : 88,
  127.             'y' : 89,
  128.             'z' : 90,
  129.  
  130.             'lApfel' : 91,
  131.             'rApfel' : 93,
  132.         }
  133.     },
  134.  
  135.     initialize: function(options) {
  136.         this.setOptions(options);
  137.  
  138.         window.addEvent('keydown', function(event) {
  139.             event = new Event(event);
  140.             if(!this.options.pressedKey.contains(event.code)) {
  141.                 this.options.pressedKey.push(event.code);
  142.             }
  143.             return this.check();
  144.         }.bind(this));
  145.  
  146.         window.addEvent('keyup', function(event) {
  147.             event = new Event(event);
  148.             this.options.pressedKey.remove(event.code);
  149.         }.bind(this));
  150.     },
  151.  
  152.     check: function() {
  153.         this.options.actions.each(function(item, index) {
  154.             if(item[0].compare(this.options.pressedKey)) {
  155.                 eval(item[1]);
  156.  
  157.                 this.options.pressedKey.empty();
  158.                 return true;
  159.             }
  160.         },this);
  161.     },
  162.  
  163.     addAction: function(pKey,script) {
  164.         pKey = pKey.map(function(item,index) {
  165.             if($type(item) == 'string') {
  166.                 eval('ret = this.options.keys.'+item+';');
  167.  
  168.                 return ret;
  169.             }
  170.             else if($type(item) == 'number') {
  171.                 return item;
  172.             }
  173.         },this);
  174.  
  175.         this.options.actions.push([pKey,script]);
  176.     }
  177. });
  178. wKeybindings.implement(new Events, new Options);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement