#🔒 How to change the keys of inputs instead pressing enter in python

25 messages · Page 1 of 1 (latest)

cosmic grail
#

Hi i was wondering to change the keys of inputs for example
instead i press enter i can change it from enter to 2 or any other key on the keyboard and thanks✨

tender pelicanBOT
#

@cosmic grail

Python help channel opened

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.

vestal orbit
tender pelicanBOT
#
Pasting large amounts of code

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.

cosmic grail
lapis tree
#

Do you mean changing the key that submits what an user types to input()?

cosmic grail
#

ya!

#

that what am trying to say

#

is possible?

unreal gale
#

This is surprisingly difficult because your terminal doesn't send the input to the python process until an entire line is entered

cosmic grail
#

aw

vestal orbit
#

Its possible you can alter you keyboard remappings (of the device) to what you want.

unreal gale
#

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))

cosmic grail
#

ah i see

#

alr guys thanks for the information i appreciate this

unreal gale
#

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: "))

cosmic grail
#

the one it says (1)) ?

unreal gale
#

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

tender pelicanBOT
#
Python help channel closed

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.