Skip to content

Commit 9828dd4

Browse files
Fixed the bug in detecting AltGr #4
1 parent 149a5b2 commit 9828dd4

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

script.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,38 @@ const handleKeyPress = function (e) {
3838
console.log(e);
3939

4040
e.preventDefault();
41+
4142
// Edge case mentioned in https://github.com/Mostafa-Abbasi/KeyboardTester/issues/4
4243
// Detect AltGr key press (Alt + Control pressed simultaneously)
43-
const isAltGr = e.ctrlKey && e.altKey && !e.shiftKey && !e.metaKey;
44+
const isAltGr = e.key === 'AltGraph';
4445

45-
let keyElement;
46+
// Ignore the left Control key if AltGr is pressed
4647
if (isAltGr) {
47-
keyElement = document.querySelector('.' + 'AltRight');
48-
} else {
49-
keyElement = document.querySelector('.' + e.code.toLowerCase());
48+
document
49+
.querySelector('.' + 'controlleft')
50+
.classList.remove('key-pressing-simulation');
51+
52+
document
53+
.querySelector('.' + 'controlleft')
54+
.classList.remove('key--pressed');
5055
}
5156

57+
const keyElement = document.querySelector('.' + e.code.toLowerCase());
58+
5259
if (e.type === 'keydown') {
5360
keyElement.classList.add('key-pressing-simulation');
5461
} else if (e.type === 'keyup') {
5562
keyElement.classList.remove('key-pressing-simulation');
5663
}
5764

58-
if (!keyElement.classList.contains('key--pressed'))
65+
if (!keyElement.classList.contains('key--pressed')) {
5966
keyElement.classList.add('key--pressed');
67+
}
6068

61-
// 'Meta' or 'OS' is a bit tricky and only in this way
62-
// we can reliably remove the class from the element
63-
if (e.key === 'Meta' || e.key === 'OS')
69+
// Handle special Meta/OS key case
70+
if (e.key === 'Meta' || e.key === 'OS') {
6471
keyElement.classList.remove('key-pressing-simulation');
72+
}
6573
};
6674

6775
document.addEventListener('keydown', handleKeyPress);

0 commit comments

Comments
 (0)