简体   繁体   English

连续鼠标点击事件

[英]Continuous mouse click event

Is there any event generated by continuous mouse click ie, not releasing the mouse button 1?是否有任何由连续鼠标单击产生的事件,即不释放鼠标按钮 1? If no, please let me know.如果没有,请告诉我。

The mousedown event is triggered when the mouse button is pressed down.当鼠标按钮被按下时触发mousedown事件。 If you are looking for an event that fires repeatedly, while the button is held down, you are out of luck, but you can use the mousedown event to repeatedly perform an action, and stop when the mouseup event is triggered.如果您正在寻找一个重复触发的事件,而在按住按钮时,您很不走运,但是您可以使用mousedown事件重复执行一个动作,并在触发mouseup事件时停止。

For example, you could use the setInterval function to repeatedly call a function while the mouse button is down, and then use clearInterval to stop when the mouse button is released.例如,您可以使用setInterval function 在鼠标按钮按下时重复调用 function,然后在释放鼠标按钮时使用clearInterval停止。 Here is an example (using jQuery):这是一个示例(使用 jQuery):

var interval;
$("#elementToClick").mousedown(function() {
    interval = setInterval(performWhileMouseDown, 100);
}).mouseup(function() {
    clearInterval(interval);  
});
function performWhileMouseDown() {
    $("#output").append("<p>Mouse down</p>");
}

You can see this running in this example fiddle .您可以在这个示例 fiddle中看到它正在运行。

There is a JQuery plugin: LongClick有一个JQuery插件:LongClick

Longclick is press & hold mouse button "long click" special event for jQuery 1.4.x. Longclick 是 jQuery 1.4.x 的按住鼠标按钮“长按”特殊事件。

The event is triggered when the mouse button stays pressed for a (configurable) number of seconds, while the pointer is stationery.当鼠标按钮保持按下(可配置的)秒数时触发该事件,而指针是静止的。

Yes, you can do this using onmousemove= movefunction(event) :是的,您可以使用onmousemove= movefunction(event)来做到这一点:

What I did to solve this is the following:我为解决此问题所做的工作如下:

First, create a onmousedown() event that sets a global variable to 1 when triggered.首先,创建一个onmousedown()事件,在触发时将全局变量设置为1

Second, create a onmouseup() event that sets that global variable to 0 when triggered.其次,创建一个onmouseup()事件,在触发时将该全局变量设置为0

Then, use the onmousemove() event to trigger in the div where I want the mouse down behavior to occur but only if the global variable we set earlier is set to 1 .然后,使用onmousemove()事件在我希望发生鼠标按下行为的 div 中触发,但前提是我们之前设置的全局变量设置为1

example on how to use onmousemove() : http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousemove关于如何使用onmousemove()的示例: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousemove

Done.完毕。

There is not such event.没有这样的事件。

What you might implement to achieve this is a function that evaluates the time elapsed between the (first) mouse click ond the following mouse release.为了实现这一点,您可能实施的是 function,它评估(第一次)鼠标单击和随后的鼠标释放之间经过的时间。

Given a predefined range you can estabilish how long should the button be clicked before being considered valid in your logic.给定一个预定义的范围,您可以确定在您的逻辑中认为该按钮有效之前应单击该按钮多长时间。

According to the spec ,根据规范

A click is defined as a mousedown and mouseup over the same screen location.单击定义为在同一屏幕位置上的 mousedown 和 mouseup。 The sequence of these events is: mousedown, mouseup, click这些事件的顺序是:mousedown、mouseup、click

So no, there isn't a "continuous click", because a click is a descrete event resulting from a sequence of actions.所以不,没有“连续点击”,因为点击是由一系列动作产生的离散事件。

What you probably want to do, is receive mousedown, set a timer, and if neither mouseup or mousemove occur within some time, invoke some behaviour.您可能想要做的是接收 mousedown,设置一个计时器,如果在一段时间内没有 mouseup 或 mousemove 发生,则调用一些行为。

import java.awt.Robot;
import java.awt.event.*;
public class App {
    private static final int key = InputEvent.BUTTON1_DOWN_MASK;
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        Robot robot;
        while (1==1) {
            try {
                robot = new Robot();
                robot.mousePress(key);
                robot.mouseRelease(key);
                // robot.mouseMove(x, y);// x,y are cordinates
                // Simulate a mouse click
                robot.mousePress(key);
                robot.mouseRelease(key);
                Thread.sleep(3000);
                // Simulate a key board press
                // robot.keyPress(KeyEvent.VK_A);
                // robot.keyRelease(KeyEvent.VK_A);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

There's a function I've been using to determine if an object is being dragged (if for some reason you cannot use the regular on drag event).我一直在使用 function 来确定是否正在拖动 object(如果由于某种原因您不能使用常规的拖动事件)。 Can't be certain that $(':focus')[0] === undefined will work for every situation, but it can be customized.不能确定 $(':focus')[0] === undefined 是否适用于所有情况,但可以自定义。

// this function will set up a mouse drag event and also check if something is being dragged
function customOnDrag(selector) {
   var dragInProgress = false;
   let mouseDrag = false;
   let mouseDown = false;
   $(selector).on('mousedown', function(event) {
       mouseDrag = false;
       mouseDown = true;
       interval = setInterval(checkIfDraggingAnObject, 20, event); // set to check every 20 ms
   }
   ).on('mousemove', function(event) {
       if ( mouseDown ){
           mouseDrag = true;
       }
   }
   ).on('mouseup', function(event) { 
       checkIfDraggingAnObject(event);
       clearInterval(interval);
       mouseDrag = false;
       mouseDown = false;
     }
  ); 
  // function to check if an object is being dregged:
  function checkIfDraggingAnObject(event){
     if ( event.type === 'mousedown' ){
        if ( $(':focus')[0] === undefined || mouseDrag === false ){
           // not dragging an object
           dragInProgress = false;   
        }else{
           // dragging an object
           dragInProgress = true;
           console.log('dragging: ');
           console.log($(':focus'));  // the object being dragged
        };
     }else if ( event.type === 'mouseup'  ) {
        if ( dragInProgress ){
           // dropped the object
           console.log('dropped: ');
           console.log($(':focus')); // the dropped object
           dragInProgress = false;
        }else if ( mouseDrag ) {
           // dragged the mouse, but no object
           console.log('did not drag an object');
        }else{
           // did not drag the mouse
           console.log('did not drag the mouse');
        }

     }
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM