|
| ||||||||||
| Tags: c language, function, kbhit |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| C : Use of kbhit()
|
|
#2
| ||||
| ||||
| Use of kbhit()
To check or test the keystroke that whether the keystrokes are currently available or not the kbhit() function can be used. To obtain the keystroke getch(0 or getche(0 function can be used, whenever the one of the keystroke can be available. Until a keystroke is available the kbhit() function can be called continuously with a stand-alone program. You have to note that loops involving the kbhit() function can not be recommended in multitasking systems. The following is the syntax of the kbhit() function : #include <conio.h> int kbhit( void );
__________________ The FIFA Manager 2009 PC Game |
|
#3
| ||||
| ||||
| C : Use of kbhit()
The following sample program describes you about the kbhit() function : #include <stdio.h> #include <conio.h> int main(void) { double amt; amt = 0.20; cprintf(" Printing 6-percent tax table\n\r "); cprintf(" Press a key to stop.\n\n\r "); do { cprintf(" Amount : %f, Tax : %f\n\r ", amount, amount * 0.06); if(kbhit()) break; amt = amt + 0.40; } while(amt < 200.0); return 0; } |
|
#4
| ||||
| ||||
| kbhit() function : Program
The following program demonstrate that program loops until a any key is pressed or a count can be exceeded : #include <stdio.h> #include <conio.h> void main() { unsigned long j; printf( " Program looping. Press any key.\n " ); for( j = 0; j < 20000; j++ ) { if( kbhit() ) { getch(); break; } } } |
|
#5
| ||||
| ||||
| Re: C : Use of kbhit()
Sample Example : #include <conio.h> #include <stdio.h> int main( void ) { while( !_kbhit() ) _cputs( "Hit me!! " ); printf( "\nKey struck was '%c'\n", _getch() ); } Output : Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Key struck was 'q' |