JQuery to detect both ctrlKey and mouse click

jquery trigger ctrl + click
simulate ctrl + click javascript
javascript ctrl key combination
jquery trigger ctrl + s
jquery detect ctrl + click
how to disable ctrl key using jquery
mouse event control key
event.ctrlkey mac

I'm new to JQuery, so I don't know much of the logic. I'm using it to find out which index of textarea I clicked while holding the CtrlKey. However how do I assign a function on a combination of onclick and a keyboard event.

//What I have tried:
//textarea is the object where I want to detect both ctrlKey and mouse click
$(textarea).keydown(function(event){
    if(event.ctrlKey){
        $(textarea).click(function(event){
            console.log("catched")
        })
    }
})

The above method does work, however It does so thrice, i.e.the console.log occurs thrice, so is there a way to make this catch it once. also it somehow also occurs when not pressing the ctrl key.

A couple of small mistakes here, but you had the right idea :)

First off, it doesn't really make sense to stack the event handlers the way you have done - I get what you're thinking logically but in reality JS doesn't work that way. What you've actually said is this:

"If the user presses a key down in this textarea, and if their control key is down, add an event listener to this textarea that detects for clicks, and logs catched to the console".

What you really want is this:

$("#txtarea").click((e)=>{
  if (e.ctrlKey) {
    console.log("Control + Click!");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<textarea id="txtarea"></textarea>

[jQuery] Detecting Ctrl + click, Hi, How could I detect someone holding down the Ctrl key and then clicking the mouse button on something? Similar holding down the Shift key and clicking the mouse button. - Dave Thanks both for your input. I guess� In jQuery, using jq event wrapper, you need to use event.originalEvent.location. Then the logic to implement on click isn't that hard i guess, just set some flag/object on keydown/keyup events and check it on click. Issue could be to handle altGr key which can give inconsistent result. If i have time, i'll make an answer, later

You can simply check the ctrlKey property of the mouse event:

$(function() {
  $('textarea').on('click', function (e) {
    if (e.ctrlKey) {
      console.log('clicked with ctrl');
    } else {
      console.log('clicked without ctrl');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea>click me</textarea>

MouseEvent ctrlKey Property, Find out whether or not the "CTRL" key was pressed when a mouse button is clicked: if (event.ctrlKey) { alert("The CTRL key was pressed!"); } else { alert("The � Click the button below and you’ll see the events. Try double-click too. On the teststand below all mouse events are logged, and if there is more than a 1 second delay between them they are separated by a horizontal ruler. Also we can see the button property that allows to detect the mouse button, it’s explained below.

I'd recommend setting up a global variable which holds the status of your ctrl key.

var ctrlDown=false;

Instead of simply listening for a keydown event, listen for a keyup event as well and update ctrldown accordingly.

$(textarea).keydown(function(event) {
  if (event.ctrlKey) {
    ctrlDown = true;
  }
});
$(textarea).keyup(function(event) {
  if (event.ctrlKey) {
    ctrlDown = false;
  }
});

Now that you know that the ctrl key is actually pressed you can do a simple check like:

$(textarea).click(function(event) {
  if (ctrlDown) {
    console.log("catched")
  }
});

Keyboard Shortcuts, View menu, Alt + V, Ctrl + F2 + V. Select all text, Ctrl + A, Cmd + A. Copy text, Ctrl + C, Cmd + C. Find text, Ctrl + F, Cmd + F. Find and replace text, Ctrl + H, Cmd +� How to distinguish between left and right mouse click with jQuery (11) How do you obtain the clicked mouse button using jQuery? $ ('div'). bind ('click', function (){alert ('clicked');}); this is triggered by both right and left click, what is the way of being able to catch right mouse click?

Javascript, HTML � CSS � JavaScript � PHP � JQuery The mouseEvent ctrlKey property is used to define whether the ctrl key is pressed or not. It is a boolean value. When ctrl key is pressed then on click of the mouse buttons it returns true and if it is article if you find anything incorrect by clicking on the "Improve Article" button below. I thought I would draw your attention that in the specific context where a listener was defined within a jQuery plugin, then the only thing that successfully simulated the keypress event for me, eventually caught by that listener, was to use setTimeout(). e.g. setTimeout(function() { $("#txtName").keypress() } , 1000);

jQuery, So, Using keydown() method we can detect if any key is on its way down. Syntax: After pressing run button- keydown keyCode and event.which will return a UNICODE value of the pressed key, Both are browser specific. Phases of JavaScript Event � How to insert text into the textarea at the current cursor position ? For example, "click.myPlugin.simple" defines both the myPlugin and simple namespaces for this particular click event. A click event handler attached via that string could be removed with .off("click.myPlugin") or .off("click.simple") without disturbing other click handlers attached to the elements. Namespaces are similar to CSS classes in that

Mouse events, Also we can see the button property that allows to detect the mouse button, it's explained below. For instance, the button below only works on Alt+Shift +click: < button id = " button It prevents both these selections: Before. Definition and Usage. The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.

Comments
  • Please share some code, what you tried, etc.
  • sorry about that
  • Thanks this works, but I really wish we could nest it within each other(not in the way I did but something like that which works)
  • This works just as well when I'm going to use the letter keys, thanks.