Solved

to get the ascii value of any key

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 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

10 Experts available now in Live!

Get 1:1 Help Now