Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/01/2011, 21:28
Avatar de ClubIce
ClubIce
 
Fecha de Ingreso: diciembre-2008
Mensajes: 216
Antigüedad: 15 años, 10 meses
Puntos: 2
Exclamación Sistema de Deteccion de Teclado mejorado

Hola,

vengo aquí con mi problema que es el siguiente: Resulta que programando en Ruby, diseñé un sistema para detectar que teclas son precionadas mas facilmente. Y ahora quiero pasar el Script a JS, pero susede que, en Ruby para detectar el teclado usaba una Win32 API, pero como en JS no se puede usar eso, usé el evento onkeydown para detectar el teclado, pero susede que despues de aver precionado la tecla, si la detecta, pero la sigue detectando despues de aver soltado la tecla, entonses no se porque no me funciona.

Script Original Ruby:
Código Ruby:
Ver original
  1. module Keyboard
  2. #===============================================================================
  3. # ● List of Codes
  4. #===============================================================================
  5.   L_MOUSE   = 0x01;  R_MOUSE   = 0x02;  M_MOUSE   = 0x03;  ROLL_UP   = 0x04;
  6.   ROLL_DOWN = 0x05;  BACK      = 0x08;  TAB       = 0x09;  ENTER     = 0x0D;
  7.   SHIFT     = 0x10;  CTRL      = 0x11;  ALT       = 0x12;  PAUSE     = 0x13;
  8.   BLOQ_CAPS = 0x14;  ESCAPE    = 0x1B;  SPACE     = 0x20;  PAGE_UP   = 0x21;
  9.   PAGE_DOWN = 0x22;  _END      = 0x23;  HOME      = 0x24;  LEFT      = 0x25;
  10.   UP        = 0x26;  RIGHT     = 0x27;  DOWN      = 0x28;  SELECT    = 0x29;
  11.   PRINT     = 0x2A;  SNAPSHOT  = 0x2C;  INSERT    = 0x2D;  DELETE    = 0x2E;
  12.   NUM0      = 0x30;  NUM1      = 0x31;  NUM2      = 0x32;  NUM3      = 0x33;
  13.   NUM4      = 0x34;  NUM5      = 0x35;  NUM6      = 0x36;  NUM7      = 0x37;
  14.   NUM8      = 0x38;  NUM9      = 0x39;  A         = 0x41;  B         = 0x42;
  15.   C         = 0x43;  D         = 0x44;  E         = 0x45;  F         = 0x46;
  16.   G         = 0x47;  H         = 0x48;  I         = 0x49;  J         = 0x4A;
  17.   K         = 0x4B;  L         = 0x4C;  M         = 0x4D;  N         = 0x4E;
  18.   O         = 0x4F;  P         = 0x50;  Q         = 0x51;  R         = 0x52;
  19.   S         = 0x53;  T         = 0x54;  U         = 0x55;  V         = 0x56;
  20.   W         = 0x57;  X         = 0x58;  Y         = 0x59;  Z         = 0x5A;
  21.   L_WIN     = 0x5B;  R_WIN     = 0x5C;  APPS      = 0x5D;  NUMPAD0   = 0x60;
  22.   NUMPAD1   = 0x61;  NUMPAD2   = 0x62;  NUMPAD3   = 0x63;  NUMPAD4   = 0x64;
  23.   NUMPAD5   = 0x65;  NUMPAD6   = 0x66;  NUMPAD7   = 0x67;  NUMPAD8   = 0x68;
  24.   NUMPAD9   = 0x69;  MULTIPLY  = 0x6A;  ADD       = 0x6B;  SUBTRACT  = 0x6C;
  25.   SEPARATOR = 0x6D;  DECIMAL   = 0x6E;  DIVIDE    = 0x6F;  F1        = 0x70;
  26.   F2        = 0x71;  F3        = 0x72;  F4        = 0x73;  F5        = 0x74;
  27.   F6        = 0x75;  F7        = 0x76;  F8        = 0x77;  F9        = 0x78;
  28.   F10       = 0x79;  F11       = 0x7A;  F12       = 0x7B;  BLOQ_NUM  = 0x90;
  29.   BLOQ_SCROLL=0x91;  L_SHIFT   = 0xA0;  R_SHIFT   = 0xA1;  L_CONTROL = 0xA2;
  30.   R_CONTROL = 0xA3;  L_ALT     = 0xA4;  R_ALT     = 0xA5;  SEP       = 0xBC;
  31.   DASH      = 0xBD;  DOTT      = 0xBE;
  32. #===============================================================================
  33. # ● List of Names
  34. #===============================================================================
  35.   Names = {
  36.   0x01 => "LEFT CLICK",       0x02 => "RIGHT CLICK",
  37.   0x03 => "ROLL UP",          0x04 => "MIDDLE CLICK",
  38.   0x05 => "ROLL DOWN",        0x08 => "BAKC",
  39.   0x09 => "TAB",              0x0D => "ENTER",
  40.   0x10 => "SHIFT",            0x11 => "CONTROL",
  41.   0x12 => "ALT",              0x13 => "PAUSE",
  42.   0x14 => "BLOQ. CAPS.",      0x1B => "ESCAPE",
  43.   0x20 => "SPACE",            0x21 => "PAGE UP",
  44.   0x22 => "PAGE DOWN",        0x23 => "END",
  45.   0x24 => "INICIO",           0x25 => "LEFT",
  46.   0x26 => "UP",               0x27 => "RIGHT",
  47.   0x28 => "DOWN",             0x29 => "SELECTION",
  48.   0x2A => "PRINT",            0x2C => "PRINT SCREEN",
  49.   0x2D => "INSERT",           0x2E => "DELETE",
  50.   0x30 => "0",  0x31 => "1",  0x32 => "2",  0x33 => "3",
  51.   0x34 => "4",  0x35 => "5",  0x36 => "6",  0x37 => "7",
  52.   0x38 => "8",  0x39 => "9",  0x41 => "A",  0x42 => "B",
  53.   0x43 => "C",  0x44 => "D",  0x45 => "E",  0x46 => "F",
  54.   0x47 => "G",  0x48 => "H",  0x49 => "I",  0x4A => "J",
  55.   0x4B => "K",  0x4C => "L",  0x4D => "M",  0x4E => "N",
  56.   0x4F => "O",  0x50 => "P",  0x51 => "Q",  0x52 => "R",
  57.   0x53 => "S",  0x54 => "T",  0x55 => "U",  0x56 => "V",
  58.   0x57 => "W",  0x58 => "X",  0x59 => "Y",  0x5A => "Z",
  59.   0x5B => "LEFT WIN.",        0x5C => "RIGHT WIN.",
  60.   0x5D => "APPLICATIONS",     0x60 => "NUM 0",
  61.   0x61 => "NUM 1",            0x62 => "NUM 2",
  62.   0x63 => "NUM 3",            0x64 => "NUM 4",
  63.   0x65 => "NUM 5",            0x66 => "NUM 6",
  64.   0x67 => "NUM 7",            0x68 => "NUM 8",
  65.   0x69 => "NUM 9",            0x6A => "MULTIPLY",
  66.   0x6B => "ADDITION",         0x6C => "SEPARATOR",
  67.   0x6D => "SUBSTRACT",        0x6E => "DECIMAL",
  68.   0x6F => "DIVIDITION",          0x70 => "F1",
  69.   0x71 => "F2",   0x72 => "F3",   0x73 => "F4",  0x74 => "F5",
  70.   0x75 => "F6",   0x76 => "F7",   0x77 => "F8",  0x78 => "F9",
  71.   0x79 => "F10",  0x7A => "F11",  0x7B => "F12",
  72.   0x90 => "BLOQ. NUM.",           0x91 => "BLOQ. SCROLL",
  73.   0xA0 => "LEFT SHIFT",      0xA1 => "RIGHT SHIFT",
  74.   0xA2 => "LEFT CONTROL",    0xA3 => "RIGHT CONTROL",
  75.   0xA4 => "LEFT ALT",        0xA5 => "RIGHT ALT",
  76.   0xBC => "SEP",  0xBD => "DASH",  0xBE => "DOTT"
  77.   }  
  78.   GetKeyState = Win32API.new("user32", "GetAsyncKeyState", "i", "i")
  79.   KeyRepeatCounter = {}
  80.  
  81.   module_function
  82.  
  83.   def self.update
  84.     for key in KeyRepeatCounter.keys
  85.       if (GetKeyState.call(key).abs & 0x8000 == 0x8000)
  86.         KeyRepeatCounter[key] += 1
  87.       else
  88.         KeyRepeatCounter.delete(key)
  89.       end
  90.     end
  91.   end
  92.   def self.press?(key)
  93.     return false if key == nil
  94.     return true unless KeyRepeatCounter[key].nil?
  95.     return key_pressed?(key)
  96.    end
  97.   def self.trigger?(key)
  98.     return false if key == nil
  99.     count = KeyRepeatCounter[key]
  100.     press = count.nil? ? key_pressed?(key) : false
  101.     return (count == 0 or press)
  102.   end
  103.   def self.repeat?(key)
  104.     return false if key == nil
  105.     count = KeyRepeatCounter[key]
  106.     return true if count == 0
  107.     if count.nil?
  108.       return key_pressed?(key)
  109.     else
  110.       return (count >= 23 and (count - 23) % 6 == 0)
  111.     end
  112.   end
  113.   def self.key_pressed?(key)
  114.     if (GetKeyState.call(key).abs & 0x8000 == 0x8000)
  115.       KeyRepeatCounter[key] = 0
  116.       return true
  117.     end
  118.     return false
  119.   end
  120.   def self.getKey
  121.     for i in 0...256
  122.       if GetKeyState.call(i) == -32767
  123.         key = i
  124.         break
  125.       end
  126.     end
  127.     return if key == nil
  128.     return Names[key] != nil ? key : 0
  129.   end
  130. end

