How to Make a Hacker Typer in JavaScript

Updated · 6 min read · By

A hacker typer is a great first JavaScript project. It is short, the result is fun, and it teaches three ideas you will use everywhere: handling events, changing the page, and keeping track of state. This tutorial builds a complete one in a single file.

JavaScript code for a simple hacker typer: a keydown listener that reveals more source text

The idea

The trick is simple. The page holds a long string of code. A counter remembers how much of it has been shown. Each key press increases the counter and displays a slightly longer slice of the string. Which key was pressed does not matter. (More background in how Hacker Typer works.)

Step 1: the HTML

Create a file named index.html. We need one element to hold the code. A <pre> element keeps spaces and line breaks exactly as written, which is what code needs.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My Hacker Typer</title>
  <!-- CSS from step 2 goes here -->
</head>
<body>
  <pre id="screen"></pre>
  <!-- JavaScript from step 3 goes here -->
</body>
</html>

Step 2: the CSS

Black background, green monospace text, and a blinking cursor made with a pseudo-element. Put this inside the <head>.

<style>
  body { margin: 0; background: #000; }
  #screen {
    margin: 0; padding: 16px;
    height: 100vh; box-sizing: border-box; overflow: hidden;
    color: #00ff66; font: 16px/1.4 "Courier New", monospace;
    white-space: pre-wrap;
    display: flex; flex-direction: column; justify-content: flex-end;
  }
  #screen span::after {
    content: "\2588";              /* a solid block character */
    animation: blink 1s step-end infinite;
  }
  @keyframes blink { 50% { opacity: 0; } }
</style>

The flexbox lines pin the text to the bottom of the screen, so new code pushes old code up and out of view, like a real terminal.

Step 3: the JavaScript

Put this just before </body>.

<script>
  // 1. The code to "type". Paste any text you like between the backticks.
  const source = `function connect(host, port) {
  const socket = new Socket(host, port);
  socket.on("open", () => {
    console.log("link established");
    socket.send(handshake(KEY));
  });
  return socket;
}

for (const node of network.nodes) {
  if (node.isOpen()) {
    connect(node.address, 443);
  }
}
`;

  // 2. State: how many characters are visible, and how many to add per key.
  let position = 0;
  const speed = 3;

  const screen = document.getElementById("screen");
  const text = document.createElement("span");
  screen.appendChild(text);

  // 3. Reveal the next few characters, looping when we reach the end.
  function type() {
    for (let i = 0; i < speed; i++) {
      text.textContent += source[position];
      position = (position + 1) % source.length;
    }
  }

  // 4. Any key press triggers it.
  document.addEventListener("keydown", (event) => {
    if (event.ctrlKey || event.metaKey) return; // keep browser shortcuts working
    event.preventDefault();
    type();
  });
</script>

Open the file in a browser and press some keys. That is a working hacker typer.

How it works, line by line

Ideas for extending it

  1. Access Granted. Count presses of the Alt key (event.key === "Alt") and show a large message on the third one.
  2. Mobile support. Phones have no keydown without a focused field. Add an invisible <input>, focus it when the screen is tapped, and call type() on its input event.
  3. Auto-type. setInterval(type, 60) makes it type by itself.
  4. Speed slider. Change speed from an <input type="range">.
  5. Trim old text. After a long session the string gets large. Keep only the last few thousand characters.
  6. Load your own code. Use fetch() to read a text file instead of a built-in string.

Most of these are in Hacker Typer 2.0, so you can see how each one feels before building it.

FAQ

Can I make a hacker typer with only HTML and CSS?

Not a real one. HTML and CSS can show and style the text, but reacting to key presses needs JavaScript.

Can I use this code in my own project?

Yes. The tutorial code on this page is free to use, change and publish.

Where can I publish my hacker typer?

Any static hosting works because it is a single HTML file. Online editors such as CodePen are a quick way to share it.

More tools

See all tools

Keep reading