Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy C++ Up Arrow/ Down Arrow?

    What could you put for like the equivalent to if u hit the up arrow / down arrow... the only thing i know that has to do with hitting keyboard buttons is "kbhit()", which if anything is hit, but what would be for up/down arrow hit?
    Thanks for all help
    ~~Fred

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    What type of program is this? Is it a console application? If so and you want to capture any of the arrow keys, you can do so with getch()
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I do not know what kind it is, all that i know is that i am using Borland C++ IDE for my compiler... And i figured you could do something with a "getch()", but I do not exactly know how... Can someone tell me? All i know about getch() is that i have been using it at the end of my other programs in order to keep the output on the screen.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It sounds like you are writing a console application. A console app is one that has no graphical interface, its all text based.

    Here is an example of using the getch() to collect arrow key input:

    Code:
    int input;
    do
    {
    
    input = getch(); cout << "You pressed key ID: " << input << endl;
    }while(input != 113);
    And if you press q it should end the loop. Also be aware that as far as letters go, upper case and lower case letters have different ID numbers.
    OracleGuy

  • #5
    New to the CF scene
    Join Date
    Nov 2006
    Location
    Shrewsbury Uk
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code

    c = getch();
    will read in a standard keystroke, such as one of the alphabetic or numeric keys. If, however, it returns a null character, some such key as an arrow, PgUp/PgDn or Del key etc has been struck, and there is another byte waiting to be read. To read it you call getch() a second time. The second byte returned, along with the initial null byte, constitutes the code for one of the extended keys. Those codes you will either need to look up somewhere, or use the following code to discover them:

    Code:
    void( main void )
    {
        int c;
    
        printf( "Please strike a key..." );
    
        c = getch();
        if( c != 0 )                               // test for extended or normal keystroke
             printf( "Normal keystroke for: %c\n", c );
        else 
        {
             c = getch();                       // retrieve second byte of extended code
             c *= 256;                          // shift left by 8 bits to get extended code
             printf( "Extended keystroke: %X\n", c );
        }
    
         return;
    }
    That way of doing things comes from the olden days, when MS-DOS programmers used to have to call int 21h function 2 twice to get an extended keystroke. (One of the few MS-DOS functions I can remember off the top of my head.)
    Last edited by mathematician; 11-19-2006 at 06:09 PM.

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by mathematician View Post
    The code

    c = getch();
    will read in a standard keystroke, such as one of the alphabetic or numeric keys. If, however, it returns a null character, some such key as an arrow, PgUp/PgDn or Del key etc has been struck, and there is another byte waiting to be read. To read it you call getch() a second time. The second byte returned, along with the initial null byte, constitutes the code for one of the extended keys. Those codes you will either need to look up somewhere, or use the following code to discover them:

    Code:
    void( main void )
    {
        int c;
    
        printf( "Please strike a key..." );
    
        c = getch();
        if( c != 0 )                               // test for extended or normal keystroke
             printf( "Normal keystroke for: %c\n", c );
        else 
        {
             c = getch();                       // retrieve second byte of extended code
             c *= 256;                          // shift left by 8 bits to get extended code
             printf( "Extended keystroke: %X\n", c );
        }
    
         return;
    }
    That way of doing things comes from the olden days, when MS-DOS programmers used to have to call int 21h function 2 twice to get an extended keystroke. (One of the few MS-DOS functions I can remember off the top of my head.)
    In the days of QBasic, the idea was the same as the MS-DOS programmers. You had to know that the first character was a 0 and the length of the string that captured the key press had to be 2. If the length was 1, a program should have been treated as if there was no key press. If the length was 2, there was an extended key press, meaning that there was another character to output. First char = '\0', second char = X, where X is as follows on the numpad:
    Code:
    G H I
    K   M
    O P Q
    The L is missing on my keyboard, and I'm not sure if that is true for all or not. It might be because the key doesn't correspond to anything other than 5, so it has an empty character code? Note that those correspond to the arrow keys as well as the Ins, Del, PgUp, PgDn, Home and End keys, in no specific order.

    The function keys F1-F10 and F11-F12 (you'll see why I list them separately) are different because they can respond to the modifier keys:
    Key - Without modifiers - With Shift - With Ctrl - With Alt
    F1 - ';' - 'T' - '^' - 'h'
    F2 - '<' - 'U' - '_' - 'i'
    F3 - '=' - 'V' - '`' - 'j'
    F4 - '>' - 'W' - 'a' - 'k'
    F5 - '?' - 'X' - 'b' - 'l'
    F6 - '@' - 'Y' - 'c' - 'm'
    F7 - 'A' - 'Z' - 'd' - 'n'
    F8 - 'B' - '[' - 'e' - 'o'
    F9 - 'C' - '\' - 'f' - 'p'
    F10 - 'D' - ']' - 'g' - 'q'
    F11 - (char)133 - (char)135 - (char)137 - (char)139
    F12 - (char)134 - (char)136 - (char)138 - (char)140

    I listed F11 and F12 differently because they are rather different. There were actually F1-F10 before. F11 and F12 were added later.

    Note that Ctrl, Shift and Alt are called "modifier" keys. A good example for the name is seen in the function keys. There are no possible ASCII codes that they can correspond to. To see when they are pressed, Assembly language is required to detect the actual keyboard scan codes, unless you use a library that uses Assembly to detect them, in which case Assembly language is still technically used. Be aware that the Tab, Backspace and Enter/Return keys aren't extended keys. The ASCII codes for Tab and Backspace are 9 and 8. The ASCII code for Enter/Return depends on a couple of things. Enter/Return is one of three possible ideas: 13+10 ('\r\n' in C) for Windows, 13 ('\r') for Mac IIRC, 10 ('\n') for Linux/UNIX. I believe you use '\n' for Windows and Linux normally though. I'm just showing you what it would look like in Assembly language. QBasic generates a single 13 for Enter/Return, which means that it uses the carriage return character ('\r') rather than the linefeed/newline ('\n') character, character 10. This last bit is confusing, I'm sure, but just know that Tab, Backspace and Enter/Return are not extended keys. Only F1-F12, Home, Ins, etc., and the arrow keys are considered extended keys.

    Edit: The characters that were shown were created using QBasic, so they may be different (or not) for C.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #7
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot for all the info.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •