The keydown event is not functioning within the threejs canvas when OrbitControls are active

I am currently developing a threejs demo that contains several objects. One of my objectives is to create a form element positioned above the canvas, allowing users to input text.


Background

My approach involves dynamically generating a form using the function below, which appears to be functioning correctly:

function createInput() {
    var form = document.createElement('form');
    form.className = 'form-inline';
    form.style.zIndex = 100;
    form.style.position = 'absolute';
    form.style.top = '0px';
    form.style.left = '0px';
    var div = document.createElement('div');
    div.className = 'form-group';
    form.appendChild(div);
    var input = document.createElement('input');
    input.id = "newId";
    input.type = 'email';
    input.className = 'form-control form-control-sm';
    input.placeholder = 'Email..'
    input.name = 'email';
    var button = document.createElement('button');
    button.id = "submitLabel";
    button.type = 'button';
    button.className = 'btn btn-primary btn-sm';
    button.innerHTML = "Put";
    var trashButton = document.createElement('button');
    trashButton.id = "trashLabel";
    trashButton.type = 'button';
    trashButton.className = 'btn btn-danger btn-sm';
    trashButton.innerHTML = "Del";
    div.appendChild(input);
    form.appendChild(button);
    form.appendChild(trashButton);
}

Please note that I am utilizing OrbitControls in my canvas. The HTML structure consists of two separate sections: a search panel and a canvas element loaded within an iframe.

My orbit controls are instantiated as

controls = new THREE.OrbitControls(camera, container);
where the container refers to renderer.domElement - the canvas element.


Issue

Unfortunately, the input field in the form does not register key inputs. Although the elements, input and button, respond to clicks, the input does not recognize any keystrokes.

Upon inspecting the Event Listeners, I found that keydown is attached to the window.

After removing the specific keydown event in the console debugger, the form started functioning properly without any issues.


Progress So Far

I have extensively researched similar issues on Stack Overflow and GitHub, including a related discussion at https://github.com/mrdoob/three.js/issues/4327. However, I still believe there may be a fundamental issue at play here. While I can remove the default keydown event attached to the window, I consider this more of a temporary fix.

If anyone has insights to share on this matter, I would greatly appreciate it. Feel free to request any additional code if needed.

Answer №1

Have you experimented with disabling the OrbitControls keys by setting OrbitControls.enableKeys = false?

If this option successfully enables event propagation, you may consider implementing an onblur / onfocus event on your input to toggle the OrbitControls.enableKeys setting.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The addClass function in jQuery does not seem to be functioning properly when used within

I've run into an issue with my jQuery addClass function in a loop. It's supposed to turn my Font Awesome star golden, but for some reason, it's not working as expected. The loop itself is functioning properly - I double-checked this by using ...

Error: Unable to locate module './clock' in the directory '/home/n/MyWork/my-app/src'

Upon attempting to start up the project, I encountered an error message stating: "Module not found: Can't resolve './clock' in '/home/n/MyWork/my-app/src'" The structure of the project is as follows: My-app --node-modules --public ...

Ajax: Failed to send POST request (404)

After adding a POST script in the manage.ejs file and console logging the data to confirm its functionality, I encountered an issue. Below is the code snippet: <script type="text/javascript"> var guildID = "<%= guild.id %>"; let data = {so ...

Sort products by the subcategory in Firebase

I am currently facing a challenge in filtering products based on their sub child nodes within Firebase. This is how my data is structured: products/ product1 /author: 12345 /title: "Awesome" /description: "more awesome" ...

AngularJS: The requested resource does not have the "Access-Control-Allow-Origin" header

Currently developing my web application using AngularJS, I have a file named script.js which contains the following code: var module = angular.module('project', ['ngRoute']); // setting up our routes module.config(function ($r ...

Discover the correct steps to transition from using particles.js to the react-tsparticles package

Migrating from react-particles-js to react-tsparticles-js Hi there! I'm currently facing an issue with my ReactJS website here, which is deployed using Netlify. The error code below keeps popping up, and I believe it's related to the transition ...

React date format error: RangeError - Time value is invalid

I'm utilizing a date in my React app using the following code: const messageDate = new Date(Date.parse(createdDate)) const options = { month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' } as const ...

