You are viewing our Forum Archives. To view or take place in current topics click here.
[C++] Detecting arrow keypress
Posted:

[C++] Detecting arrow keypressPosted:

-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532

#define LEFT_ARROW 37
#define UP_ARROW 38
#define RIGHT_ARROW 39
#define DOWN_ARROW 40

switch (response)
{
   case UP_ARROW:
   {
      printf("Up arrow was pressed.\n");
   }
   case DOWN_ARROW:
   {
      printf("Down arrow was pressed.\n");
   }
   case LEFT_ARROW:
   {
      printf("Left arrow was pressed.\n");
   }
   case RIGHT_ARROW:
   {
      printf("Right arrow was pressed.\n");
   }
   default:
   {
      printf("That was an invalid movement.");
   }
}


When pressing an arrow key, they return a value of 224, and from what I've read it is the second number (37-40) that I need to use to work upon. How would I program this to use the second number instead of this original 224?
#2. Posted:
PoetVin
  • TTG Master
Status: Offline
Joined: Jan 14, 201014Year Member
Posts: 863
Reputation Power: 29
Status: Offline
Joined: Jan 14, 201014Year Member
Posts: 863
Reputation Power: 29
-Deano wrote

#define LEFT_ARROW 37
#define UP_ARROW 38
#define RIGHT_ARROW 39
#define DOWN_ARROW 40

switch (response)
{
   case UP_ARROW:
   {
      printf("Up arrow was pressed.\n");
   }
   case DOWN_ARROW:
   {
      printf("Down arrow was pressed.\n");
   }
   case LEFT_ARROW:
   {
      printf("Left arrow was pressed.\n");
   }
   case RIGHT_ARROW:
   {
      printf("Right arrow was pressed.\n");
   }
   default:
   {
      printf("That was an invalid movement.");
   }
}


When pressing an arrow key, they return a value of 224, and from what I've read it is the second number (37-40) that I need to use to work upon. How would I program this to use the second number instead of this original 224?


Are you saying when you release the arrow keys they return the value 224 instead of 37-40?
#3. Posted:
-Deano
  • Spooky Poster
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
What I had (to detect what number I should be using) was something like

c = _getch();
printf("%c", c);

which would always print 224 from all of the arrow keys instead of manipulating that second character code.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.