Reputation: 61
I want to replace detecting a double-click with detecting Ctrl-click, but I do not know how to do so.
$(document).ready(function () {
$(document).dblclick(function (e) {
var target = (e && e.target) || (event && event.srcElement);
var tag = target.tagName.toLowerCase();
if (!(tag == 'input' || tag == 'textarea' || tag == 'img')) {
openIfSelected("dblclick");
}
});
});
Upvotes: 0
Views: 2656
Reputation: 33366
You are looking for the jQuery .click()
method and the MouseEvent.ctrlKey
property.
You could do something like:
$(document).ready(function () {
$(document).click(function (e) {
if(e.ctrlKey){
var tag = e.target.tagName.toLowerCase();
if (!(tag == 'input' || tag == 'textarea' || tag == 'img')) {
openIfSelected("dblclick"); //Still pass "dblclick", but upon Ctrl-click
}
}
});
});
function openIfSelected(reason){
//This is now only called upon Ctrl-click, but the "reason" remains "dblclick".
console.log('Called "openIfSelected" with reason: ' + reason);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><kbd>Ctrl</kbd>-Click me</div>
Note:
You used the code:
var target = (e && e.target) || (event && event.srcElement);
This is strange as written. You were probably attempting to use window.event
. However, this is not needed because you are using jQuery, which normalizes the event object that is passed to event handlers, specifically so you don't have to do things like this. In addition, the tags you are using declare this question as a Google Chrome extension issue. If that is the case, and you weren't using jQuery, then you would still not need to do this as you do not need to account for the use of the code in older browsers (e.g. IE).
Upvotes: 1
Reputation: 61
I fixed like this. Big thanks Makyen.
$(document).ready(function () {
$(document).click(function (e) {
if(event.ctrlKey){
var target = event.srcElement;
var tag = target.tagName.toLowerCase();
if (!(tag == 'input' || tag == 'textarea' || tag == 'img')) {
openIfSelected("dblclick");
}
}
});
});
Upvotes: 0