Click here to Skip to main content
14,334,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
hey,
I am working on an application where I would like to perform some actions on various keystrokes,from my understanding,this is possible by finding out the ascii code of the key pressed.
so Is there any funtion in "c" to determine the ascii code of the key pressed...
I came across certain functions like "getch","getche" and "getchar"but none of them seem to serve my purpose...
const int size=50;
     char name[size];//any character array
     unsigned int char
     for(int i=0;i<size;i++)
 {
     ch=getch();
      scanf("%s",Name[i]);
      if(ch==33)//ascii code of "Esc"
      {
          Name[i]='\0';
      }

     break;
 }
Posted
Updated 10-Apr-13 21:26pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
getch","getche" and "getchar"but none of them seem to serve my purpose...

Actually ALL of them return the ASCII code of the input character (So what is your purpose?).


[Update]
Please note the decimal ESC code is 27. You are using its octal value in a decimal constant (in C an octal constant must be 0-prefixed, that is 033).
Anyway intercepting ESC is tricky (I got 146 on my console application). I suggest you to use 'q' or something else).
[/Update]


By the way, your code looks a bit messy.
   
v3
Comments
Aayman Khalid 11-Apr-13 3:21am
   
well to begin with,I am taking some charater input from user,the size of this input might be different but since you have to define the size of the array,I am designing a situation where if a user presses "Esc" the program stops taking input and puts a '\0' at the end...
Aayman Khalid 11-Apr-13 3:28am
   
I've also added my code...
whenever I press "Esc" the input is erased from the console but the program doesn't terminate which I think should happen...
Aayman Khalid 11-Apr-13 4:16am
   
I've tried with "27" too,the resultis still the same!!!
CPallini 11-Apr-13 4:25am
   
Well I warned you. I'm getting 146 on my application. Why don't you use another character?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100