Solved

to get the ascii value of any key

Posted on 2004-08-15
5
26,696 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 410
How to get time_t value that is a week old 6 163
C++ vs C compilers 13 157
Problem to save 10 147
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now