Public Group

# Arrow key values for DevC++

This topic is 4022 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to get the little arrows on the key board to work through the DevC++ but the only asci values I can find for them are as follows: /* Arrows + Home/End pad */ UP = 273, DOWN = 274, RIGHT = 275, LEFT = 276, INSERT = 277, HOME = 278, END = 279, PAGEUP = 280, .... etc How do you get DevC++ to use the solo arrows, as in the one not on the number pad.

##### Share on other sites
Okay.

1)ASCII only has 128 characters. a char on a large number of platforms is 8 bits, or 256 characters. Those values are larger than you can fit into a char.

2)There is no standard way to do what you want to do.

3)Check out GetAsyncKeyState I believe that should have what you need. Include windows.h. YOu probably have to do a few other things to get the program to work with DevC++, like link the correct libraries.

Hope that helps.

##### Share on other sites
I checked out that web site and it was plain hard to understand. I'm not exactly the sharpest tool in the shed.

I've tried doing a key stroke code to get the number when I press the keys but it comes up with this little fish character, not a number ... ? The secondary enter key is 271 value, and it works with DevC++.

You said there is no way to get those, but how does someone make use of them in DevC++ ... I know it can be done because those keys work for so many of my applications.

I'm sorry I'm a bit of a script kitty, there is so many hours I can put in with playing with vague lines of code to get them to work before I give up on them.

Is there a number which works, or is there 0x0000 kind of number to work with in addition to them?

##### Share on other sites
FOUND IT!!!!

In order: Up, down, left, right
--------------
Font Type: Terminal
asci#: 224H, 224P, 224K, 224M

Ok the solo arrow keys spits out the little fish character which is asci 224, then their is a secondary capital letter which goes with it. Therefore, the way you make it work is as follows:

char keyStroke;
keyStroke = getche();
switch(keyStroke) {

case '224H': {
... go north ...
break;
}

case '224P': {
... go south ...
break;
}

case '224K': {
... go west ...
break;
}

case '224M': {
... go east ...
break;
}

default: printf("do you know what a keyboard looks like?\r\n");
}// END OF SAMPLE CODE

The only warning I get is "[Warning] Multi-Character Character Constant" Which I'm guessing it means you can't use this code in any windows gui like visualC++ or visual studios because EDIT: because there is a better way to do it, see Zahlman's comment.

*grins innocently*
*runs and hides*

[Edited by - BladeStone on July 15, 2007 2:40:11 AM]

##### Share on other sites
visual * doesn't automatically make it a GUI app. I've never written one GUI app in Visual C++. That error you posted is a warning. These can easily be ignored without problems(usually). And it doesn't matter if you use DevC++ or Visual C++ or gcc, C++ is C++ no matter where you go. Its the same on Mac and Linux as it is in Windows. Same with Xbox, PS3 and Wii.

##### Share on other sites
This conversation is getting into topics which could flame up, which is not where the conversation needs to go ... thank you for your input even if I disagree. Private message me instead of posting here please.

##### Share on other sites
Quote:
 Original post by BladeStoneThe only warning I get is "[Warning] Multi-Character Character Constant" Which I'm guessing it means you can't use this code in any windows gui like visualC++ or visual studios because ... insert smug comment here ... hahahaha *grins innocently* *runs and hides*

No, your code is in this case completely wrong, and the compiler is warning you about a common language construct that is, while sometimes legitimate, usually a sign of doing something wrong.

First off, look up multi-character literals with Google.

Second, you seem not to understand at all how numbers versus text work in memory. '2' is not 2, for example; it's 50. '65' is a multi-character literal, which means its value isn't even guaranteed to be the same on different compilers. Whereas the number 65, interpreted as a character, is 'A'. That's what this ASCII encoding is doing for you: allowing letter symbols to share the same "space" as digit symbols. But you must distinguish a digit symbol from the value of that digit.

Third, the key sends two characters at once. That means you can't read them into a char at once and then compare them, because the read doesn't even pick them both up.

What you want to do is call getche() and check if the result is 224: if it is, call getche() again (it will pick up the second character sent by the arrow key, because it's immediately "available") and interpret it.

Also, initialize variables where possible. Although here, you don't need a variable at all.

switch (getche()) {  case 224: // NOT in quotes. This means 224 is the NUMBER, i.e. ascii 224.  // This is equivalent to putting the fishy symbol between single quotes,  // except that you're not really supposed to be able to use that symbol  // in source code :)  switch (getche()) { // pick up the other character.    case 'H': {      ... go north ...      break;    }    // etc. for 'P', 'K', 'M'.  }}

##### Share on other sites
And I learn a new thing about coding. Thank you, I didn't think about parcing the second character out from the first one in a single character keystroke command. o.0

I will do that to remove the warnings. Thank you

##### Share on other sites
Ok Zahlman, I updated like what you said to do, and in the game the character's location on the map jumped all around with descriptions and shop own names all messed up. Then I tried it as 224K etc without the single quoits and the same results. The only thing which works for DevC++ on my system is to force the '224K' through. and it works fine.

*shrugs*

##### Share on other sites
Quote:
 Original post by BladeStoneOk Zahlman, I updated like what you said to do, and in the game the character's location on the map jumped all around with descriptions and shop own names all messed up. Then I tried it as 224K etc without the single quoits and the same results. The only thing which works for DevC++ on my system is to force the '224K' through. and it works fine. *shrugs*

I can't even figure out what you mean by "jumped all around with descriptions and shop own names all messed up". But I'm telling you, those literals are nonsense.

For what it's worth, I tried to google it, and the first hit tells me this:

Quote:
 Under MS-DOS, if you receive a character with value 0 (not '0'!) while reading the keyboard, it's a flag indicating that the next character read will be a code indicating a special key. See any DOS programming guide for lists of keyboard codes. (Very briefly: the up, left, right, and down arrow keys are 72, 75, 77, and 80, and the function keys are 59 through 68.)

Which happens to work for me (note that those values 72/75/77/80 do in fact correspond to 'H', 'K', 'M' and 'P' - the same letters you discovered) when I open a command prompt ('cmd' under Windows XP), type in and compile the appropriate code (using gcc 3.4.4 from the command line) and run it. (Although I had to change getche() to just getch() in order to *not* display the stuff that was actually typed in.)

This eventually got refined into the following test program, which lets you move the cursor around with the arrow key until a non-arrow-key is pressed:

#include <conio.h>#include <algorithm>int main() {	const int MAX_X = 80;	const int MAX_Y = 25;	int x = MAX_X / 2;	int y = MAX_Y / 2;	clrscr();	gotoxy(x, y);	while (getch() == 0) {		switch (getch()) {			case 'H': y = std::max(y - 1, 1); break;			case 'K': x = std::max(x - 1, 1); break;			case 'P': y = std::min(y + 1, MAX_Y); break;			case 'M': x = std::min(x + 1, MAX_X); break;		}		gotoxy(x, y);	}}

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×