• Advertisement
Sign in to follow this  

Arrow key values for DevC++

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Thank you in advance.

Share this post


Link to post
Share on other sites
Quote:
Original post by BladeStone
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 ... 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by BladeStone
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*


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);
}
}

Share this post


Link to post
Share on other sites
Zahlman:

o.0 Thank you for your effort, I've added notes to my code snip-it library about how things should work, as well as your curser moving code.

I know you are on XP, but what is the compiler you are using?

I've already rated you at the highes because you've been so passionate about helping me out, but I don't want to use up more of your time then I have already.

Share this post


Link to post
Share on other sites
gcc 3.4.3 (I incorrectly stated 3.4.4 above). (Please note, if you are trying to compare, that "DevC++" is not your compiler; it is your IDE.) But this should not matter; the behaviour is up to conio.h and the 'cmd' program, not to the compiler.

Share this post


Link to post
Share on other sites
Quote:
Original post by BladeStone
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.


No, I said there was no standard way to do this. Both C and C++ do not directly support what what you want to do. You have to make use of compiler extensions or 3rd party libraries. It seems like you got it working with getch(e). Still, I'll explain the GetAsyncKeyState function because, believe it or not, it's pretty easy to use, although it is used differently from getch(e). If you recall from the link, the article lists first the 'syntax', then the 'parameters' and finally the 'return value'.

First: syntax. This tells you that the function returns a SHORT value and takes an integer named vKey.

Second: parameters. They don't list the things you can pass into the function directly. Instead it provides a link to the list of valid virtual keys. The ones you are concerned with are VK_LEFT, VK_RIGHT, VK_UP, and VK_DOWN.

Third: return value. The most important nugget of this section says "If the most significant bit is set, the key is down." Actually, I'm pretty sure you can just check for a non-zero value and you'll be fine. So, here's how you'd use it:

if(GetAsyncKeyState(VK_LEFT))
{
// do stuff if the left key is down
}
if(GetAsyncKeyState(VK_UP))
{
// etc
}


Another useful function is just GetKeyboardState which gets the state of every key at once. You can use it like so:
std::vector<BYTE> keyStates(256);
// this is just how you fill up a vector when the function
// expects a pointer. We use vectors because it's safer
GetKeyboardState(&keyStates[0]);
if(keyStates[VK_UP])
{
// do stuff
}


But if what you are doing works for you, I don't see a reason to change.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement