-5

I have a babylonjs 3D scene where certain keys, when pressed, cause the camera to navigate forwards/backwards, etc. For example pressing T moves the viewer forward.

I am trying to generate an effect where the T key thinks it has been pressed whenever a mousedown occurs anywhere in the window. In short I need mousedown to move the camera forward.

 document.addEventListener("mousedown", function(e) {
   var code = 116;//T
   $('document').trigger(
     jQuery.Event('keypress', {
       keyCode: code,
       which: code
     })
   );
   $("#show").append("mousedown creates: " + code + "<BR>");
 });

 document.addEventListener("keypress", function(e) {
   var key = e.which;
   //If it's the T key
   if (key == 116) {
     $("#show").append("T key has been pressed<BR>");
   }
 });

 // I would like the mousedown event to fire the keypress event 
 // as if the T key has been pressed

The fiddle is here

user2016210
  • 119
  • 4
  • 13
  • 5
    Don't try to get around posting code by wrapping a link in code block. Questions are supposed to be self contained. We shouldn't have to copy/paste a url and go off site to review your issue. You want help, don't make it difficult for those that might be able to help. Only use off site demos to support what is actually in the question itself – charlietfl Oct 13 '16 at 16:53
  • Just making the link work is not what was expected. Post your code here!! – charlietfl Oct 13 '16 at 16:57

1 Answers1

0

This gets somewhat closer as the keypress event seems to occur. However the keycode doesn't come through. The output says "key: undefined."

 function fire(type, options) {
   var event = new CustomEvent(type);
   for (var p in options) {
    event[p] = options[p];
   }
   this.dispatchEvent(event);
 }

 window.addEventListener("mousedown", function(e) {
   fire("keypress", {
    keyCode: 116,
    bubbles: true
   })
 });

 window.addEventListener("keypress", function(e) {
   var key = e.which;
   $("#show").append("key: " + key + "<BR>");
   if (key == 116) {
    $("#show").append("T key was pressed<BR>");
   }
 });

The fiddle is here

user2016210
  • 119
  • 4
  • 13