How to perform an action before 'mousedown' in javascript/jquery?

Asked

Viewed 234 times

6

My intention after all is to turn a 'mousedown' into a "Ctrl + mousedown". But I can only activate Ctrl after mousedown is processed.

Example:

$(document).ready(function(){
    $('#CC option').on('mousedown', function(event){ 
        event.ctrlKey = true;
    }); 
});

Since it activates ctrlKey after mousedown. But I want it to activate first. How to do this? I am now knowing these JS methods (preventDefault, addeventlistener) and do not know how to work properly with them, but I believe that with some of them I can sew what I want.

  • 1

    What do you really want to do? I don’t think it’s possible to simulate the key ctrl thus.

  • My final intention is to use this in a select Multiple to select several in the mouse click(mousedown). But IE does not identify events in options. As you can see in my fiddle: http://jsfiddle.net/fp4WD/5/

  • if you do not use jquery and put the 3 parameter addEventlistener as true (useCapture) it will run before the event you want to reach, but this will only work in modern browsers with support for useCapture

2 answers

4


Updating

After reading your comment and rereading your question things already make another sense.

Assuming you want to allow multiple options to be selected without the user using the key Ctrl, you can reach that end as follows:

Demonstration at Jsfiddle

var multiSelect = {};
function init() {      
  var s = document.getElementsByTagName('select');
  for (var i = 0; i < s.length; i++) {
    if (s[i].multiple) {
      var n = s[i].name;
      multiSelect[n] = [];
      for (var j = 0; j < s[i].options.length; j++) {
        multiSelect[n][j] = s[i].options[j].selected;
      }
      s[i].onclick = changeMultiSelect;
    }
  }
}
function changeMultiSelect() {
  var n = this.name;
  for (var i=0; i < this.options.length; i++) {
    if (this.options[i].selected) {
      multiSelect[n][i] = !multiSelect[n][i];
    }
    this.options[i].selected = multiSelect[n][i];
  }
}
window.onload = init;

See this answer by @Vedmant on SOEN dated 16 Jan 2013.


Original Response

The original answer was given on the understanding that it was intended to detect that key Ctrl was in use when there was a click with the mouse.

There is no way to simulate the use of a certain key without the user actually using it.

This example of yours:

event.ctrlKey = true;

Does not strictly anything, does not have a real impact on the key Ctrl nor in the event associated with its use.

Even if some effect was present, it would not be easy to make it a viable solution, because on MAC for example, the key is used cmd.

See this answer by @Juhana at SOEN dated 22 Feb 2012.

Solution

By dropping your initial approach for its impracticability, you may be aware of the use of this key in conjunction with the mouse click.

For this you apply the event of click to your widget and check if you have the key Ctrl clicked also:

Demonstration at Jsfiddle

$(selector).click(function(e) {
  if(e.ctrlKey) {
    //Ctrl+Click fazer algo
  }
});

See this answer by @Nick Craver on SOEN dated 21 Mar 2010.

0

From what I know, changing the values passed in the variable that identifies the event has no real effect, so it makes no sense to change these values hoping that the browser has a different reaction than expected.

EDIT

Once identified its intention to select multiple elements of the select multiple without the key Ctrl, I found the following solution:

Multi select avoiding Ctrl button, provided by Pure select tag and jQuery

The solution can be found in jsfiddle... and since jsfiddle does not work in older versions of IE, here is the version Embedded do jsfiddle, which I tested on IE 8 and worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.