<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez" -->

---
title: Konami Code on a Keyboard: Detect It in JavaScript
description: You can detect the Konami Code in plain JavaScript with a keydown listener, a konamiCode array, and a keyCount counter. Each event's e.key (ArrowUp, b) is...
canonical: https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Konami Code on a Keyboard: Detect It in JavaScript | daily.dev
og:description: You can detect the Konami Code in plain JavaScript with a keydown listener, a konamiCode array, and a keyCount counter. Each event's e.key (ArrowUp, b) is...
og:url: https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez
og:image: https://api.daily.dev/og/posts/c0wEaCYEZ.png
og:image:alt: Konami Code on a Keyboard: Detect It in JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Konami Code on a Keyboard: Detect It in JavaScript

**[Kirupa](https://daily.dev/sources/kirupa)** · 16 min read · 0 upvotes · 0 comments

## Summary

You can detect the Konami Code in plain JavaScript with a keydown listener, a konamiCode array, and a keyCount counter. Each event's e.key (ArrowUp, b) is compared to the array. This replaces the deprecated e.keyCode numbers and the lookup object they required. Held-down repeats are skipped via e.repeat. Every correct key restarts a 1000 ms setTimeout, and a wrong key or a slower gap resets keyCount to 0. When keyCount equals the array length, an alert says 'Cheat code activated!'.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/konami_code.htm>

## Questions this post answers

### Should I use e.key or e.keyCode to detect arrow keys and letters in JavaScript keydown handlers?

Use e.key. The keyCode property is deprecated and returns numbers like 38 for the up arrow and 66 for B, which forces a separate lookup object. The key property returns readable names such as ArrowUp and b, so an array of those names can be compared directly. Lowercase single-character keys so B and b match.

_Developers modernizing old keyboard-handling code keep up with deprecations like this on daily.dev._

### How do I stop a held-down key from counting twice in a JavaScript keydown sequence check?

Check e.repeat at the top of the keydown handler and return early when it is true. Browsers keep firing keydown events while a key is held, each with e.repeat set to true, so without the check holding the up arrow would count as both ups in a sequence. Also ignore lone modifier keys like Shift and CapsLock.

_Small browser-event gotchas like this are what working web developers trade notes on in daily.dev._

### How can I make a key sequence reset if the user types too slowly in JavaScript?

Keep a timer ID and, on each correct key, call clearTimeout and then start a new setTimeout of 1000 ms that calls a reset function. If the next valid key arrives in time, the countdown restarts. If not, the reset sets the counter back to 0 and clears the timer.

_Anyone building timing-sensitive UI logic benefits from following JavaScript techniques on daily.dev._

## Similar posts on daily.dev

- [Press and Hold \(Long Press\) Button in JavaScript](https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t) · Kirupa · 0 upvotes · 0 comments
- [The Gamepad API Lies to You: A Practical Guide to Reading Controller Input in JavaScript](https://daily.dev/posts/the-gamepad-api-lies-to-you-a-practical-guide-to-reading-controller-input-in-javascript-92khctuvi) · freeCodeCamp · 1 upvotes · 0 comments
- [A simple "copy this code" button in JavaScript](https://daily.dev/posts/a-simple-copy-this-code-button-in-javascript-1lsbtexwg) · Terence Eden · 2 upvotes · 0 comments
- [JavaScript: Simple Demo](https://daily.dev/posts/javascript-simple-demo-m5tpqbvod) · Medium · 0 upvotes · 0 comments
- [A minimal keyboard key effect with CSS](https://daily.dev/posts/a-minimal-keyboard-key-effect-with-css-lrsy90dcz) · Lobsters · 1 upvotes · 0 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#javascript](https://daily.dev/tags/javascript)

[View this post on daily.dev](https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Konami Code on a Keyboard: Detect It in JavaScript","url":"https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez"},"datePublished":"2026-11-28T06:30:38.094Z","dateModified":"2026-11-28T06:31:14.736Z","description":"You can detect the Konami Code in plain JavaScript with a keydown listener, a konamiCode array, and a keyCount counter. Each event's e.key (ArrowUp, b) is...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d3aa21797bd84214e59a8ccc77ab0929?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d3aa21797bd84214e59a8ccc77ab0929?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"webdev,javascript","timeRequired":"PT16M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"Konami Code on a Keyboard: Detect It in JavaScript"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez#faq","mainEntity":[{"@type":"Question","name":"Should I use e.key or e.keyCode to detect arrow keys and letters in JavaScript keydown handlers?","acceptedAnswer":{"@type":"Answer","text":"Use e.key. The keyCode property is deprecated and returns numbers like 38 for the up arrow and 66 for B, which forces a separate lookup object. The key property returns readable names such as ArrowUp and b, so an array of those names can be compared directly. Lowercase single-character keys so B and b match. Developers modernizing old keyboard-handling code keep up with deprecations like this on daily.dev."}},{"@type":"Question","name":"How do I stop a held-down key from counting twice in a JavaScript keydown sequence check?","acceptedAnswer":{"@type":"Answer","text":"Check e.repeat at the top of the keydown handler and return early when it is true. Browsers keep firing keydown events while a key is held, each with e.repeat set to true, so without the check holding the up arrow would count as both ups in a sequence. Also ignore lone modifier keys like Shift and CapsLock. Small browser-event gotchas like this are what working web developers trade notes on in daily.dev."}},{"@type":"Question","name":"How can I make a key sequence reset if the user types too slowly in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Keep a timer ID and, on each correct key, call clearTimeout and then start a new setTimeout of 1000 ms that calls a reset function. If the next valid key arrives in time, the countdown restarts. If not, the reset sets the counter back to 0 and clears the timer. Anyone building timing-sensitive UI logic benefits from following JavaScript techniques on daily.dev."}}]}
```

