Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 26723
  • Last Modified:

to get the ascii value of any key

How can i get the ascii value of any key such as up and down arrow keys or esc key etc. in c language.
0
ashwini_mohan
Asked:
ashwini_mohan
1 Solution
 
ashwini_mohanAuthor Commented:
how can i get the ascii value of any key such as Enter key in c programming. i want this solution with sample program.
0
 
brettmjohnsonCommented:
#include <stdio.h>
#include <curses.h>

int main(int argc, char **argv)
{
  int ch;
  int gotCtlC = 0;

  /* Put the terminal in raw mode - this avoids input processing */
  initscr();  cbreak(); raw();

  printf("This prints hexadecimal ascii codes of keys typed\r\n");
  printf("To exit, hit Control-C twice in a row.\r\n");   fflush(stdout);

  while ((ch = getch()) != EOF) {
    if ((ch == '\r') || (ch == '\n'))
      printf("\r\n");
    printf(" %02X  ", ch); fflush(stdout);

    /* control-c twice in a row gets you out */
    if (ch == 03) {
      if (gotCtlC++) break;
    }
    else
      gotCtlC = 0;
  }

  noraw();
  nocbreak();
  return 0;
}
0
 
ankuratvbCommented:
If you're using Borlands Compilers(TurboC,BorlandC)

You can use the getch() function from conio.h

#include<conio.h>
#include<stdio.h>
int main()
{
 char ch;
 ch=getch();
 //To print the ascii value,simply use:
 printf("%d",ch);
 //To process it,use something like this
 switch(ch)
 {
   case 13://Enter key
                 break;
   case 27://Escape key
                 break;
   case 75://Left arrow
                 break;
   case 77://Right arrow
                 break;
   case 72://Up arrow
                 break;
   case 80://Down arrow
                 break;
 }
}
0
 
realraelCommented:

you'd love this link! - rael

http://www.jimprice.com/jim-asc.htm
0
 
mikem_2auCommented:
#include <windows.h>
#include <stdio.h>

/* Esc to quit */

int main()
{

      HANDLE  stin = GetStdHandle(STD_INPUT_HANDLE);
      INPUT_RECORD buffer_read_data;
      DWORD events_read;


      for(;;)
      {

            FlushConsoleInputBuffer(stin);
            ReadConsoleInput( stin, &buffer_read_data, 1, &events_read );

            if ( buffer_read_data.EventType == KEY_EVENT )
                  if ( buffer_read_data.Event.KeyEvent.bKeyDown == TRUE )
                  {
                        if ( buffer_read_data.Event.KeyEvent.wVirtualScanCode == 0x1 )
                              break;
                        else
                              printf("%x\n", buffer_read_data.Event.KeyEvent.wVirtualScanCode );

                  }

      }

      return 0;
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now