Adaptado JS:

Código Javascript:
Ver original
  1. Keyboard = {
  2.     keyLog:[],
  3.     setup:function () {
  4.         document.onkeypress = function(event) {
  5.             Keyboard.keyCode = event == null ? window.event.keyCode : event.keyCode
  6.         }
  7.     },
  8.     update:function () {
  9.         for (var i in Keyboard.KeyLog) {
  10.             if (Keyboard.keyCode == i) {
  11.                 Keyboard.keyLog[i]++
  12.             } else {
  13.                 Keyboard.keyLog[i] = undefined
  14.             }
  15.         }
  16.     },
  17.     press: function (key) {
  18.         if (key == undefined) return false;
  19.         if (Keyboard.keyLog[key] != undefined) return true;
  20.         return Keyboard.keyPressed(key)
  21.     },
  22.     trigger:function (key) {
  23.         if (key == undefined) return false;
  24.         return (Keyboard.keyLog[key] == 0 || (Keyboard.keyLog[key] == undefined ? Keyboard.keyPressed(key) : false))
  25.     },
  26.     repeat:function (key) {
  27.         if (key == undefined) return false;
  28.         if (Keyboard.keyLog[key] == 0) return true;
  29.         if (Keyboard.keyLog[key] == undefined) {
  30.             return Keyboard.keyPressed(key)
  31.         } else {
  32.             return (Keyboard.keyLog[key] >= 23 && (Keyboard.keyLog[key] - 23) % 6 == 0)
  33.         }
  34.     },
  35.     keyPressed:function (key) {
  36.         if (Keyboard.keyCode == key) {
  37.             Keyboard.keyLog[key] = 0
  38.             return true
  39.         }
  40.         return false
  41.     }
  42. }

Demo de su funcionamiento:

Código Javascript:
Ver original
  1. Keyboard.setup();
  2. setInterval(checkInput,1);
  3. function checkInput () {
  4.   Keyboard.update();
  5.   if (Keyboard.trigger(13)) {
  6.     alert('has precionado Enter')
  7. }
  8. }