Skip to content

Instantly share code, notes, and snippets.

@kvnxiao
Last active November 16, 2023 10:31
Show Gist options
  • Save kvnxiao/bf4ae8cc6838509b101663895a195bae to your computer and use it in GitHub Desktop.
Save kvnxiao/bf4ae8cc6838509b101663895a195bae to your computer and use it in GitHub Desktop.
Generate KeyboardEvent.code values from MDN Table
// Generates string union type values from Keyboard event code values taken from the MDN table located at:
// https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values#code_values_on_windows
const table = document.querySelector("table.standard-table");
const firefox = table.querySelectorAll("tbody > tr > td:nth-child(2) > code:first-child");
const ignoreList = ["\"\"", "\"Unidentified\""];
const codes = [...new Set([...firefox].map((node) => node.innerText))].filter(s => !ignoreList.includes(s));
console.log("export type KeyCode =", codes.join("\n | "));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment