#🔒 How to change the keys of inputs instead pressing enter in python
25 messages · Page 1 of 1 (latest)
@cosmic grail
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!paste
You are going to have to give us a little more details on what exactly you are doing and what the issue is. Please provide us the code and the problem you are encountering. Please refer to this message >#1281772780576047114 message< so that we can fix you issue efficiently.
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
ok so basically when you run your code isn't you have to press enter on ur keyboard to change the next page
all i want to know how i can change that to another botton in ur keyboard
Do you mean changing the key that submits what an user types to input()?
This is surprisingly difficult because your terminal doesn't send the input to the python process until an entire line is entered
aw
Its possible you can alter you keyboard remappings (of the device) to what you want.
e.g. this reads exactly 1 character from the terminal input (that's what stdin means), but it only happens after you hit enter: ```py
import sys
print(sys.stdin.read(1))
It's definitely possible (it's been done before), but I think it will involve a 3rd party library
@cosmic grail Actually, it turns out you can disable it with just the standard library: ```py
import sys
import tty
tty.setcbreak(sys.stdin) # disables line buffering
print(sys.stdin.read(1))
I forgot to add, you probably want to return the terminal to normal afterwards
Or something like this perhaps? (based on this)```py
import sys
if sys.platform == "nt":
# Windows
import msvcrt
def read_char(prompt):
print(prompt, end="")
return msvcrt.getch()
else:
# MacOS / Linux
import termios
def read_char(prompt):
print(prompt, end="")
old_settings = termios.tcgetattr(0)
try:
tty.setraw(0)
char = sys.stdin.read(1)
finally:
termios.tcsetattr(0, termios.TCSADRAIN, old_settings)
return ch
print(read_char("Enter a character: "))
may u point where is enter botton in the code here?
the one it says (1)) ?
it has nothing to do with the enter key, it disables line buffering (= your terminal not sending input until enter is typed)
If you want to read input until a specific character, you can make a function like this: ```py
def input_until(char):
text = ""
while True:
current = sys.stdin.read(1)
if current == char:
break
text += current
return text
If you don't mind installing packages, I would recommend using this instead: https://pypi.org/project/readchar/
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.