I'm using Linux console and I would like to do a program which outputs random characters until ESC is pressed. How can I make such a keyboard handler?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
getch() from Curses library perhaps? Also, you will need to use notimeout() to tell getch() not to wait for next keypress.
change the tty settings for one key press:
In this program u will only need to press
enter
afteresc
char,becausegetchar()
is a blocking function. Also u may remove or decrease sleep time for child process as ur need.The line discipline for a terminal device often works in canonical mode by default. In this mode, the terminal driver doesn't present the buffer to userspace until the newline is seen (Enter key is pressed).
You can set the terminal into raw (non-canonical) mode by using
tcsetattr()
to manipulate thetermios
structure. Clearing theECHO
andICANON
flags respectively disables echoing of characters as they are typed and causes read requests to be satisfied directly from the input queue. Setting the values ofVTIME
andVMIN
to zero in thec_cc
array causes the read request (fgetc()
) to return immediately rather than block; effectively polling stdin. The call tofgetc()
will returnEOF
if a character is not available in the stream.Note: This code omits error checking for simplicity.