I apologize in advance for using AI translation.
Currently, I am implementing a system to move meshes by dragging them. However, I am facing an issue where the behavior changes between dragging with the left mouse button and the right mouse button. As a countermeasure, I want to completely disable right-click input across the entire page (the right button will never be used!).
I tried using window.addEventListener()
to set a flag when the right mouse button is clicked, so that no further actions occur. However, this approach is being ignored, and the right button is still being detected. When I logged the event.target.mouseButtons
property to the console, both left-click and right-click are recognized as {LEFT: 2, MIDDLE: 2, RIGHT: 0}
.
What steps are necessary to correctly distinguish between left and right mouse buttons in this case?
Are you using TransformControls or something home-brewn?
Thank you for your reply.
I searched for TransformControls
in VSCode, but it didn’t show up, so it seems it is not being used.
I apologize for asking this, but if there’s any code you’d like to see, please let me know.
I’m having a hard time imagining how you’re “implementing” something if you can’t even tell which part of your code is doing the actual dragging.
Thank you for your response.
I am sending the specific part of the code that moves the meshes.
I apologize for my lack of understanding regarding the process of moving the meshes.
// Set up the camera controls
const orbitControls = new OrbitControls(camera, renderer.domElement);
// Initial drag position
let startDragPos = new THREE.Vector3();
// Set up drag controls
let oldMousePosition = new THREE.Vector3();
let startmeshPosition = new Array<THREE.Vector3>();
const controls = new DragControls(meshs, camera, renderer.domElement);
controls.addEventListener("dragstart", function (event) {
// Disable camera controls when dragging starts
orbitControls.enabled = false;
// Store the initial position of the dragged object
startDragPos.copy(event.object.position);
//console.log(event.target);
// Store the mouse position from the previous frame
const currentWorldPosition = new THREE.Vector3();
event.object.getWorldPosition(currentWorldPosition);
oldMousePosition.copy(currentWorldPosition);
for (const mesh of meshs) {
startmeshPosition.push(mesh.position);
}
});
function hasMoved(
oldPosition: THREE.Vector3,
newPosition: THREE.Vector3,
) {
const distance = oldPosition.distanceTo(newPosition);
return distance > 0.01; // Return true if the distance exceeds the threshold
}
controls.addEventListener("drag", function (event) {
const currentWorldPosition = new THREE.Vector3();
event.object.getWorldPosition(currentWorldPosition);
if (hasMoved(oldMousePosition, currentWorldPosition)) {
for (const mesh of meshs) {
const newWorldPosition = currentWorldPosition.clone();
mesh.parent?.worldToLocal(newWorldPosition);
mesh.position.copy(newWorldPosition);
}
oldMousePosition.copy(currentWorldPosition);
}
});
OK, so you are using DragControls.
DragControls inherits from its base class Controls and as such has a property .mouseButtons .
The DragControls.js source initializes its .mouseButtons property as follows:
this.mouseButtons = { LEFT: MOUSE.PAN, MIDDLE: MOUSE.PAN, RIGHT: MOUSE.ROTATE };
Try overwriting your DragControls’ .mouseButtons property with the following:
const controls = new DragControls(meshs, camera, renderer.domElement);
controls.mouseButtons = { LEFT: MOUSE.PAN, MIDDLE: MOUSE.PAN, RIGHT: null };
1 Like
I successfully managed to stop it from functioning! Thank you so much.
For my future reference, may I ask if this was because the behavior was restricted by changing the value associated with this enum? I tried commenting out the RIGHT
in this enum, but it didn’t seem to have any effect. Is it necessary to change the value instead?
If you don’t explicitely overwrite a property, it retains the value it was initialized with.
Thank you for taking the time to reply.
I see, in that case, commenting it out wouldn’t have any effect.
Thank you very much!