开发者

Continuous mouse click event

Is there any event generated by continuous mouse click i.e., not releasing the mouse button 1? If no, please let me kn开发者_运维知识库ow.


The mousedown event is triggered when the mouse button is pressed down. 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.

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. Here is an example (using 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.


There is a JQuery plugin: LongClick

Longclick is press & hold mouse button "long click" special event for 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) :

What I did to solve this is the following:

First, create a onmousedown() event that sets a global variable to 1 when triggered.

Second, create a onmouseup() event that sets that global variable to 0 when triggered.

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.

example on how to use 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.

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. The sequence of these events is: 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.


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). Can't be certain that $(':focus')[0] === undefined will work for every situation, but it can be customized.

// 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');
        }

     }
  }
}


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();
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