how to implement 'press n within 5 secs to terminate..' type of code
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how to implement 'press n within 5 secs to terminate..' type of code
greetings everybody!
I was trying to write a piece of code in C (similar to the messages found during bootup in Linux sometimes.)
The program should display a message like-
'Proceeding to phase2...(press 'n' within 5 secs to exit)'
if the user types in 'n' before 5 secs are up, the program should
exit,else after 5 secs, the program should go on to the next phase.
I am trying to use alarm(5) and loop type of mechanism to implement this
but the problem is:
To take an input from the user we have to use input-fns like getchar(),
which block for user-input and if the user doesn't input anything,the
loop continues forever and the program doesn't move forward.
Any ideas how to implement this ,mates?
(forgive me if this proves to be childishly simple though..sometimes,
during programming, your mind really gets blocked misses the little things.
regards,
aru
you will need to play with termios.
tcsetattr tcgetattr.
look up man pages.
here's a shell script (messy) to give you a clue to values to set.
the stty bits are basically the same as what you need for tcsetattr
Code:
#!/bin/ksh
trap cleanup EXIT HUP TERM
save=`stty -g`
count=0
cleanup()
{
stty $save
echo '\033c'
}
get_key()
{
2>/dev/null dd bs=10 count=1
}
ttyset()
{
# if min is zero it won't wait
# if min is 1 it waits for 1 char etc.
#
#
# if it's zero it doesn't wait if it's non-zero
# it returns the minimum amount
stty cs8 -icanon igncr \
min 1\
time 0\
-echo isig -xcase \
-inpck -opost
}
ESC=
echo '\033c'
echo "\n\nPress a key (q = quit)"
ttyset
while true ;do
x=`get_key`
count=`expr $count + 1`
[ "$x" = q ] && exit
# [ "$x" = $ESC ] && break
echo '\033[H' "$count\033[10H\033[1K got"
print -n "$x "|od -c
done
print -n "$x"|od -c
My solution would be:
Use select and add stdin to the rdfds. Select will timeout if nothing has happened on the filedescriptor, else you can check the input with e.g. read.
To just handle a keypress instead of having to wait for <enter> as well, have a look at cfmakeraw.
Originally posted by vivekr
[B]Hai buddy,
I think this piece of code will work.
.....
.....
I did this using MS Visualstudio-
Although it's not stated that the code should run under Linux, I assume that that's more than likely the case on this forum.
I could not compile the code on my RH8 box.
1) I could not find conio.h in the include files on my RH8 box (and the compiler complains)
2) man kbhit does not give results on RH8 or Slack10 (and the linker complains as well)
1. The problem with "conio.h" and "kbhit()" is that it's specific to DOS (not even Windows, unless you're in a command prompt).
2. "select()" is an EXTREMELY powerful, EXTREMELY useful Unix call for waiting on the first of any number of possible inputs: for example, a mouse click OR a key press OR a socket read OR a timeout. You can find more here:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.