...

View Full Version : C++ Up Arrow/ Down Arrow?



frederickan
11-19-2006, 01:52 AM
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

oracleguy
11-19-2006, 04:46 AM
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()

frederickan
11-19-2006, 04:54 AM
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.

oracleguy
11-19-2006, 05:41 PM
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:



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.

mathematician
11-19-2006, 05:56 PM
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:


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.)

rpgfan3233
11-19-2006, 07:36 PM
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:


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:

G H I
K M
O P QThe 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. :p 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.

frederickan
11-19-2006, 08:11 PM
Thanks a lot for all the info.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum