{"id":16138245,"url":"https://github.com/euclidianace/lgetchar","last_synced_at":"2025-04-06T17:24:33.910Z","repository":{"id":172595352,"uuid":"242069048","full_name":"euclidianAce/lgetchar","owner":"euclidianAce","description":"A small wrapper around getch and getchar for lua.","archived":false,"fork":false,"pushed_at":"2021-11-25T12:44:02.000Z","size":55,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T15:35:40.558Z","etag":null,"topics":["lua"],"latest_commit_sha":null,"homepage":"","language":"C","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/euclidianAce.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-02-21T06:20:19.000Z","updated_at":"2021-11-25T12:44:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"2334887e-242a-4fa4-9db5-6f79674d4825","html_url":"https://github.com/euclidianAce/lgetchar","commit_stats":null,"previous_names":["euclidianace/lgetchar"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euclidianAce%2Flgetchar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euclidianAce%2Flgetchar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euclidianAce%2Flgetchar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euclidianAce%2Flgetchar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/euclidianAce","download_url":"https://codeload.github.com/euclidianAce/lgetchar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247519489,"owners_count":20951991,"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":["lua"],"created_at":"2024-10-09T23:33:11.559Z","updated_at":"2025-04-06T17:24:33.879Z","avatar_url":"https://github.com/euclidianAce.png","language":"C","readme":"# lgetchar\nI just wanted a way to detect arrow key presses in Lua.\nAfter scouring the internet and reading very sparse man pages, this is what I've come up with.\n\n# Teal\n## (i.e. What are all those .tl files?)\nThis module is written with [Teal](https://github.com/teal-language/tl), a typed dialect of Lua.\n\n# Installation\nlgetchar is avaliable on luarocks\n```sh\nluarocks install lgetchar --dev\n```\n\nOr you can build from source by cloning the repo and running `make` (hopefully)\n\n# Usage\nlgetchar is separated into 3 modules:\n - `raw`: the C component that controls the terminal, you shouldn't need to use this directly, but hey, it's there if you want.\n - `wrapper`: Lua wrappers around the `raw` component for more convenience. Again, probably don't need this, but could be useful.\n - `lgetchar`: The main module.\n\n### `raw`\n - `raw.setup(): boolean, string`  `raw.nonBlockingSetup(): boolean, string`\n   - Turn off canonical mode and echo. Returns whether the operation succeeded and an error message if any.\n   - These functions will probably only error when stdin is not a tty or an `ioctl` call fails\n - `raw.restore(): boolean, string`\n   - Re-enable canonical mode and echo. Returns whether the operation succeded and an error message if any.\n - `raw.getChar(noSetup: boolean): integer, string`\n   - `read`s from stdin, returns the character read as an integer or nil and an error message. This function automatically calls `setup()` and `restore()`, pass `true` to do these manually.\n - `raw.getCharSeq(len: integer, noSetup: boolean): integer... | (nil, string)`\n   - reads for `len` characters and returns them as integers, or nil and an error message. `noSetup` is the same as `getChar`\n\n### `wrapper`\n - `wrapper.keys: {string:{integer}}`\n   - A map of keys to use in `expect` functions: For example, `keys.up` is the up arrow escape sequence, `{27, 91, 65}`\n - `wrapper.expect(keys: {integer}): integer, string`\n   - calls `raw.getChar` until the result is one of `keys`, forwards any errors\n - `wrapper.expectSeq(keys: {{integer}}): integer, string`\n   - calls `raw.getChar` until the resulting sequence is one of `keys`, forwards any errors\n\n### `lgetchar`\n - `lgetchar.keys: {string:{integer}}`\n   - an alias for `wrapper.keys`\n - `lgetchar.getChar(): string, string`\n   - calls `raw.getChar` and returns the result as a character, or nil and an error message\n - `lgetchar.getCharSeq(len: integer): string, string`\n   - calls `raw.getCharSeq` and returns the result as a string, or nil and an error message\n - `lgetchar.expect(keys: {{integer}}): {integer}, string`\n   - calls `wrapper.expectSeq` and returns which key was caught, or nil and an error message\n\n# Implementation Details\nFor Windows, this is basically a wrapper for `getch()` from conio.h.\nFor \\*nix, this uses termios.h and sys/ioctl.h to change some terminal settings (mainly turning off echo) and calls `getchar()`.\nI still have no idea how portable this is, but it has worked on a few of my machines decently enough.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuclidianace%2Flgetchar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feuclidianace%2Flgetchar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuclidianace%2Flgetchar/lists"}