Monday 1 July 2013

Detecting The Arrow, Page Up & Page Down, Function Keys etc. And Using Ctrl Combinations In C Programming.

I found this function HERE a few months back when I looking how to detect the function keys.
I have used it a couple of times, but never really understood how it worked.
I decided to figure it out and I think I have it. All props to the 'nucleon' & 'hauzer' guys for sharing with noobs like me!

All the keys that have normal ASCII values are easy enough to implement in our codes, but the function, arrow, page. up down keys are a bit trickier.

Pressing the F1 ,F2, Pg Down keys etc. return 2 values. The first one is a value that varies
the other is either a 0 or 224. This function uses that fact to return a unique value.


Here is an example to see how the 2 values are handled in C:
_________________________________________________
#include <stdio.h>
#include <conio.h>

int main()
{
    printf("Press any key. Do it two times!");
    getch();
    getch();
}
__________________________________________________
The above program requires 2 key presses to end.
If you press one of the ascii keys, you will need to do it twice.
But if you press one of the special keys that return double values
you need to press it only once. That's because one keypress of, say F1,
returns 2 values (59, and 0): the first getch() in the program sees the zero
because that is the one sent last, but 59 is still in memory, and when it
encounters the second getch(); it is fed the value 59.

This function returns those values with additions to differentiate them from
the normal ascii values (eg. 59 is ;(semi-colon) in the ASCII character keyset.)
so 256 is added to the 59 for F1 and is returned as 315.

I have this program saved as "AllKeys.c" so that I can use it to quickly look up the key codes.
This program will exit when you hit the Esc key.
Suppose we want to perform a certain action when the user hits the F1 key. This function will return 315 for the F1 key. So we can declare an int variable, example 'key', and say 'key=get_key()', the program will wait for a user keypress, and if the user hits F1, 'key' will hold the value 315. Then we can do if(key==315){do your thing}.

By the way, Man Of Steel is friggin awesome! Watched it last night.

#include <stdio.h>
#include <conio.h>

int get_key()
{
    int c = getch(); //first getch() sees the first value
    switch (c)  //This will only take action if the value is 0 or 224
    {
      case 0:   return getch()+256;  //and this second getch() eats the 2nd value
      case 224: return getch()+512; //and 256 or 512 is added to it and returned.
    }
    return c;   //if the value is not 0 or 224 (IE key is not special) normal value is returned
}

int main()
{
    printf("Esc to exit: \n");
    int d;
    while ((d = get_key()) != 27)
    {
        printf( "%d\n", d);

        if(d==315)  //if the key if f1.....
        {
            printf("F1 Key!\n"); //print this
        }
    }
    printf( "%d\n",d); //print the return value of the key pressed
}


______________________________________________________________________________
EDIT:
I just saw that we can use the ctrl combination for most of the keys. Ctrl-a and ctrl-A both return value 1, combined with b it returns 2 and so on and so forth till 26 for ctrl-z.
These values are unique in the sense that no other key on the keyboard returns these values (1 through 26) because they are special characters not available on the keyboard,  except for Enter (value=13) is the same as Ctrl+m (value=13), Backspace (Value=8) is the same as Ctrl+h (value=8) and the TAB key (value=9) is the same as Ctrl+i (value=9). There may be others, but none I can find. Best avoid those combinations in our programs.
Incidentally, you can backspace using Ctrl+h when on the DOS screen, ESC using Ctrl+[ etc. So, it's not a problem we have to address I guess, DOS treats these return values as identical keys so we can do the same.

So yeah, I guess we can use ctrl combinations, unless you are Chuck Norris (Ref: Chuck Norris does not have Control keys on his keyboard; Chuck Norris is always in control). Now about the Alt keys.......

No comments:

Post a Comment