0

This is the first code

int main()
{
   int ch;
   while(ch)
   {
       ch=getch();
       printf("%d",ch);
       printf("\n");
   }
   return 0;
}

if in the above code I input

up arrow key 
down arrow key 
right arrow key 
left arrow key 

RESPECTIVELY the the outputs are as following

224
72

224
80

224
77

224
75

but if I remove the LOOP from the code ie

int main()
{
   int ch;
   ch=getch();
   printf("%d",ch);
   printf("\n");
   return 0;
}

and input

up arrow key
down arrow key
right arrow key
left arrow key

RESPECTIVELY then the outputs are as follows

224
224
224
224

From where

224

is coming in the first code and after removing the LOOP where the following numbers are gone

72
80
77
75
alk
  • 69,737
  • 10
  • 105
  • 255

2 Answers2

3

getch() fetches the next character from the console, but some keys like up arrow key etc. produce two successive "characters". Thus, when you remove the loop, you always only read the first character, but not the second one.

Generally, reading and handling special keys such as the cursor keys is very system specific and not defined in the C language - you would usually use an additional library (e.g. ncurses on Unix) to handle these.

As a last resort, you could also check if the first call to getch() returns the value 224 and in that case call it again, something like this:

int key = getch();
if (key == 224) {
    key = 0x100 + getch();   // arrow keys will have values > 256
}

...

switch(key) {
   case 0x142 : printf("Key up"); break;
   ...
}

But note that this is completely unportable and very system dependent. You should at least encapsulate it in a separate function and define some constants for the various keys.

See also

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • if inputs like arrow keys produce two successive characters then how to input them from the keyboard. I mean to say up arrow key==224 or up arrow key==72 –  Jan 14 '14 at 12:42
  • 1
    you have to read the first key , then if it was 224 you can say that it's a functional key , then you have to read the second to decide which function it denote – Mostafa 36a2 Jan 14 '14 at 13:00
0

224 denoted that it's a functional key , so you have to get the next byte then you'll decide if the next byte was 72 then its Up arrow and so on

here is an example

int a,b;
while(a=getch())
{
    if(a==224)
    {
        b=getch();
        switch(b)
        {
             case 72:puts("Up arrow");break;
             case 80:puts("Down arrow");break;
             case 75:puts("Left arrow");break;
             case 77:puts("Right arrow");break;
         }
     }
 }

your second code will output 224 and stop , because no loop in there , try this :

a=getch(),b=getch();
printf("%d %d\n",a,b);
Mostafa 36a2
  • 157
  • 1
  • 15
  • you code works even when you ... switch(a) and remove the "if" condition and remove the b=getch(); –  Jan 14 '14 at 12:54
  • No , this is not going to work because 72 is the ASCII value for 'H' and 80 for 'P' and so on , you have to handle the 224 to ensure that the following is the Arrow ID – Mostafa 36a2 Jan 14 '14 at 12:56
  • now try typing 'H' or 'P' or 'K' or 'M' and you will see what I'm talking about ;) – Mostafa 36a2 Jan 14 '14 at 13:01
  • you are also right my friend... H and UP ARROW KEY both are printing UP ARROW –  Jan 14 '14 at 13:04
  • and that's why you have to write the `if` statement for the value `224` then write the `switch` :) – Mostafa 36a2 Jan 14 '14 at 13:05
  • but why without the "if" statement the code if working for the arrow keys...ie why it is ignoring 224 and taking only 72 and hence works for both UP KEY and H......why? –  Jan 14 '14 at 13:06
  • Because it will Discard the first byte `224` and handle the arrow Id just like any other key pressed [i.e. it can't decide whether it's an arrow or an Alphabetical character] and that's why they make the first byte unique`224` so you can ensure that the next byte is not a normal ASCII char – Mostafa 36a2 Jan 14 '14 at 13:09
  • but it is discarding the 224 in case of while loop only ...ie.... if I remove while loop in your program ie ch=getch()...if(ch==72)...printf("up key"); then it is not printing up key...ie it has not discarded the 224....but in case of loop why it discard it.. –  Jan 14 '14 at 13:14
  • please tell me how you highlight a letter in comment box...then I will type both the modified codes of yours –  Jan 14 '14 at 13:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45197/discussion-between-user3180902-and-mostafa-36a2) –  Jan 14 '14 at 13:19
  • just use the same key of ~ [wothout pressing shift] ,any way without a loop , the `224` will not match the values in the `if`s [72,77,75,80] and so it will not print any thing, in a loop this [unmatch cycle ] will pass and the the arrow Id will match one of the values [72,77,75,80] in the second cycle. when I said discard I ment pass the cycle without doing any thing . – Mostafa 36a2 Jan 14 '14 at 13:19