{"id":26895981,"url":"https://github.com/ashubham/key-sequence","last_synced_at":"2025-10-17T11:01:47.905Z","repository":{"id":57289166,"uuid":"54866468","full_name":"ashubham/key-sequence","owner":"ashubham","description":"Detect a sequence of key presses.","archived":false,"fork":false,"pushed_at":"2016-04-01T09:08:10.000Z","size":33,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-01T03:26:47.133Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashubham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-28T04:56:47.000Z","updated_at":"2024-04-11T15:01:28.000Z","dependencies_parsed_at":"2022-08-29T12:02:27.210Z","dependency_job_id":null,"html_url":"https://github.com/ashubham/key-sequence","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashubham%2Fkey-sequence","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashubham%2Fkey-sequence/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashubham%2Fkey-sequence/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashubham%2Fkey-sequence/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashubham","download_url":"https://codeload.github.com/ashubham/key-sequence/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246574844,"owners_count":20799221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-01T02:59:20.621Z","updated_at":"2025-10-17T11:01:47.843Z","avatar_url":"https://github.com/ashubham.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# key-sequence\n\u003ca href=\"https://travis-ci.org/ashubham/key-sequence\"\u003e\n  \u003cimg src=\"https://api.travis-ci.org/ashubham/key-sequence.svg?branch=master\" class=\"badge\"\u003e\n\u003c/a\u003e\n\u003ca href='https://coveralls.io/github/ashubham/key-sequence?branch=master'\u003e\n    \u003cimg src='https://coveralls.io/repos/github/ashubham/key-sequence/badge.svg?branch=master' alt='Coverage Status' /\u003e\n\u003c/a\u003e\n\n[![NPM](https://nodei.co/npm/key-sequence.png?downloads=true\u0026stars=true\u0026downloadRank=true)](https://www.npmjs.org/package/key-sequence)\n\nA featherweight utility to detect a sequence of key presses or a stream, and call the supplied callback. Fast!\n\n### Usage\n\n```javascript\nvar keySequence = require('key-sequence'); // CommonJS\n\u003cscript src=\"key-sequence.min.js\"\u003e\u003c/script\u003e // Browser\n\n// Regex '+'/'*' (denotes repeatable characters)\n// The below matches 'omg\u003center\u003e'/'omgggg\u003center\u003e'/'o   mggggggg\u003center\u003e'\nvar onKey = keySequence(['o', '\\s*', 'm', 'g+', '\\n'], (evt) =\u003e {\n    // Do what needs to be done when the key sequence is detected.\n    console('OMG it works!', evt);\n});\n\n// Konami code\nvar onKey = keySequence([38, 38, 40, 40, 37, 39, 37, 39, 'b', 'a'], () =\u003e {\n    console.log('Achievement unlocked!!');\n});\n\n// Use with Observables\nsourceKeyStream.subscribe(\n  k =\u003e onKey(k));\n\n// Can pass keycode using e.which using jQuery or events.\n$(document).keypress(function(e) {\n    onKey(e.which, e /* optional */);\n});\n\n// or pass the character itself.\nonKey('x');\n\n\n/* Some more supported keysequences: */\n[17, 'c'] //The classic \u003cctrl\u003e and 'c'\n['I', '.*', 'U'] // I \u003canything in between\u003e U\n```\n\n### Features\n\n* Supports `+` and `*` as wildcards to denote repeating characters.\n* Support for keyCodes, so you can use metakeys like `\u003cctrl\u003e`, `\u003calt\u003e` in the sequence.\n* Support for meta characters like `\\s`, `.` etc.\n\n### Wait, but why ? I'd rather use a regex.\n\nUsing a regex is slow and cannot get less elegant, its `O(n)` in the size of your total input,\non every single keystroke.\n\n`key-sequence` generates a DFA and maintains the state. `O(1)` every single keystroke.\nCan think of it as a regex which accepts streaming input.\n\n### TODO\n\n\u003e - Support for more Regex metacharacters like `|` etc.\n\n### Theory\n\n![Image](img/sample-dfa.png?raw=true)\n\nThis is the DFA for the above example. `key-sequence` maintains the present state according to the key input.\nOnce the end state(`4` in this case) is reached the supplied `done` callback is called.\n\nIf, at any point the input does not match a transition, we go back to the starting state `(0)`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashubham%2Fkey-sequence","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashubham%2Fkey-sequence","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashubham%2Fkey-sequence/lists"}