Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

interactive character input 1

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
0
0
US
I searched the forum on this but didn't find what I was looking for. I know this has been discussed before but I couldn't find anything. So anyway, what is the best way to read a character from stdin without having to hit 'return'?

#include <stdio.h>

main(int argc, char** argv)
{
char iochar;
while((iochar=getchar())!=EOF)
putchar(iochar);

return(0);
}

In the above example you have to hit 'return' because the input is buffered. I just want to hit 'a' or something and have the program react immediately w/o hitting 'return'...possible?

Thanks,
bitwise
 
Not in standard C. You have to do stuff to the terminal that C doesn't cover.

If you're in a DOS environment, see if your compiler supports kbhit(). If you're in a *nix environment, you can do it with select() and using the function in <termios.h> to adjust the terminal appropriately beforehand. I posted an article recently with sample code for doing this. I think it's labeled &quot;Key press w/o return.&quot;

Russ
bobbitts@hotmail.com
 
Like rbobbitt mentioned, kbhit() can be used to wait for a keypress and perform background processing. Then all you gotta do is call getch() to retrieve the key. As mentioned, this is OS / compiler specific and not ANSI code. Untested code follows:

#include <stdio.h>
#include <conio.h>

int main(){

int a;
while(!kbhit())
{ /* Do stuff while waiting for user input */ ;}

a = getch(); /* Get key press and */
if (a == 0 ) { a = getch() + 256; } /* Add 256 if extended */
switch ( a )
{
case 27: /* If escape key */
printf( &quot;Escape key. Exiting...\n&quot; );
return 0;
case 315: /* If F1 key */
printf(&quot;Help me!\n&quot;);
break;
case 328: /* If Up Arrow key */
printf(&quot;Up Arrow key\n&quot;);
break;
case 336: /* If Down Arrow key */
printf(&quot;Down Arrow key\n&quot;);
break;
case 333: /* If Right Arrow key */
printf(&quot;Right Arrow key\n&quot;);
break;
case 331: /* If Left Arrow key */
printf(&quot;Left Arrow key\n&quot;);
break;
default:
printf(&quot;Key code is --> %d\n&quot;, a);
}
return 0; /* just exit anyways */
}


Kim_Christensen@telus.net
 
This is on UNIX not windows so I can't use kbhit(). I can however use the version rbobbitt wrote in his post &quot;Key press w/o return&quot;. I managed to find that post and the sample program given works quite nicely. I'll have to adapt it slightly but not a problem. Thank you both for your input.

bitwise
 
Hey bitwise why not try conio.h for it.
ch = getche();
printf(&quot;%c&quot;,ch);

or use getch()
ch = getch();
printf(&quot;%c&quot;,ch);

if you are trying to seek normal alphabets this will suffice. If u need scan code lemme know. I think these both will work on Unix machine.

Regards,
SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top