Jump to content

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
     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>


Link to comment
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.

Link to comment
Share on other sites

38 minutes ago, hfeist said:

looks daunting!

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


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



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

Link to comment
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) {
window.addEventListener("keyup", function(e) {
  var keycode = e.which;
   if (e.keyCode == 16) {

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

 speed = camera._computeLocalCameraSpeed()/4;

Here it is in action:


Link to comment
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.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...