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.
What do you really want to do? I don’t think it’s possible to simulate the key
ctrl
thus.– Miguel Angelo
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/
– Joao Paulo
if you do not use jquery and put the 3 parameter
addEventlistener
astrue
(useCapture) it will run before the event you want to reach, but this will only work in modern browsers with support foruseCapture
– Gabriel Gartz