Solved

to get the ascii value of any key

Posted on 2004-08-15
5
26,712 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

734 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