3
Hello, I’m making a system to detect javascript shortcuts, and it works great, except if for example click on SHIFT + 1
what I get SHIFT+!
, and all the other changes shift makes. There is some way to standardize this?
Below is the code I made, someone can help a little?
/**
* This function convert pressed keys in formatted string.
*
* @param {Object} event - The event.
*
* @returns {string} Returns string formated.
* E.g.: "CTRL+A"
*/
function convertToStringPressedKeys(event) {
const specialKeys = [
{
key: "ctrlKey",
original: "CONTROL",
value: "CTRL"
},
{
key: "altKey",
original: "ALT",
value: "ALT"
},
{
key: "shiftKey",
original: "SHIFT",
value: "SHIFT"
}
];
const pressedKey = specialKeys
.filter(specialKey => event[specialKey.key])
.map(specialKey => specialKey.value);
const keyPressed = event.key.toUpperCase();
const kepPressedIsSpecial = specialKeys.find(
key => key.original === keyPressed
);
!kepPressedIsSpecial && pressedKey.push(keyPressed);
return pressedKey.join("+");
};
function handleOnKeyDown(event) {
const combinationKeys = convertToStringPressedKeys(event);
console.log(combinationKeys);
event.preventDefault();
}
document.body.addEventListener('keydown', handleOnKeyDown);
I liked the question. At my level of knowledge, I would do replacing the characters with
Shift
the equivalent without. Example:!
flipped1
,@
flipped2
,...
and, for the letters, just call the methodtoLowerCase()
...– LipESprY
What you define as "unifying"?
– Luiz Felipe
When I talk about standardizing, it is for example imagine that a key can correspond to three different characters depending on whether SHIFT, CTRL, ALT are pressed simultaneously. what I want is to abstract it. I want to click on an independent key if SHIFT, CTRL, ALT is pressed. I could notice?
– Cláudio Hilário
One option would be to make a kind of dictionary, but it depends on the keyboards, I’m not sure how it works
– Cláudio Hilário