{"id":19564259,"url":"https://github.com/ronenness/stinput","last_synced_at":"2025-04-27T00:32:48.220Z","repository":{"id":57371001,"uuid":"131315607","full_name":"RonenNess/stinput","owner":"RonenNess","description":"State-based mouse and keyboard input for JavaScript","archived":false,"fork":false,"pushed_at":"2020-05-15T10:36:35.000Z","size":44,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-09T14:25:07.313Z","etag":null,"topics":["input","javascript-game","javascript-keyboard","javascript-keycodes","javascript-library"],"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/RonenNess.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":"2018-04-27T15:42:03.000Z","updated_at":"2023-11-24T15:23:55.000Z","dependencies_parsed_at":"2022-09-09T01:13:29.900Z","dependency_job_id":null,"html_url":"https://github.com/RonenNess/stinput","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fstinput","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fstinput/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fstinput/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2Fstinput/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RonenNess","download_url":"https://codeload.github.com/RonenNess/stinput/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224051929,"owners_count":17247596,"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":["input","javascript-game","javascript-keyboard","javascript-keycodes","javascript-library"],"created_at":"2024-11-11T05:21:05.610Z","updated_at":"2024-11-11T05:21:06.788Z","avatar_url":"https://github.com/RonenNess.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StInput\n\nState-based input for modern browsers.\n\n## Install\n\n### NPM\n\n```\nnpm install stinput\n```\n\n### Bower\n\n```\nbower install stinput\n```\n\n### Other\n\nJust fetch `stinput.js` from this repo and include in your web page.\n\n## Supported Browsers\n\nStInput don't require any external libs and should work on all modern browsers, but note that it uses JavaScript classes and therefor requires ECMAScript 2015 or newer to run.\n\n## Usage Example\n\nTo use `StInput` you need to create an StInput instance, update it at the end of every frame, and query it. \n\nFor example:\n\n```js\n// create the input manager\ninput = new StInput();\n\n// our main loop\nfunction mainLoop() \n{\n\t// left mouse button is down\n\tif (input.down('mouse_left')) {\n\t\tconsole.log(\"Mouse is down.\");\n\t}\n\n\t// left mouse button was released this update call\n\tif (input.released('mouse_left')) {\n\t\tconsole.log(\"Mouse was released this update call.\");\n\t}\n\t\n\t// left mouse button was released this update call\n\tif (input.pressed('mouse_left')) {\n\t\tconsole.log(\"Mouse was pressed this update call.\");\n\t}\n\t\n\t// keyboard button up arrow was released\n\tif (input.released('left_arrow')) {\n\t\tconsole.log(\"Left arrow was released this update call.\");\n\t}\n\t\n\t// mouse moved\n\tif (input.mouseMoving) {\n\t\tconsole.log(\"Mouse delta:\", input.mouseDelta);\n\t}\n\t\n\t// update input\n\tinput.endFrame();\n}\n\n// start main loop\nsetInterval(mainLoop);\n```\n\nWhen you finish using StInput call ```input.dispose()``` to unregister all its event handlers.\n\n## Full API\n\n### Create / Destroy\n\nCreating New StInput:\n\n```js\n// 'element' is the DOM element you want to register events on. \n// if not defined, will use 'window'.\nvar input = new StInput(element);\n```\n\nDestroying StInput:\n\n```js\ninput.dispose();\n```\n\n### Update\n\nFor StInput to work properly, you must update it every frame of your main loop, **at the end of your update function**. \nTo do so, call `endFrame()`:\n\n```js\ninput.endFrame();\n```\n\n### Mouse Buttons\n\nChecking if mouse button is down:\n\n```js\n// returns if different mouse buttons are held down\ninput.down('mouse_left')\ninput.down('mouse_right')\ninput.down('mouse_middle')\n\n// Or:\n\n// returns if different mouse buttons are held down\ninput.mouseDown(input.MouseButtons.left)\ninput.mouseDown(input.MouseButtons.right)\ninput.mouseDown(input.MouseButtons.middle)\n```\n\nChecking if a mouse button was released this update call:\n\n```js\n// returns if different mouse buttons were released this update call\ninput.released('mouse_left')\ninput.released('mouse_right')\ninput.released('mouse_middle')\n\n// Or:\n\n// returns if different mouse buttons were released this update call\ninput.mouseReleased(input.MouseButtons.left)\ninput.mouseReleased(input.MouseButtons.right)\ninput.mouseReleased(input.MouseButtons.middle)\n```\n\nChecking if mouse button was pressed this update call:\n\n```js\n// returns if different mouse buttons were released this update call\ninput.pressed('mouse_left')\ninput.pressed('mouse_right')\ninput.pressed('mouse_middle')\n\n// Or:\n\n// returns if different mouse buttons were released this update call\ninput.mousePressed(input.MouseButtons.left)\ninput.mousePressed(input.MouseButtons.right)\ninput.mousePressed(input.MouseButtons.middle)\n```\n\n### Mouse Position \u0026 Movement\n\nGet mouse current position:\n\n```js\n// returns Point with x,y representing mouse current position\ninput.mousePosition\t\n```\n\nChecking if mouse is currently moving:\n\n```js\n// returns true if mouse is currently moving\ninput.mouseMoving\n```\n\nGet a point {x,y} representing the mouse position delta since last update call:\n\n```js\n// returns Point with x,y representing mouse movement\ninput.mouseDelta\n```\n\n### Mouse Wheel\n\nGet mouse wheel delta in this update call:\n\n```js\n// get mouse wheel delta\ninput.mouseWheel\n\n// get just the mouse wheel change sign:\ninput.mouseWheelDirection\n```\n\n### Keyboard\n\nChecking if keyboard key is down:\n\n```js\n// returns if 'a' key is down\ninput.down('a')\n\n// Or:\n\n// returns if 'a' key is down\ninput.keyDown(input.KeyboardKeys.a)\n```\n\nChecking if keyboard key was released this update call:\n\n```js\n// returns if 'a' key was released this update call\ninput.released('a')\n\n// Or:\n\n// returns if 'a' key was released this update call\ninput.keyReleased(input.KeyboardKeys.a)\n```\n\nChecking if keyboard key was pressed this update call:\n\n```js\n// returns if 'a' key was released this update call\ninput.pressed('a')\n\n// Or:\n\n// returns if 'a' key was released this update call\ninput.keyPressed(input.KeyboardKeys.a)\n```\n\nCheck if any keyboard key is currently down:\n\n```js\n// returns if any keyboard key is held down\ninput.anyKeyDown\n```\n\nCheck special keys states:\n\n```js\n// returns if alt, ctrl, or shift are currently held down\n// note: in some browsers pressing 'alt' will make the window lose focus, so its not recommended to use\ninput.altDown\ninput.ctrlDown\ninput.shiftDown\n```\n\n### Supported Keys\n\nAll supported mouse buttons are:\n\n```js\nStinput.MouseButtonss = {\n\tleft: 0,\n\tmiddle: 1,\n\tright: 2,\n}\n```\n\nAll supported keyboard keys are:\n\n```js\nStinput.KeyboardKeys = {\n\tbackspace: 8,\n\ttab: 9,\n\tenter: 13,\n\tshift: 16,\n\tctrl: 17,\n\talt: 18,\n\tbreak: 19,\n\tcaps_lock: 20,\n\tescape: 27,\n\tpage_up: 33,\n\tpage_down: 34,\n\tend: 35,\n\thome: 36,\n\tleft_arrow: 37,\n\tup_arrow: 38,\n\tright_arrow: 39,\n\tdown_arrow: 40,\n\tinsert: 45,\n\tdelete: 46,\n\tspace: 32,\n\tn0: 48,\n\tn1: 49,\n\tn2: 50,\n\tn3: 51,\n\tn4: 52,\n\tn5: 53,\n\tn6: 54,\n\tn7: 55,\n\tn8: 56,\n\tn9: 57,\n\ta: 65,\n\tb: 66,\n\tc: 67,\n\td: 68,\n\te: 69,\n\tf: 70,\n\tg: 71,\n\th: 72,\n\ti: 73,\n\tj: 74,\n\tk: 75,\n\tl: 76,\n\tm: 77,\n\tn: 78,\n\to: 79,\n\tp: 80,\n\tq: 81,\n\tr: 82,\n\ts: 83,\n\tt: 84,\n\tu: 85,\n\tv: 86,\n\tw: 87,\n\tx: 88,\n\ty: 89,\n\tz: 90,\n\tleft_window_key: 91,\n\tright_window_key: 92,\n\tselect_key: 93,\n\tnumpad_0: 96,\n\tnumpad_1: 97,\n\tnumpad_2: 98,\n\tnumpad_3: 99,\n\tnumpad_4: 100,\n\tnumpad_5: 101,\n\tnumpad_6: 102,\n\tnumpad_7: 103,\n\tnumpad_8: 104,\n\tnumpad_9: 105,\n\tmultiply: 106,\n\tadd: 107,\n\tsubtract: 109,\n\tdecimal_point: 110,\n\tdivide: 111,\n\tf1: 112,\n\tf2: 113,\n\tf3: 114,\n\tf4: 115,\n\tf5: 116,\n\tf6: 117,\n\tf7: 118,\n\tf8: 119,\n\tf9: 120,\n\tf10: 121,\n\tf11: 122,\n\tf12: 123,\n\tnumlock: 144,\n\tscroll_lock: 145,\n\tsemicolon: 186,\n\tequal_sign: 187,\n\tcomma: 188,\n\tdash: 189,\n\tperiod: 190,\n\tforward_slash: 191,\n\tgrave_accent: 192,\n\topen_bracket: 219,\n\tback_slash: 220,\n\tclose_braket: 221,\n\tsingle_quote: 222,\n}\n```\n\n## Miscs\n\nSome extra properties you can set to StInput instance:\n\n### input.preventDefaults\n\nIf true, will prevent defaults on all events it register to (defaults to false).\n\n### input.enableMouseDeltaWhileMouseWheelDown\n\nIf true, will disable the default mouse-wheel-down action to allow getting mouse delta while users hold down the mouse wheel (defaults to true).\n\n### input.disableContextMenu\n\nIf true, will disable the right-click context menu (defaults to true).\n\n### input.resetOnFocusLoss\n\nIf true, will reset all internal states when element / window loses focus (defaults to true).\nThis property is described later in details.\n\n## Handling Focus Loss\n\nBy default, whenever the browser window loses its focus, StInput will reset all its internal state.\n\nThis behavior is important to prevent keys from getting \"stuck\" in down state due to browsers limitations (for example, if you hold down a key, make the window lose focus, and then release the key outside - you won't get the keyup event and the key will remain stuck).\n\nHowever, if you wish to override this behavior, set `resetOnFocusLoss` to false:\n\n```js\n// will not reset state on focus loss.\ninput.resetOnFocusLoss = false;\n```\n\n## License\n\nStInput is distributed under the permissive MIT license and may be used for any purpose, commercial included. ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronenness%2Fstinput","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fronenness%2Fstinput","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronenness%2Fstinput/lists"}