I have an SVG 'app' that uses many key commands for control. The problem I have is that with all the various techniques I have researched and tried, I have not been able to find the correct way to isolate key combos without them colliding. For example, if I trap for Y
and Ctrl
Y
, when I press Ctrl
Y
, the Ctrl
Y
responds, but so does the Y
.
I can't possibly paste all the tests I have tried that failed, but I did manage to come up with my own solution that, while working, seems quite burdensome. So, what is the right way to trap for key combos?
p.s. I am not a JS programmer, and this 'app' is my first JS project, outside of some onclick
functions in webpages.
Thanks
Most of the examples I have found look something like...
var lastkey = {}
window.addEventListener('keydown', function (event) {
lastkey[event.code] = true
lastkey[event.key] = true
if (lastkey['Alt'] && lastkey['KeyY']) { blah() }
if (lastkey['KeyY']) { blahblah() }
});
window.addEventListener('keyup', function (event) {
lastkey[event.code] = false
lastkey[event.key] = false
});
The version I wrote that works is...
var lastkey = {}
function isOn(key) {
if (typeof lastkey[key] === "undefined") {
res = false
} else {
res = true
}
if (typeof lastkey[key] === "boolean") {
res = lastkey[key]
}
return res;
}
window.addEventListener('keydown', function(event) {
lastkey[event.code] = lastkey[event.key] = true
if (isOn('Alt') && !isOn('Control') && !isOn('Meta') && !isOn('Shift')) {
isAlt = true
}
if (!isOn('Alt') && isOn('Control') && !isOn('Meta') && !isOn('Shift')) {
isCtl = true
}
if (!isOn('Alt') && !isOn('Control') && !isOn('Meta') && !isOn('Shift')) {
isKey = true
}
if (!isOn('Alt') && !isOn('Control') && isOn('Meta') && !isOn('Shift')) {
isMet = true
}
if (!isOn('Alt') && !isOn('Control') && isOn('Meta') && !isOn('Shift')) {
isSft = true
}
//? CONTROL-ALT
if (isOn('Alt') && isOn('Control') && !isOn('Meta') && !isOn('Shift')) {
isAltCtl = true;
isAlt = isCtl = isMet = isSft = isAltMet = isSftCtl = false
}
//? ALT-META
if (isOn('Alt') && !isOn('Control') && isOn('Meta') && !isOn('Shift')) {
isAltMet = true;
isAlt = isCtl = isMet = isSft = isSftCtl = isAltCtl = false
}
//? CONTROL-META
if (!isOn('Alt') && isOn('Control') && isOn('Meta') && !isOn('Shift')) {
isCtlMet = true;
isAlt = isCtl = isMet = isSft = isAltMet = isSftCtl = isAltCtl = false
}
//? SHIFT-CONTROL
if (!isOn('Alt') && isOn('Control') && !isOn('Meta') && isOn('Shift')) {
isSftCtl = true;
isAlt = isCtl = isMet = isSft = isCtlMet = isAltMet = isAltCtl = false
}
if (isKey && lastkey['KeyY']) {
console.log("Y")
}
if (isCtl && lastkey['KeyY']) {
console.log("Ctl-Y")
}
if (isAlt && lastkey['KeyY']) {
console.log("Alt-Y")
}
if (isMet && lastkey['KeyY']) {
console.log("Meta-Y")
}
if (isCtlMet && lastkey['KeyY']) {
console.log("Control-Meta-Y")
}
if (isAltMet && lastkey['KeyY']) {
console.log("Alt-Meta-Y")
}
if (isSftCtl && lastkey['KeyY']) {
console.log("Shift-Control-Y")
}
});
window.addEventListener('keyup', function(event) {
lastkey[event.code] = lastkey[event.key] = false
isCtl = isAlt = isKey = isMet = isSft = isSftCtl = isAltCtl = isAltMet = isCtlMet = false
});