georgemandis / konami-js

Adding the Konami Code easter egg to your projects since 2009! Compatible with gestures on smartphones and tablets as well. Compatible with all front-end frameworks and vanilla JavaScript
http://konamijs.mand.is/
MIT License
959 stars 122 forks source link

Potential keyboard bug #55

Closed Girgias closed 2 years ago

Girgias commented 5 years ago

In the source file shouldn't line 13 sequence: ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA'] , be equal to sequence: ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a'], ?

georgemandis commented 5 years ago

On a keydown event e.code will return KeyB and KeyA if either the "A" or "B" keys are pressed on the keyboard. This works in latest Chrome, Firefox and Safari. If you look at event.key you'll see "b" and "a" or "B" and "A" depending on whether you held down shift or have caps-lock enabled.

Internet Explorer and Edge don't support e.code in the same way, but in light of Edge moving to Chromium now... I'm debating if a workaround matters? There is a polyfill keyboard events that would make e.code behave consistently across browsers, but I'd rather avoid any external dependencies or code bloat for what I suspect is a really small % of use cases and only going to shrink...

Girgias commented 5 years ago

Fair enough also is it's just for an easter egg if it doesn't work on some browsers it doesn't really matter. Will close the issue

Girgias commented 5 years ago

Actually gonna reopen the issue just did a quick test on Firefox latest version and it seems like the e.code is keyboard layout dependent. On my french AZERTY keyboard pressing A will result in a 'KeyQ' event being launched

(EDIT: however this is maybe a FF bug) (EDIT2: This is not a FF bug it's something intended by the spec. See: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code )

georgemandis commented 5 years ago

@Girgias That's a very interesting catch! This seems like an good argument for using key instead of code. I'll take a look at updating the code in the 2.0 branch. If you feel like tackling it please feel free to submit a pull request!

Girgias commented 5 years ago

Not the best in JS but that should be doable will see if I manage.