how i use arrows in a program?
like -> to move right , <-- to move left etc...
Printable View
how i use arrows in a program?
like -> to move right , <-- to move left etc...
Compiler/OS Please. On windows you would look into:
ReadConsoleInput
INPUT_RECORD
Virtual-key codes
??? what? ???
what i meaned is like:
[code/]
string x;
x = getch;
if ( x == "F" )
// do f;
else if ( x == "left arrow or smthing like that" )
//move left
[code]
see?
Sort out your CODE tags, and AFAIK that should be:Quote:
Originally Posted by wyvern
Code:if (x == 'F') // Not "F"
// etc...
That displays the value of x, which is what getchar returns. It quits when getchar returns 13, which is the enter key. Figure out what the arrow keys are.Code:#include <iostream>
#include <conio.h>
int main()
{
int x=getch();
while(x!=13)
{
std::cout<<x<<std::endl;
x=getch();
}
return 0;
}
Try this:
Code:int a = 0;
while(a != 'q')
{
a = getch();
if(a == 0 || a == 224)
a = getch();
cout << a << "\n";
}
The arrow keys (for command line at least) are a combination of two codes, 224 and 75 for left, 77 right, 72 up and 80 down.
So possibly somethething likeCode:int x = 0;
if(getch() == 224)
{
x = getch();
if(x == 72){} //up
else if(x == 75){} //left
else if(x == 77){} //right
else if(x == 80){} //down
}
Processing keyboard and mouse events is covered in part 5 of my console programming tutorial.
This is assuming you are using Windows of course, you were asked, but didn't answer.
I finnaly found a safe way to do it here it is
hope everyone finds this usefull as i also found it very usefull ^^Code:
int x;
x = getch();
if ( x == 0x48 ) {} // up
else if ( x == 0x4B ) {} // left
else if ( x == 0x50){} // down
else if ( x == 0x 4D) {} // right
I wrote this recently, it should basically answer your question.
Though I'm sure there are functions in some header that already did this for you. Perhaps windows.h or something.Code:/* ******************************************
Name: Detecting Extended Characters in DOS
Author: W---------------- ** Not a chance **
Date: 06/10/05 12:43
Description: Shows how a programmer may
use functions in conio.h to detect extended
characters while still allowing the ability
to read non-extended characters.
****************************************** */
#include <iostream.h>
#include <conio.h>
const char UPKEY = 72;
const char LEFTKEY = 75;
const char RIGHTKEY = 77;
const char DOWNKEY = 80;
const char INSERTKEY = 82;
const char DELETEKEY = 83;
const char HOMEKEY = 71;
const char ENDKEY = 79;
const char PGUPKEY = 73;
const char PGDNKEY = 81;
const char SPACE = 32;
const char ENTER = 13;
const char TAB = 9;
const char BSPACE = 8;
const char ESC = 27;
int main()
{
bool isExtended;
char keyPress;
cout << "Press any key on your keyboard. This program should read" << endl
<< "and echo them all accordingly, with the exception of some" << endl
<< "certain keys, as they were either not defined or have" << endl
<< "specific functions within the DOS window." << endl << endl;
do
{
keyPress = getch();
if (int(keyPress) == -32 || int(keyPress) == 0) // Values returned by
isExtended = true; // Extended Characters.
else
isExtended = false;
cout << "You pressed ";
if (isExtended == true)
{
keyPress = getch(); // Reads in the second value of the
// Extended Character.
switch(keyPress)
{
case UPKEY:
cout << "the UP key." << endl;
break;
case DOWNKEY:
cout << "the DOWN key." << endl;
break;
case LEFTKEY:
cout << "the LEFT key." << endl;
break;
case RIGHTKEY:
cout << "the RIGHT key." << endl;
break;
case INSERTKEY:
cout << "the INSERT key." << endl;
break;
case DELETEKEY:
cout << "the DELETE key." << endl;
break;
case HOMEKEY:
cout << "the HOME key." << endl;
break;
case ENDKEY:
cout << "the END key." << endl;
break;
case PGUPKEY:
cout << "the PAGE UP key." << endl;
break;
case PGDNKEY:
cout << "the PAGE DOWN key." << endl;
break;
default:
cout << "an undefined extended character." << endl;
break;
}
}
else if (isExtended == false)
{
switch(keyPress)
{
case SPACE:
cout << "the SPACE key." << endl;
break;
case BSPACE:
cout << "the BACKSPACE key." << endl;
break;
case ENTER:
cout << "the ENTER key." << endl;
break;
case TAB:
cout << "the TAB key." << endl;
break;
case ESC:
cout << "the ESCAPE key." << endl;
break;
default:
cout << "the [" << keyPress << "] key." << endl;
break;
}
}
} while (1);
return 0;
}
Oh well, look around or do something like this.
...and for the hell of it, here is a program that uses the arrowkeys:
Code:/* *************************************************
Name: Active Movement Interfaces in DOS
Author: W---------------
Date: 06/10/05 18:14
Description: Demonstrates the use of the gotoxy()
function and getch() fuction to create an active
interface in a standard DOS program.
***************************************************/
#include <iostream.h>
#include <conio.h> // Should contain a standard gotoxy() function, but
// mine doesn't for some reason, so I defined it
// accessing Windows API directly.
#include <windows.h> // Needed for the gotoxy() function I used.
void gotoxy(int x, int y);
int main()
{
const char UPKEY = 72;
const char LEFTKEY = 75;
const char RIGHTKEY = 77;
const char DOWNKEY = 80;
int horiAXIS = 2;
int vertAXIS = 2;
int keyPress;
cout << "Press the arrow keys to move the \"o\"." << endl << endl;
cout << " o";
while (1)
{
keyPress = getch();
switch(keyPress)
{
case UPKEY:
if (vertAXIS > 1)
vertAXIS = vertAXIS - 1;
break;
case DOWNKEY:
vertAXIS = vertAXIS + 1;
break;
case LEFTKEY:
if (horiAXIS > 0)
horiAXIS = horiAXIS - 1;
break;
case RIGHTKEY:
horiAXIS = horiAXIS + 1;
break;
}
cout << " "; //Clears the last character.
gotoxy(horiAXIS,vertAXIS);
cout << "o"; //Creates the new character.
gotoxy(horiAXIS,vertAXIS); //Returns to standard space after print.
}
return 0;
}
// I didn't write the following gotoxy() function. It can be found at:
// http://spike.scu.edu.au/~jmaltby/c.html
void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}