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

6

My intention in the end is to transform a 'mousedown' into a 'ctrl + mousedown'. But I can only activate 'ctrl' after the mousedown has been processed.

Example:

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

From this it activates the ctrlKey after the mousedown. But I want you to activate earlier. How to do this? I'm now learning about these JS methods (preventDefault, addEventListener) and I do not know how to work right with them, but I believe that with some of them I can match what I want.

    
asked by anonymous 13.03.2014 / 22:42

2 answers

4

Update

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

Assuming you want to allow the selection of multiple options without the user using the ctrl key, you can achieve this in the following way:

JSFiddle Demo

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 from @Vedmant on SOEN dated 16 Jan 2013.

Original Response

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

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

Your example:

event.ctrlKey = true;

Do not do anything rigorously, it does not really impact the ctrl key or the event associated with its use.

Although it had some effect, it would not be easy to make this a viable solution, since in MAC for example, the cmd key is used.

See this answer from @Juhana on SOEN dated Feb 22, 2012

Solution

Dropping your initial approach to its unfeasibility, you can be aware of the use of this key in conjunction with the mouse click.

For this you apply the click event to your element and verify that you have the ctrl key clicked too:

JSFiddle Demo

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

See this answer from @Nick Craver ♦ in SOEN dated 21 Mar 2010.     

13.03.2014 / 23:36
0

As far as I know, changing the values passed in the variable that identifies the event has no real effect, so it does not make sense to change these values expecting the browser to have a different reaction than expected.

EDIT

Once you have identified your intention to select multiple elements of select multiple without the ctrl key, I found the following solution:

Multi select avoiding ctrl button, provided by pure select tag and jQuery

The solution can be found in the jsfiddle ... and as jsfiddle does not work in older versions of IE , here is the embedded jsfiddle , which I tested in IE 8 and worked.

    
13.03.2014 / 22:46