Remember how you execute the cheat on GTA? I want to execute my code in jquery when I pressed a certain combination of keys. Is this possible in jQuery? Like for example, I want to display an alert when I press ctrl + h + e + l + l + o
. Can anyone teach me how to do it?
Asked
Active
Viewed 73 times
-1

Mark Vincent Manjac
- 507
- 1
- 6
- 27
-
1on keyup event if the key matches your key selection then run the function? – guradio Oct 10 '15 at 08:59
-
Possible duplicate of [javascript multiple keys pressed at once](http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once) – Max Leske Oct 10 '15 at 11:52
3 Answers
0
Tested and working:
var combo = 'hello';
var keys = [];
function onKeyUp(evt) {
if (!evt.ctrlKey) return;
keys.push(String.fromCharCode(evt.keyCode).toLowerCase());
if (keys.length > combo.length) keys.shift();
if (keys.join('') == combo) //you have a match
}

eosterberg
- 1,422
- 11
- 11
0
Ok, I finally figured out this one.
var combo = '72,73,68,69'; //The keycode combination you want and It is 'h','i','d','e'
var imp;
var key = []; //this is where I will store all the keycode
$(document).on('keyup', function(e) {
if (e.which == 72 || e.which == 73 || e.which == 68 || e.which == 69) //this is optional. Filtering the keycode only to what your combination has.
key.push(e.which);
imp = key.join(',');
setTimeout(function() { //delete the stored keycode after 3 seconds
key.length = 0;
}, 3000);
if (imp == combo) { //if keycode combination is equal to your combination then alert
alert('I did it!');
key.length = 0;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Mark Vincent Manjac
- 507
- 1
- 6
- 27
-1
I'm not sure if there is a limit for the key combination, but should be possible.
This is an example I used to fire a call to my function getContent()
:
$('#searchInput').on('keyup', function(e) {
if (e.keyCode == 13) {
getContent();
}
});
What you could need to do is modify the e.keyCode == 13
(which is the code for the enter
) with the keyCode of ctrl
and add the other keys you need
Cheers

tabris963
- 110
- 2
- 14