Thread: Implenting Function Keys

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    Implenting Function Keys

    Well I Am Sort of a noob in c right, but i would like to know how do you implement the f keys on your keyboard into a c program and the arrow keys too. i dont kno the library for them.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Here's a little program that may help you figure it out. Run it and push regular keys and function/arrow keys. Note that regular keys only generate one byte while the function/arrow keys actually generate two bytes.
    Code:
    #include <conio.h>
    int main()
    {
        int c;
        while ((c = getch()) != 27) /* exit with Escape key */
            printf( "&#37;d\n", c);
    }
    Last edited by nucleon; 11-07-2008 at 10:55 AM. Reason: forgot include

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well that would depend on your OS and compiler (and not a presumption of using TurboC and DOS).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Salem: Is your comment directed at me? I've never heard of "TurboC" and I certainly don't use "DOS". I compiled it using gcc and ran it under Windows XP.

    I always assume that someone who does not name their specific OS is using Windows, since someone using another OS should be smart enough to mention it!

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Hardcore Win puppy, huh?
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Actually I've used Unix longer than I've used Windows.
    And don't call me a puppy.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Surely, you like being called puppy (Airplane humor... gotta love it). I am starting to notice that despite the validity of your assertion about win-users you also forget a lot of them are also first year comp sci students who do not know what they are doing either... And may very well run BSD at school.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Okay, you can call me puppy, although I'm almost 4 in dog years.
    But "Airplane humor"? What's that?
    Good point about compsci students. I didn't think of that.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Quote Originally Posted by nucleon View Post
    Here's a little program that may help you figure it out. Run it and push regular keys and function/arrow keys. Note that regular keys only generate one byte while the function/arrow keys actually generate two bytes.
    Code:
    #include <conio.h>
    int main()
    {
        int c;
        while ((c = getch()) != 27) /* exit with Escape key */
            printf( "%d\n", c);
    }
    interesting, the only way to exit the program is using the escape key because escape = int 27, am i correct? i actually use turboC(borland C++ ) as my compiler and yes am using windows. suppose i have a program how could i use the function keys to navigate through the .exe file?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Yes, the numeric code for escape is 27. Function and arrow keys give two numeric codes in a row, taking two iterations of the loop.

    There may be a higher-level way of detecting them. Windows has virtual key codes. Are you making a console program or a gui program?

    What do you mean by "navigate through the .exe file"?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    i am making a console program, for a data structures project, but bonus marks are alotted if we can get like instead of using enter key escape key and the arrow key to browse and navigate through the program, we should implement the function keys and the + and - keys to browse through the program, a lot of clear screen functions will be used also.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So have you tried nucleon's post #2 yet?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    getch() doesn't work with some keys like F's and Arrows properly. For example, it adds some values when CTRL is pressed and so on.. I made a functions that can detect pressing of: all F's, Arrows, buttons at Insert and Home and Numpads. It isn't portable because it uses a Windows function to check if CTRL is pressed, but I'm sure there is some Unix or Linux (or BSD) function to detect if CTRL is pressed.

    Code:
    #define KEY_ESCAPE  0
    #define KEY_ENTER   1
    
    #define KEY_INSERT  2
    #define KEY_HOME    3
    #define KEY_PGUP    4
    #define KEY_DELETE  5
    #define KEY_END     6
    #define KEY_PGDOWN  7
    
    #define KEY_UP      14
    #define KEY_DOWN    15
    #define KEY_LEFT    16
    #define KEY_RIGHT   17
    
    #define KEY_F1      18
    #define KEY_F2      19
    #define KEY_F3      20
    #define KEY_F4      21
    #define KEY_F5      22
    #define KEY_F6      23
    #define KEY_F7      24
    #define KEY_F8      25
    #define KEY_F9      26
    #define KEY_F10     27
    #define KEY_F11     28
    #define KEY_F12     29
    
    #define KEY_NUMDEL  30
    #define KEY_NUMPAD0 31
    #define KEY_NUMPAD1 127
    #define KEY_NUMPAD2 128
    #define KEY_NUMPAD3 129
    #define KEY_NUMPAD4 130
    #define KEY_NUMPAD5 131
    #define KEY_NUMPAD6 132
    #define KEY_NUMPAD7 133
    #define KEY_NUMPAD8 134
    #define KEY_NUMPAD9 135
    
    unsigned int getkey(void);
    unsigned int getkey(void)
    {
        unsigned int f[12]={KEY_F1,KEY_F2,KEY_F3,KEY_F4,KEY_F5,KEY_F6,KEY_F7,KEY_F8,KEY_F9,KEY_F10,KEY_F11,KEY_F12};
        unsigned int key;
    
        key=getch();
        while(GetKeyState(VK_LCONTROL)&0x8000 && GetKeyState(VK_RCONTROL)&0x8000) continue;
    
        switch(key)
        {
            case 0:
            {
                int k;
    
                switch((k=getch()))
                {
                    case 71: return KEY_NUMPAD7;
                    case 72: return KEY_NUMPAD8;
                    case 73: return KEY_NUMPAD9;
                    case 75: return KEY_NUMPAD4;
                    case 77: return KEY_NUMPAD6;
                    case 79: return KEY_NUMPAD1;
                    case 80: return KEY_NUMPAD4;
                    case 81: return KEY_NUMPAD3;
                    case 82: return KEY_NUMPAD0;
                    case 83: return KEY_NUMDEL;
                    default: return f[k-59];
                }
            }
            case 224:
            {
                int k;
    
                switch((k=getch()))
                {
                    case 71: return KEY_HOME;
                    case 72: return KEY_UP;
                    case 73: return KEY_PGUP;
                    case 75: return KEY_LEFT;
                    case 77: return KEY_RIGHT;
                    case 79: return KEY_END;
                    case 80: return KEY_DOWN;
                    case 81: return KEY_PGDOWN;
                    case 82: return KEY_INSERT;
                    case 83: return KEY_DELETE;
                    default: return f[k-123];
                }
            }
            case 13: return KEY_ENTER;
            case 27: return KEY_ESCAPE;
            default: return key;
        }
    }
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    hmmm, thats something like what i would want, but i thought there was a simpler way in getting to implement them, like from a library, but thanks for all the help and new tips i learnt..

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    You can simplify that, but you still need a bunch of defines.
    Code:
    #include <conio.h>
    
    // Name some control-codes
    #define KEY_BEL        7
    #define KEY_BS         8
    #define KEY_TAB        9
    #define KEY_LF        10
    #define KEY_CR        13
    #define KEY_ESC       27
    #define KEY_DEL      127
    
    // If 0 proceeds extended code, add 256
    #define KEY_F1        59 + 256
    #define KEY_F2        60 + 256
    #define KEY_F3        61 + 256
    #define KEY_F4        62 + 256
    #define KEY_F5        63 + 256
    #define KEY_F6        64 + 256
    #define KEY_F7        65 + 256
    #define KEY_F8        66 + 256
    #define KEY_F9        67 + 256
    #define KEY_F10       68 + 256
    
    // If 224 proceeds extended code, add 512
    #define KEY_F11      133 + 512
    #define KEY_F12      134 + 512
    #define KEY_HOME      71 + 512
    #define KEY_UP        72 + 512
    #define KEY_PAGEUP    73 + 512
    #define KEY_LEFT      75 + 512
    #define KEY_RIGHT     77 + 512
    #define KEY_END       79 + 512
    #define KEY_DOWN      80 + 512
    #define KEY_PAGEDOWN  81 + 512
    #define KEY_INSERT    82 + 512
    #define KEY_DELETE    83 + 512
    
    int get_key() {
        int c = getch();
        switch (c) {
          case 0:   return getch() + 256;
          case 224: return getch() + 512;
        }
        return c;
    }
    
    int main() {
        int c;
        while ((c = get_key()) != KEY_ESC) {
            printf( "%d\n", c);
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM