Advertisement
dmilicev

keyboard_ascii_codes_with_kbhit()_v1.c

Nov 7th, 2019
319
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None
  1. /*
  2.  
  3.     keyboard_ascii_codes_with_kbhit()_v1.c
  4.  
  5.     While no key is pressed, it reports that no key is pressed.
  6.  
  7.     When a key is pressed, it displays its ASCII code.
  8.  
  9.     Also try function keys and key combinations
  10.     with Shift, Control and Alt keys.
  11.  
  12.  
  13.     Note that we use new functions _getch() and _kbhit()
  14.     instead of old functions        getch() and   kbhit ().
  15.  
  16.     _kbhit()
  17.  
  18.     https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/kbhit?view=vs-2019
  19.  
  20.     _getch()
  21.  
  22.     https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2019
  23.  
  24.  
  25.     You can find all my C programs at Dragan Milicev's pastebin:
  26.  
  27.         https://pastebin.com/u/dmilicev
  28.  
  29.         https://www.facebook.com/dmilicev
  30.  
  31. */
  32.  
  33.  
  34. #include <stdio.h>
  35. #include <conio.h>              // for kbhit()
  36. #include <time.h>               // for clock()
  37.  
  38. #define WAIT_TIME 100
  39.  
  40.  
  41. // wait (do nothing) for time in milliseconds ( 1000 for 1 second )
  42. void wait(int milliseconds)
  43. {
  44.     clock_t start_time = clock();   // get start time
  45.  
  46.     // looping (do nothing) till required time is not acheived, ; means do nothing
  47.     while ( clock() < start_time + milliseconds );
  48. }
  49.  
  50.  
  51. int main(void)
  52. {
  53.     int i = 0, ch;              // ch is pressed key that we will analyze
  54.     int end = 0;                // = 0 because it's not the end of the program yet
  55.  
  56.     while(!end)
  57.     {
  58.         while( !_kbhit() )      // rotates in that loop until some key is pressed
  59.         {
  60.             if( i<100 )         // we only show i that something would happen
  61.                 i++;            // on the screen while no key was pressed
  62.             else
  63.                 i=0;
  64.  
  65.             printf("\n No key was pressed. i = %d \n", i );
  66.  
  67.             wait(WAIT_TIME);
  68.         }
  69.  
  70.         ch = _getch();              // Now a key is pressed and we read it.
  71.  
  72.         switch( ch ) {              // We analyze which key is pressed:
  73.  
  74.             case 27 :               // ESC for end of program
  75.                     printf("\n Pressed is ESCAPE key to end program \t\t %d \t %c \n", ch, ch);
  76.                     end = 1;        // to end program
  77.                     break;
  78.  
  79.             case  0 :               // if a function key with a leading 0 is pressed
  80.                     ch = _getch();  // let's empty that 0 and show which key is pressed
  81.                     printf("\n Pressed is function key with a leading   0 \t %d \t %c \n", ch, ch);
  82.                     break;
  83.  
  84.             case 224 :              // if a function key with a leading 224 is pressed
  85.                     ch = _getch();  // let's empty that 224 and show which key is pressed
  86.                     printf("\n Pressed is function key with a leading   224 \t %d \t %c \n", ch, ch);
  87.                                 break;
  88.             default :               // show all other pressed keys
  89.                     printf("\n Pressed is key \t\t %d \t %c \n", ch, ch);
  90.                     break;
  91.         } // switch( ch )
  92.  
  93.         wait(20*WAIT_TIME);         // pause for better view
  94.  
  95.     } // while(!end)
  96.  
  97.     return 0;
  98. }
Advertisement
RAW Paste Data Copied
Advertisement