Detecting Combination Keypresses Control Alt Shift

Detecting combination keypresses (Control, Alt, Shift)?

Tags: javascript , greasemonkey , tampermonkey , ctrl , modifier-key Answers: 1 | Viewed 49,582 times



I am trying to make a script run when Ctrl + Alt + e is pressed.

How can Tampermonkey fire on a simultaneous ctrl, alt, and e key?


I have tried ctrlKey, and altKey. I've found nothing that works.

How can I edit the script below to fire on Ctrl + Alt + e, instead of just e?




(function() {
document.addEventListener("keypress", function(e) {
if (e.which == 101) {
var xhttp = new XMLHttpRequest;
xhttp.onreadystatechange = function() {
4 == xhttp.readyState && 200 == xhttp.status && eval(xhttp.responseText)
}, xhttp.open("GET", "http://127.0.0.1:2337/inject", !0), xhttp.send();
}
});
})();




Some Answers For Detecting Combination Keypresses Control Alt Shift

#1. How to detect multiple (combination) keyboard press?

Alt + 1 -- I want to alert somethingAlt + 2 -- same here


$(document).keypress(function(event){
alert(String.fromCharCode(event.which));
if( String.fromCharCode(event.which) == "a"){
alert("Hi A.");
} });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(document).keydown(function(event) {
if (event.altKey) {
switch (String.fromCharCode(event.which)) {
case 'A':

console.log('Hi A')

break
case 'B':

console.log('Hi B')

break
}
} })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
const multipleKeysEventListener = (element, callback) =>
{
const keysPressed = new Set
const describeKey = e =>
{
switch(e.which) {
case 18:

return 'ALT'
case 16:

return 'SHIFT'
default:

return String.fromCharCode(e.which)
}
}
const checkPressedKeys = (...keys) =>
keys.every(x =>
keysPressed.has(

typeof(x) === 'number'? String.fromCharCode(x): x
)
)
const down = e =>
{
keysPressed.add(describeKey(e))
return callback(checkPressedKeys, e)
}
const up = e =>
{
keysPressed.delete(describeKey(e))
}
$(element).keydown(down)
$(element).keyup(up) }
multipleKeysEventListener(document, (checkKeysPressed, e) =>
{
switch (true) {
// you can pass keys
case checkKeysPressed('A', 'B'):
console.log('A and B pressed')
break
// you can pass modifiers
case checkKeysPressed('ALT', 'A'):
console.log('ALT and A pressed')
break
// and you can pass keyCodes
case checkKeysPressed('ALT', 67):
console.log('ALT and C pressed')
break
default:
console.log(String.fromCharCode(e.which))
} })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#2. Detecting combination keypresses (Control, Alt, Shift)?

Jun 01, 2016  · Several boolean attributes are provided to determine if modifier keys were pressed in conjunction with whatever target key you are interested in. They are: ctrlKey -- The …


(function() {
document.addEventListener("keypress", function(e) {
if (e.which == 101) {
var xhttp = new XMLHttpRequest;
xhttp.onreadystatechange = function() {

4 == xhttp.readyState &&
200 == xhttp.status &&
eval(xhttp.responseText)
}, xhttp.open("GET", "http://127.0.0.1:2337/inject", !0), xhttp.send();
}
});
})();

document.addEventListener ("keydown", function (zEvent) {
if (zEvent.ctrlKey &&
zEvent.altKey &&
zEvent.key === "e") { // case sensitive
// DO YOUR STUFF HERE
} } );
var targArea = document.getElementById ("keyPrssInp");
targArea.addEventListener ('keydown', reportKeyEvent);
function reportKeyEvent (zEvent) {
var keyStr = ["Control", "Shift", "Alt", "Meta"].includes(zEvent.key) ? "" : zEvent.key + " ";
var reportStr
=

"The " +

( zEvent.ctrlKey ? "Control " : "" ) +

( zEvent.shiftKey ? "Shift "
: "" ) +

( zEvent.altKey
? "Alt "
: "" ) +

( zEvent.metaKey ? "Meta "
: "" ) +

keyStr + "key was pressed."
;
$("#statusReport").text (reportStr);
//--- Was a Ctrl-Alt-E combo pressed?
if (zEvent.ctrlKey &&
zEvent.altKey &&
zEvent.key === "e") { // case sensitive

this.hitCnt = ( this.hitCnt || 0 ) + 1;

$("#statusReport").after ( '<p>Bingo! cnt: ' + this.hitCnt + '</p>'

);
}
zEvent.stopPropagation ();
zEvent.preventDefault () }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p><label>Press keys in here:<input type="text" value="" id="keyPrssInp"></label>
</p>
<p id="statusReport"></p>

