1

For practice and fun, I am working on a small Python chat client which connects to an online service. The chat allows the other users to see when the main user is typing. I'd like the other users to see when the program's user is in the process of entering something to send to them.

>>>Stranger: Hello, user.
>>>Hello, how are y...

This would perhaps set the variable hasSomethingEntered to True because something is being typed whereas

>>>Stranger: Hello, user.
>>>

would set the variable hasSomethingEntered to False because the user has not yet typed anything into the input box. This variable would then be passed into a separate thread which communicates with the server and lets it know that the client's user is typing.

Right now I am using Python's input command to get the user's chat entries. Is it possible to do what I wish to do using Python? If not, can you recommend an alternative way or language that I can use to accomplish the same goal?

I hope it is clear what I am trying to accomplish here and thank you in advance.

Jerro39
  • 113
  • 4
  • 2
    You should check out the curses module for python (it doesn't work on windows I think) http://docs.python.org/3/library/curses.html – MWB Jul 30 '13 at 21:52
  • Thanks. This is really great because it has a lot of what I need. Unfortunately, I am trying to make this client usable on both Windows and unix systems so I need to find some sort of alternative that will work on both. – Jerro39 Jul 31 '13 at 18:18

1 Answers1

0

You can read one character at a time, making hasSomethingEntered true after the first character and false after the end of a line. Unfortunately, this is platform-dependent.

Windows:

import msvcrt
one_character= msvcrt.getch()

Unix:

import sys, tty
tty.setraw(sys.stdin.fileno())
one_character= sys.stdin.read(1)

In the later case you will most probably want to save and restore sys.stdin's mode with

import sys, termios
previous_mode= termios.tcgetattr( sys.stdin.fileno() )

and

import sys, termios
termios.tcsetattr(sys.stdin.fileno(),termios.TCSADRAIN, previous_mode )

, respectively.

Mario Rossi
  • 7,651
  • 27
  • 37
  • I like the idea to process the entry one character at a time instead of looking at the string as a whole. I was able to accomplish what I needed using this as well as some information about dynamic printing here: http://stackoverflow.com/questions/2122385/dynamic-terminal-printing-with-python – Jerro39 Jul 31 '13 at 23:45