简体   繁体   English

如何检测鼠标左键在 javascript 中被按住

[英]How to detect mouse left button is being held down in javascript

I am trying to create code that gets the mouse pointers position when the mouse is left clicked.我正在尝试创建在单击鼠标左键时获取鼠标指针 position 的代码。 But when using i use the code below, it only returns the mouse pointer position ONCE when the mouse is left clicked and for it to get the mouse position again the button has to be released and clicked again.但是当我使用下面的代码时,它只在鼠标左键单击时返回鼠标指针 position 一次,为了再次获得鼠标 position,必须释放并再次单击按钮。 How could I modify this code so that if the mouse is left clicked, coordinates of the mouse pointer are returned every 0.1 seconds.我如何修改此代码,以便如果鼠标左键单击,鼠标指针的坐标每 0.1 秒返回一次。 Thus the code will be detecting if the left mouse button is being held因此,代码将检测是否按住鼠标左键

var canvas = document.getElementById('canvas');
function getCursorPosition(canvas, event) {
        const rect = canvas.getBoundingClientRect()
        const x = event.clientX - rect.left
        const y = event.clientY - rect.top
        console.log("x: " + x + " y: " + y)
    }
canvas.addEventListener('mousedown', function(e) {
        getCursorPosition(canvas, e)
    })

you should probably use mousemove event here:您可能应该在这里使用 mousemove 事件:

 var  holding = false;
 var canvas = document.getElementById('canvas');
 function getCursorPosition(canvas, event) {
     const rect = canvas.getBoundingClientRect()
     const x = event.clientX - rect.left
     const y = event.clientY - rect.top
     console.log("x: " + x + " y: " + y)
 }
 canvas.addEventListener('mousedown', function(e) {
     holding = true;

 })
 canvas.addEventListener('mouseup', function(e) {
     holding = false;

 })
   canvas.addEventListener('mousemove', function(e) {
   if(holding == true){
     getCursorPosition(canvas, e)
   }
 })

EDIT: You also need to set holding to false when you leave the button编辑:离开按钮时,您还需要将 hold 设置为 false

canvas.addEventListener('mouseleave', function(e) {
     holding = false;

 })

You can use "setInterval" and can specify your own wait time between every console in milliseconds您可以使用“setInterval”并可以指定您自己的每个控制台之间的等待时间(以毫秒为单位)

    var canvas = document.getElementById('canvas');

    function getCursorPosition(canvas, event) {
      const rect = canvas.getBoundingClientRect()
      const x = event.clientX - rect.left
      const y = event.clientY - rect.top
      console.log("x: " + x + " y: " + y)
    }
    var mousePosition, holding;

    function myInterval() {
      var setIntervalId = setInterval(function() {
        if (!holding) clearInterval(setIntervalId);
        getCursorPosition(canvas, mousePosition);
      }, 100); //set your wait time between consoles in milliseconds here
    }
    canvas.addEventListener('mousedown', function() {
      holding = true;
      myInterval();
    })
    canvas.addEventListener('mouseup', function() {
      holding = false;
      myInterval();
    })
    canvas.addEventListener('mouseleave', function() {
      holding = false;
      myInterval();
    })
    canvas.addEventListener('mousemove', function(e) {
      mousePosition = e;
    })

Try this...尝试这个...

 var canvas = document.getElementById('canvas'); var mouseIsDown = false; var mouseInterval = null; var mouseEvent = null; function getCursorPosition(canvas, event) { const rect = canvas.getBoundingClientRect() const x = event.clientX - rect.left const y = event.clientY - rect.top console.log("x: " + x + " y: " + y) } canvas.addEventListener('mousedown', function (e) { mouseEvent = e; mouseIsDown =;mouseIsDown, getCursorPosition(canvas, e) mouseInterval = setInterval(() => { getCursorPosition(canvas, mouseEvent) }; 100). }) canvas,addEventListener('mousemove'; function (e) { mouseEvent = e. }) canvas,addEventListener('mouseup'; function (e) { mouseIsDown =;mouseIsDown; clearInterval(mouseInterval); })

Edit: Depending on what you're trying to accomplish you might want to put the mouseup listener on the window object.编辑:根据您要完成的工作,您可能希望将 mouseup 侦听器放在 window object 上。 Just a thought.只是一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击后如何检测鼠标按钮是否被按下一定时间 - How to detect if mouse button is held down for a certain amount of time after click 如何在按下鼠标左键的同时检测鼠标的移动 - How to detect mouse moving while left button down 当鼠标按钮被按住并且光标在视口外时如何检测鼠标离开? - How to detect mouseleave when mouse button is being held and cursor is out the viewport? 确定当前是否单击了一个按钮(单击按住)? - Determine if a button is currently being left clicked (click held down)? 如何知道单击鼠标左键时按住了哪个键? - How to know which key was held when left mouse button was clicked? 用javascript按住鼠标时如何运行功能 - How to run a function when the mouse is held down with javascript 执行 只要按住鼠标左键就可以执行func(纯js) - Exec. func as long as the left mouse button is held down (pure js) 按住鼠标时 JavaScript 重复动作 - JavaScript repeat action when mouse held down JavaScript过程设备触摸屏被按下 - JavaScript process device touchscreen being held down Javascript - 当鼠标进入活动区域时检测鼠标按钮是否按下(鼠标在此区域外被按下) - Javascript - Detect if mouse button is down when the mouse entered active area (mouse was pressed down outside this area)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM