{"id":13545699,"url":"https://github.com/Nearoo/pygame-text-input","last_synced_at":"2025-04-02T16:30:38.446Z","repository":{"id":14040679,"uuid":"73417284","full_name":"Nearoo/pygame-text-input","owner":"Nearoo","description":"a small module that enables you to input text with your keyboard using pygame","archived":false,"fork":false,"pushed_at":"2023-08-08T19:26:27.000Z","size":69,"stargazers_count":141,"open_issues_count":5,"forks_count":64,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-03T18:02:29.514Z","etag":null,"topics":["font","input","keyboard-input","pygame","text"],"latest_commit_sha":null,"homepage":"","language":"Python","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/Nearoo.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}},"created_at":"2016-11-10T20:11:15.000Z","updated_at":"2025-02-03T20:15:00.000Z","dependencies_parsed_at":"2024-01-16T17:02:19.109Z","dependency_job_id":"2f109775-6934-4f56-96d9-bf0d5a9e11a8","html_url":"https://github.com/Nearoo/pygame-text-input","commit_stats":{"total_commits":41,"total_committers":10,"mean_commits":4.1,"dds":0.3414634146341463,"last_synced_commit":"6fc3c61bcd6c171836ec94ba5968712f376209ab"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nearoo%2Fpygame-text-input","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nearoo%2Fpygame-text-input/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nearoo%2Fpygame-text-input/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nearoo%2Fpygame-text-input/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nearoo","download_url":"https://codeload.github.com/Nearoo/pygame-text-input/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246850722,"owners_count":20844135,"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":["font","input","keyboard-input","pygame","text"],"created_at":"2024-08-01T11:01:10.251Z","updated_at":"2025-04-02T16:30:38.147Z","avatar_url":"https://github.com/Nearoo.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Pygame Text Input Module\n\nThis module provides two utility classes that simplify entering text using pygame. The classes are:\n* `TextInputVisualizer` which can be used to both manage and draw text input. Simply pass all events returned by `pygame.event.get()` to it every frame, and blit its `surface` attribute on the screen.\n*  `TextInputManager` that can be used to just manage inputted text, with no visual aspect. Used by `TextInputVisualizer` behind the scenes.\n\n\n\n![Example of module in use](https://i.imgur.com/h7a64Y2.gif)\n\n# Installation\n\nSimplest way is using pypi:\n\n```\npython3 -m pip install pygame-textinput\n```\n\n# Usage\n\n## `TextInputVisualizer`\n\n### Example\n\nAll arguments to the constructor are optional. Once constructed, call `update` every frame with all pygame events as an argument, then blit it's `surface` field to the screen, like so:\n\n```python\nimport pygame_textinput\nimport pygame\npygame.init()\n\n# Create TextInput-object\ntextinput = pygame_textinput.TextInputVisualizer()\n\nscreen = pygame.display.set_mode((1000, 200))\nclock = pygame.time.Clock()\n\nwhile True:\n    screen.fill((225, 225, 225))\n\n    events = pygame.event.get()\n\n    # Feed it with events every frame\n    textinput.update(events)\n    # Blit its surface onto the screen\n    screen.blit(textinput.surface, (10, 10))\n\n    for event in events:\n        if event.type == pygame.QUIT:\n            exit()\n\n    pygame.display.update()\n    clock.tick(30)\n```\n\n## Arguments:\n\nAll arguments are optional. \n\nArgument | Description\n---|---\nmanager | The `TextInputManager` used to manage the input\nfont_object | The `pygame.font.Font` object used for rendering\nantialias |  whether to render the font antialiased or not\nfont_color | color of font rendered\ncursor_blink_interval | The interval of the cursor blinking, in ms\ncursor_width | The width of the cursor, in pixels\ncursor_color | The color of the cursor\n\n## Fields\n\nAll arguments above are also fields that can be accessed and modified on the fly, e.g. like this:\n\n```python\ntextinput.cursor_width = 12\ntextinput.value = \"Hello, World!\"\nprint(textinput.font_color)\n```\n\nField | Description\n--- | ---\nvalue | (string) The text entered so far\nsurface | The `pygame.Surface` object with entered text \u0026 cursor on it and a transparent background. **Cannot be set.**\ncursor_visible | (bool) wether the cursor is currently visible or not. Flips every `cursor_interval` ms as long as `update` is called continuously. \n\n## Methods\n\nMethod | Description\n--- | ---\nupdate(events) | Call this method every frame for as long as input should be processed and `cursor_visible` should be updated.\n\n### Notes on the newer version:\n* You have to watch for \"return\" presses by the user yourself, e.g. like this:\n\n```python\nfor event in events:\n    ...\n    if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:\n        print(\"Oooweee\")\n```\n\n* Contrary to the old version, key-stroke repeats are not manually introduced anymore, since they can now be enabled within `pygame` directly:\n\n```python\npygame.key.set_repeat(200, 25) # press every 50 ms after waiting 200 ms\n```\n\n\nThis new version has also been optimized such that you can **modify any fields on the fly** and the actual surface will only re-render if you access it using `textinput.surface` - and only if you actually modified any values.\n\n\n# `TextInputManager`\n\nIf you prefer to draw the text on the screen yourself, you can use `TextInputManager` to only manage the string that has been typed so far.\n\nLike `TextInputVisualizer`, you feed its `update` method all events received by `pygame.event.get()` which you want it to process. `TextInputVisualizer` does this for you inside its `update` method if you pass it a `TextInputManager`.\n\n## Arguments:\nArgument | Description\n---|---\ninitial | The initial value (text)\nvalidator | A function taking a `string` and returning a `bool`. Every time an input modifies the value, this function is called with the modified value as an argument; if the function returns `True`, the input is accepted, otherwise the input is ignored.\n\nSo say you want to only allow input to up to 5 letters, you could do that with\n\n```python\nmanager = TextInputManager(validator=lambda input: len(input) \u003c= 5)\n```\n\n## Fields\nField | Description\n---|---\nvalue | The inserted value so far. When change, `cursor_pos` is kept as far as possible.\ncursor_pos | The position of the cursor. `0` is before the first character, `len(manager.value)` the position after the last. Values outside this range are clamped.\n\n\n# More Examples\n\n## Most features\n\n```python\nimport pygame\nimport pygame.locals as pl\n\npygame.init()\n\n# No arguments needed to get started\ntextinput = TextInputVisualizer()\n\n# But more customization possible: Pass your own font object\nfont = pygame.font.SysFont(\"Consolas\", 55)\n# Create own manager with custom input validator\nmanager = TextInputManager(validator = lambda input: len(input) \u003c= 5)\n# Pass these to constructor\ntextinput_custom = TextInputVisualizer(manager=manager, font_object=font)\n# Customize much more\ntextinput_custom.cursor_width = 4\ntextinput_custom.cursor_blink_interval = 400 # blinking interval in ms\ntextinput_custom.antialias = False\ntextinput_custom.font_color = (0, 85, 170)\n\nscreen = pygame.display.set_mode((1000, 200))\nclock = pygame.time.Clock()\n\n# Pygame now allows natively to enable key repeat:\npygame.key.set_repeat(200, 25)\n\nwhile True:\n    screen.fill((225, 225, 225))\n\n    events = pygame.event.get()\n\n    # Feed it with events every frame\n    textinput.update(events)\n    textinput_custom.update(events)\n\n    # Get its surface to blit onto the screen\n    screen.blit(textinput.surface, (10, 10))\n    screen.blit(textinput_custom.surface, (10, 50))\n\n    # Modify attributes on the fly - the surface is only rerendered when .surface is accessed \u0026 if values changed\n    textinput_custom.font_color = [(c+10)%255 for c in textinput_custom.font_color]\n\n    # Check if user is exiting or pressed return\n    for event in events:\n        if event.type == pygame.QUIT:\n            exit()\n\n        if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:\n            print(f\"User pressed enter! Input so far: {textinput.value}\")\n\n    pygame.display.update()\n    clock.tick(30)\n    \n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNearoo%2Fpygame-text-input","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNearoo%2Fpygame-text-input","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNearoo%2Fpygame-text-input/lists"}