javascript - Replicate keyboard event with html button -


i have tried few ways make happen. have javascript pinball game uses keyboard keys controls. converting application touch screen display. workaround, hoping overlay buttons when clicked simulate keyboard strokes. have done research , sounds should work. using jquery v3.2.1

here things have tried far...

html element:

<input type="button" class="coil"></input>  $('.coil').trigger({type: 'keydown', which: 40, keycode: 40}); 

with this, not errors, nothing happens.

    var coilkey = $('body').keydown(function(e) {       switch (e.keycode) {       case 40:       break;}     })      $('.coil').click(coilkey); 

this 1 returns error in console...

((jquery.event.special[handleobj.origtype] || (intermediate value)).handle || handleobj.handler).apply not function

below javascript game engine handles key listeners...

addkeylistener: function (keyandlistener) {   this.keylisteners.push(keyandlistener); },  // given key, return associated listener.  findkeylistener: function (key) {   var listener = undefined;    for(var i=0; < this.keylisteners.length; ++i) {      var keyandlistener = this.keylisteners[i],          currentkey = keyandlistener.key;      if (currentkey === key) {         listener = keyandlistener.listener;      }   };   return listener;  },   // method call key down , key press  // events.   keypressed: function (e) {   var listener = undefined,       key = undefined;    switch (e.keycode) {      // add more keys needed       case 32: key = 'space';        break;      case 68: key = 'd';            break;      case 75: key = 'k';            break;      case 83: key = 's';            break;      case 80: key = 'p';            break;      case 37: key = 'left arrow';   break;      case 39: key = 'right arrow';  break;      case 38: key = 'up arrow';     break;      case 40: key = 'down arrow';   break;   }    listener = this.findkeylistener(key);   if (listener) { // listener function      listener();  // invoke listener function   }  }, 

and here javascript triggered listeners...

lastkeylistenertime = 0,  // throttling arrow keys  game.addkeylistener( {   key: 'k',   listener: function () {      if ( !launching && !gameover) {         rightflipperrisetimer.start();         rightflipperangle = 0;         game.playsound('flipper');        }     }   } );  game.addkeylistener(  {   key: 'd',   listener: function () {      if ( !launching && !gameover) {         leftflipperrisetimer.start();         leftflipperangle = 0;         game.playsound('flipper');       }   }  } );  game.addkeylistener( {   key: 'p',   listener: function () {      togglepaused();   } } );  game.addkeylistener(  {   key: 'up arrow',   listener: function () {      var now;       if (!launching || launchstep === 1)         return;       = +new date();      if (now - lastkeylistenertime > 80) { // throttle         lastkeylistenertime = now;         launchstep--;         actuatorsprite.painter.image = launchimages[launchstep-1];          ballsprite.top = ball_launch_top + (launchstep-1) * 9;         adjustactuatorplatformshape();      }   } } );  game.addkeylistener( {   key: 'down arrow',   listener: function () {      var now;       if (!launching || launchstep === launch_steps)         return;       = +new date();      if (now - lastkeylistenertime > 80) { // throttle         lastkeylistenertime = now;         launchstep++;         actuatorsprite.painter.image = launchimages[launchstep-1];          ballsprite.top = ball_launch_top + (launchstep-1) * 9;         adjustactuatorplatformshape();      }   }  } ); 

thanks everyone, found way make work event listeners in javascript rather jquery...

      document.getelementbyid("coil").addeventlistener("click", function () {       if (!launching || launchstep === launch_steps)         return;         launchstep++;         actuatorsprite.painter.image = launchimages[launchstep-1];          ballsprite.top = ball_launch_top + (launchstep-1) * 9;         adjustactuatorplatformshape();      }); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Web audio api 5.1 surround example not working in firefox -