Solved

to get the ascii value of any key

Posted on 2004-08-15
5
26,702 Views
Last Modified: 2011-08-18
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
Comment
Question by:ashwini_mohan
5 Comments
 

Author Comment

by:ashwini_mohan
ID: 11805568
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
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 250 total points
ID: 11806017
#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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11806966
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
 
LVL 3

Expert Comment

by:realrael
ID: 11807141

you'd love this link! - rael

http://www.jimprice.com/jim-asc.htm
0
 

Expert Comment

by:mikem_2au
ID: 11849331
#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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question