Having trouble with the clear button for text input in Javascript when using Bootstrap and adding custom CSS. Any suggestions on how to fix

My code was working perfectly until I decided to add some CSS to it. You can view the code snippet by clicking on this link (I couldn't include it here due to issues with the code editor): View Gist code snippet here The code is based on Bootstrap. ...

RxJS - Only emit if another source does not emit within a specified time frame

Imagine having two observables. Whenever the first one emits, there should be a 2-second pause to check if the other observable emits something within that timeframe. If it does, then no emission should occur. However, if it doesn't emit anything, the ...

Vuejs Error: The 'in' operator cannot be used for searching

I am facing an issue with my form and VueJS. Upon clicking the "Login" button, I intend to change the text on the button. However, instead of achieving this, I encounter an error stating 'cannot use 'in''. Below is the HTML code snippet ...

Is the setInterval function in JavaScript only active when the browser is not being used?

I am looking for a way to ensure proper logout when the browser is inactive using the setInterval() function. Currently, setInterval stops counting when the browser is active, but resumes counting when the browser is idle. Is there a way to make setInterv ...

Notification within the conditional statement in React JS

I am working on validating phone number input within a React JS component using an if/else statement. If the user enters letters instead of numbers, I want to display a message saying "please check phone number". While I have been able to create a function ...

Steps for eliminating an item from an array in MongoDB

I have been struggling with the functionality of the Mongoose library, specifically the remove method. On my webpage, I display comments and have a form with a Delete button. My objective is to delete only the comment that was clicked. Below is an excerpt ...

The Vue Watch feature fails to trigger when incorporating axios

Hello everyone, I am facing an issue with my code that involves fetching data from a database using axios. The problem is that the watch function is not triggering in the .then() block. Below is the snippet of code I am currently working with. Thank you ...

What is the best way to show an array within a string using javascript?

Take a look at this code snippet : const names = ["Alex", "John", "Kit", "Lenny"]; for(let i = 0; i < 4; i++) { $("body").append("<p>" + names[i] + "</p>'); }; Can you figure out how to dynamically add each item in the 'names&a ...

How to execute a JavaScript function within a Jinja for loop

I am currently working on an HTML page where the variable schedule contains a series of sequential decimal numbers representing seconds. My goal is to develop a function in JavaScript/jQuery that can convert these decimal numbers into time format. However ...

Tips for effectively loading data of a specific user or event into a bootstrap modal in Laravel using AJAX

I'm having some trouble loading Event data into my bootstrap modal in Laravel. This is all new to me as I'm just getting started with AJAX. Can someone please take a look at what I've done so far? Modal Template <div class="modal fa ...

Instructions on how to insert a new row into Datatables following the successful submission of a form via AJAX请求

Could someone please provide guidance on how to insert a new row into an existing datatable after a successful AJAX request? Here is the code I am currently using: $(document).ready(()=>{ $.ajax({ url: 'http://localhost:3000/api/post/getUserDat ...

Challenges with jQuery: Appending strings to a dynamically selected element

Hello everyone, I have a question regarding adding the string 'url(" ")' around my Any assistance you can provide would be greatly appreciated. Thank you! $(function() { $('#list li a').hover( function(){ $("#meow").css( " ...

Looking to include a data-* attribute within a div element for the utilization of a third-party JavaScript library like React or Next.js?

let speed = '{ "speed": 0.2 }'; <div className="section jarallax h-100vh" data-jarallax={speed} style={{backgroundImage: "url('/images/header-bg.jpg')"}} id="home"> </div> <Script src="./js/parallax.js" strate ...