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
module Keyboard #=============================================================================== # ● List of Codes #=============================================================================== L_MOUSE = 0x01; R_MOUSE = 0x02; M_MOUSE = 0x03; ROLL_UP = 0x04; ROLL_DOWN = 0x05; BACK = 0x08; TAB = 0x09; ENTER = 0x0D; SHIFT = 0x10; CTRL = 0x11; ALT = 0x12; PAUSE = 0x13; BLOQ_CAPS = 0x14; ESCAPE = 0x1B; SPACE = 0x20; PAGE_UP = 0x21; PAGE_DOWN = 0x22; _END = 0x23; HOME = 0x24; LEFT = 0x25; UP = 0x26; RIGHT = 0x27; DOWN = 0x28; SELECT = 0x29; PRINT = 0x2A; SNAPSHOT = 0x2C; INSERT = 0x2D; DELETE = 0x2E; NUM0 = 0x30; NUM1 = 0x31; NUM2 = 0x32; NUM3 = 0x33; NUM4 = 0x34; NUM5 = 0x35; NUM6 = 0x36; NUM7 = 0x37; NUM8 = 0x38; NUM9 = 0x39; A = 0x41; B = 0x42; C = 0x43; D = 0x44; E = 0x45; F = 0x46; G = 0x47; H = 0x48; I = 0x49; J = 0x4A; K = 0x4B; L = 0x4C; M = 0x4D; N = 0x4E; O = 0x4F; P = 0x50; Q = 0x51; R = 0x52; S = 0x53; T = 0x54; U = 0x55; V = 0x56; W = 0x57; X = 0x58; Y = 0x59; Z = 0x5A; L_WIN = 0x5B; R_WIN = 0x5C; APPS = 0x5D; NUMPAD0 = 0x60; NUMPAD1 = 0x61; NUMPAD2 = 0x62; NUMPAD3 = 0x63; NUMPAD4 = 0x64; NUMPAD5 = 0x65; NUMPAD6 = 0x66; NUMPAD7 = 0x67; NUMPAD8 = 0x68; NUMPAD9 = 0x69; MULTIPLY = 0x6A; ADD = 0x6B; SUBTRACT = 0x6C; SEPARATOR = 0x6D; DECIMAL = 0x6E; DIVIDE = 0x6F; F1 = 0x70; F2 = 0x71; F3 = 0x72; F4 = 0x73; F5 = 0x74; F6 = 0x75; F7 = 0x76; F8 = 0x77; F9 = 0x78; F10 = 0x79; F11 = 0x7A; F12 = 0x7B; BLOQ_NUM = 0x90; BLOQ_SCROLL=0x91; L_SHIFT = 0xA0; R_SHIFT = 0xA1; L_CONTROL = 0xA2; R_CONTROL = 0xA3; L_ALT = 0xA4; R_ALT = 0xA5; SEP = 0xBC; DASH = 0xBD; DOTT = 0xBE; #=============================================================================== # ● List of Names #=============================================================================== Names = { 0x01 => "LEFT CLICK", 0x02 => "RIGHT CLICK", 0x03 => "ROLL UP", 0x04 => "MIDDLE CLICK", 0x05 => "ROLL DOWN", 0x08 => "BAKC", 0x09 => "TAB", 0x0D => "ENTER", 0x10 => "SHIFT", 0x11 => "CONTROL", 0x12 => "ALT", 0x13 => "PAUSE", 0x14 => "BLOQ. CAPS.", 0x1B => "ESCAPE", 0x20 => "SPACE", 0x21 => "PAGE UP", 0x22 => "PAGE DOWN", 0x23 => "END", 0x24 => "INICIO", 0x25 => "LEFT", 0x26 => "UP", 0x27 => "RIGHT", 0x28 => "DOWN", 0x29 => "SELECTION", 0x2A => "PRINT", 0x2C => "PRINT SCREEN", 0x2D => "INSERT", 0x2E => "DELETE", 0x30 => "0", 0x31 => "1", 0x32 => "2", 0x33 => "3", 0x34 => "4", 0x35 => "5", 0x36 => "6", 0x37 => "7", 0x38 => "8", 0x39 => "9", 0x41 => "A", 0x42 => "B", 0x43 => "C", 0x44 => "D", 0x45 => "E", 0x46 => "F", 0x47 => "G", 0x48 => "H", 0x49 => "I", 0x4A => "J", 0x4B => "K", 0x4C => "L", 0x4D => "M", 0x4E => "N", 0x4F => "O", 0x50 => "P", 0x51 => "Q", 0x52 => "R", 0x53 => "S", 0x54 => "T", 0x55 => "U", 0x56 => "V", 0x57 => "W", 0x58 => "X", 0x59 => "Y", 0x5A => "Z", 0x5B => "LEFT WIN.", 0x5C => "RIGHT WIN.", 0x5D => "APPLICATIONS", 0x60 => "NUM 0", 0x61 => "NUM 1", 0x62 => "NUM 2", 0x63 => "NUM 3", 0x64 => "NUM 4", 0x65 => "NUM 5", 0x66 => "NUM 6", 0x67 => "NUM 7", 0x68 => "NUM 8", 0x69 => "NUM 9", 0x6A => "MULTIPLY", 0x6B => "ADDITION", 0x6C => "SEPARATOR", 0x6D => "SUBSTRACT", 0x6E => "DECIMAL", 0x6F => "DIVIDITION", 0x70 => "F1", 0x71 => "F2", 0x72 => "F3", 0x73 => "F4", 0x74 => "F5", 0x75 => "F6", 0x76 => "F7", 0x77 => "F8", 0x78 => "F9", 0x79 => "F10", 0x7A => "F11", 0x7B => "F12", 0x90 => "BLOQ. NUM.", 0x91 => "BLOQ. SCROLL", 0xA0 => "LEFT SHIFT", 0xA1 => "RIGHT SHIFT", 0xA2 => "LEFT CONTROL", 0xA3 => "RIGHT CONTROL", 0xA4 => "LEFT ALT", 0xA5 => "RIGHT ALT", 0xBC => "SEP", 0xBD => "DASH", 0xBE => "DOTT" } GetKeyState = Win32API.new("user32", "GetAsyncKeyState", "i", "i") KeyRepeatCounter = {} module_function def self.update for key in KeyRepeatCounter.keys if (GetKeyState.call(key).abs & 0x8000 == 0x8000) KeyRepeatCounter[key] += 1 else KeyRepeatCounter.delete(key) end end end def self.press?(key) return false if key == nil return true unless KeyRepeatCounter[key].nil? return key_pressed?(key) end def self.trigger?(key) return false if key == nil count = KeyRepeatCounter[key] press = count.nil? ? key_pressed?(key) : false return (count == 0 or press) end def self.repeat?(key) return false if key == nil count = KeyRepeatCounter[key] return true if count == 0 if count.nil? return key_pressed?(key) else return (count >= 23 and (count - 23) % 6 == 0) end end def self.key_pressed?(key) if (GetKeyState.call(key).abs & 0x8000 == 0x8000) KeyRepeatCounter[key] = 0 return true end return false end def self.getKey for i in 0...256 if GetKeyState.call(i) == -32767 key = i break end end return if key == nil return Names[key] != nil ? key : 0 end end
Adaptado JS:
Código Javascript:
Ver original
Keyboard = { keyLog:[], setup:function () { document.onkeypress = function(event) { Keyboard.keyCode = event == null ? window.event.keyCode : event.keyCode } }, update:function () { for (var i in Keyboard.KeyLog) { if (Keyboard.keyCode == i) { Keyboard.keyLog[i]++ } else { Keyboard.keyLog[i] = undefined } } }, press: function (key) { if (key == undefined) return false; if (Keyboard.keyLog[key] != undefined) return true; return Keyboard.keyPressed(key) }, trigger:function (key) { if (key == undefined) return false; return (Keyboard.keyLog[key] == 0 || (Keyboard.keyLog[key] == undefined ? Keyboard.keyPressed(key) : false)) }, repeat:function (key) { if (key == undefined) return false; if (Keyboard.keyLog[key] == 0) return true; if (Keyboard.keyLog[key] == undefined) { return Keyboard.keyPressed(key) } else { return (Keyboard.keyLog[key] >= 23 && (Keyboard.keyLog[key] - 23) % 6 == 0) } }, keyPressed:function (key) { if (Keyboard.keyCode == key) { Keyboard.keyLog[key] = 0 return true } return false } }
Demo de su funcionamiento:
Código Javascript:
Ver original
Keyboard.setup(); setInterval(checkInput,1); function checkInput () { Keyboard.update(); if (Keyboard.trigger(13)) { alert('has precionado Enter') } }