Wednesday, April 27, 2016

C program to use Arrow keys.

This program explains how to use Arrow keys in C program.


#include<stdio.h>
int main()
{
 int chr1, chr2;
 printf("Press any arrow key...\n");
 chr1 = getch();
 if (chr1 == 0xE0) //to check scroll key interrupt
 {
  chr2 = getch();  //to read arrow key
  switch(chr2)
  {
   case 72: printf("UP ARROW KEY PRESSED\n");
      break;
   case 80: printf("DOWN ARROW KEY PRESSED\n");
      break;
   case 75: printf("LEFT ARROW KEY PRESSED\n");
      break;
   case 77: printf("RIGHT ARROW KEY PRESSED\n");
      break;
   default: printf("OTHER KEY PRESSED: %d %d\n", chr1, chr2);
      break;
  };
 }
 return 0;
}

Output of program

Press any arrow key...
UP ARROW KEY PRESSED

3 comments:

  1. Replies
    1. Pl. share type of platform you are using to run this program. Also, share what error are you getting while running this program.

      For your information this program is running in my system with Windows 7, 32 bit version.

      Delete