I am trying to create an easy way for my users to select the trigger key that makes the code work altogether. I have this so far.
var triggers = {space:"32",enter:"13",tab:"9"};
var triggerKeys = ["space","enter"];
$('input').on('keypress',function(event) {
var unicode= event.keyCode ? event.keyCode : event.which;
if(unicode === triggers[triggerKeys]) {
//code initiation here
}
Though of course this doesn't work. Any ideas on how I could possibly make it so in the array of triggerKeys
the code is initiated, as well if they have more than one triggerKeys?
Code works for just one Trigger. Should I for loop the triggerKeys for more than one?
Not sure what you're after, but I'd do something like this :
$('input').on('keypress',function(e) {
switch (e.which) {
case 32: //space
console.log('space');
break;
case 13: //enter
console.log('enter');
break;
case 9: //tab
console.log('tab');
}
});
as using an array as a key for another array never seems to work for me ?
If you are just trying to check if a key exists in an array, store the keycodes :
var triggerKeys = [32, 13, 9];
$('input').on('keypress',function(e) {
if ( $.inArray(e.which, triggerKeys) != -1 ) {
// one of the triggerkeys was pressed
}
});
EDIT:
Here's another example with the map you're using, and checking if a trigger key was pressed :
var triggerKeys = ["space","enter", "tab"];
var triggers = {space: 32, enter: 13, tab: 9};
$('input').on('keypress',function(e) {
var arr = $.map(triggerKeys, function(el, i) {return triggers[el];});
if ( $.inArray( e.which, arr ) != -1 ) {
console.log('trigger key pressed')
}
});