Sign in to follow this  
hfeist

Is it possible to make a navigation key think it has been pressed when a mousedown occurs?

Recommended Posts

I'm trying to find a way to use the mousedown event to move the camera forward. I've mapped the keys so that T moves forward and want it to fire whenever there is a mousedown event.  I have come up with the following but cannot get the keypress function to fire when there is a mousedown.

Any suggestions would be most welcome.

In the scene setup I've set the forward key to:

camera.keysUp = [84]; // T

After the scene setup I've added:

 document.addEventListener("mousedown", function(e) {
   var code = 84;//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;
   $("#show").append("key code:+key+"<BR>");
   //If it's the T key
   if (key == 84) {
     $("#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

there is a div in the body to display the output:

<div id="show"></div>

 

Share this post


Link to post
Share on other sites

thanks for the pointers. certainly looks daunting!

but i've added it to my project and can see the camera inch forward ever so slightly at each mouseclick. would much prefer to keep moving as long as the mouse is down. and perhaps move backward if the shift key is down. and i can see no mention of mouseup or mousedown so guess it's an entirely different way of doing things.

Share this post


Link to post
Share on other sites
38 minutes ago, hfeist said:

looks daunting!

All I did was copy/rename FreeCameraMouseInput and strip out unneeded code.

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freecamera.input.mouse.ts

and then found the code that moved the camera forward in the keyboard input:

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freecamera.input.keyboard.ts#L90

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/Inputs/babylon.freecamera.input.keyboard.ts#L101

and moved it to the pointerdown section of the new camera input.

Share this post


Link to post
Share on other sites

thanks for going to so much trouble.

i'm trying to understand it well enough to find where i might create a loop that keeps you moving as long as the mouse is down just as it does as long as a nav key is held down

Share this post


Link to post
Share on other sites

Hey Adam--thanks for that.

I've added the ability to move backwards by holding shift down

window.addEventListener("keydown", function(e) {
  var keycode = e.which;
   if (e.keyCode == 16) {
        speed=-1*speed;;
    }
});
window.addEventListener("keyup", function(e) {
  var keycode = e.which;
   if (e.keyCode == 16) {
        speed=-1*speed;;
    }
});

Just needed to set a flag so the speed gets locked down when the script first runs:

if(flag==false){
 flag=true;
 speed = camera._computeLocalCameraSpeed()/4;
}

Here it is in action:  http://64.78.15.229/eyemap3Djs/walk.html

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.