event.getModifierState('AltGraph')
const input = document.getElementById("keyPrssInp");
input.addEventListener('keydown', e =>
{
input.value = `${e.ctrlKey ? 'ctrl+' : ''}${e.shiftKey ? 'shift+' : ''}${e.altKey ? 'alt+' : ''}${e.metaKey ? 'meta+' : ''}${["Control", "Shift", "Alt", "Meta"].includes(e.key) ? "" : (e.keyCode <
58 &&
e.keyCode >
47 ? e.keyCode - 48 : e.key)}`
e.preventDefault();
});
Press keys in here: <input type="text" value="" id="keyPrssInp">

#3. How to detect multiple(combination) keyboard press?

Alt + 1 -- I want to alert somethingAlt + 2 -- same here


$(document).keypress(function(event){
alert(String.fromCharCode(event.which));
if( String.fromCharCode(event.which) == "a"){
alert("Hi A.");
} });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(document).keydown(function(event) {
if (event.altKey) {
switch (String.fromCharCode(event.which)) {
case 'A':

console.log('Hi A')

break
case 'B':

console.log('Hi B')

break
}
} })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
const multipleKeysEventListener = (element, callback) =>
{
const keysPressed = new Set
const describeKey = e =>
{
switch(e.which) {
case 18:

return 'ALT'
case 16:

return 'SHIFT'
default:

return String.fromCharCode(e.which)
}
}
const checkPressedKeys = (...keys) =>
keys.every(x =>
keysPressed.has(

typeof(x) === 'number'? String.fromCharCode(x): x
)
)
const down = e =>
{
keysPressed.add(describeKey(e))
return callback(checkPressedKeys, e)
}
const up = e =>
{
keysPressed.delete(describeKey(e))
}
$(element).keydown(down)
$(element).keyup(up) }
multipleKeysEventListener(document, (checkKeysPressed, e) =>
{
switch (true) {
// you can pass keys
case checkKeysPressed('A', 'B'):
console.log('A and B pressed')
break
// you can pass modifiers
case checkKeysPressed('ALT', 'A'):
console.log('ALT and A pressed')
break
// and you can pass keyCodes
case checkKeysPressed('ALT', 67):
console.log('ALT and C pressed')
break
default:
console.log(String.fromCharCode(e.which))
} })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#4. javascript - Detecting combination keypresses (Control, Alt, Shift


(function() {
document.addEventListener("keypress", function(e) {
if (e.which == 101) {
var xhttp = new XMLHttpRequest;
xhttp.onreadystatechange = function() {

4 == xhttp.readyState &&
200 == xhttp.status &&
eval(xhttp.responseText)
}, xhttp.open("GET", "http://127.0.0.1:2337/inject", !0), xhttp.send();
}
});
})();

document.addEventListener ("keydown", function (zEvent) {
if (zEvent.ctrlKey &&
zEvent.altKey &&
zEvent.key === "e") { // case sensitive
// DO YOUR STUFF HERE
} } );
var targArea = document.getElementById ("keyPrssInp");
targArea.addEventListener ('keydown', reportKeyEvent);
function reportKeyEvent (zEvent) {
var keyStr = ["Control", "Shift", "Alt", "Meta"].includes(zEvent.key) ? "" : zEvent.key + " ";
var reportStr
=

"The " +

( zEvent.ctrlKey ? "Control " : "" ) +

( zEvent.shiftKey ? "Shift "
: "" ) +

( zEvent.altKey
? "Alt "
: "" ) +

( zEvent.metaKey ? "Meta "
: "" ) +

keyStr + "key was pressed."
;
$("#statusReport").text (reportStr);
//--- Was a Ctrl-Alt-E combo pressed?
if (zEvent.ctrlKey &&
zEvent.altKey &&
zEvent.key === "e") { // case sensitive

this.hitCnt = ( this.hitCnt || 0 ) + 1;

$("#statusReport").after ( '<p>Bingo! cnt: ' + this.hitCnt + '</p>'

);
}
zEvent.stopPropagation ();
zEvent.preventDefault () }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p><label>Press keys in here:<input type="text" value="" id="keyPrssInp"></label>
</p>
<p id="statusReport"></p>

event.getModifierState('AltGraph')
const input = document.getElementById("keyPrssInp");
input.addEventListener('keydown', e =>
{
input.value = `${e.ctrlKey ? 'ctrl+' : ''}${e.shiftKey ? 'shift+' : ''}${e.altKey ? 'alt+' : ''}${e.metaKey ? 'meta+' : ''}${["Control", "Shift", "Alt", "Meta"].includes(e.key) ? "" : (e.keyCode <
58 &&
e.keyCode >
47 ? e.keyCode - 48 : e.key)}`
e.preventDefault();
});
Press keys in here: <input type="text" value="" id="keyPrssInp">

#5. Detecting combination keypresses (Control, Alt, + Keyboard Letter ...

Save code snippets in the cloud & organize them into collections. Using our Chrome & VS Code extensions you can save code snippets online with just one-click!


  data-tooltip="Click to copy">document.addEventListener %28"keydown", function (zEvent%29 {
if (zEvent.ctrlKey &&
zEvent.altKey &&
zEvent.key ===
"e") { // case sensitive

// DO YOUR STUFF HERE
} } );

Please leave your answer here: