PDA

View Full Version : getch();



frenchfry164
11-06-2001, 09:17 PM
What will getch() return for these keys:

Left Arrow
Right Arrow
Up Arrow
Down Arrow
ESC
F1-12
Tab
Enter
Shift
CTRL
ALT
Backspace

like in this

switch (getch())
{
case LEFT ARROW:
blah blah
break;
}

Hillbillie
11-06-2001, 10:09 PM
Why don't you enter this in the compiler and check it for yourself:



#include <iostream.h>
#include <conio.h>

int main()
{
char ch = getch();

cout << ch << endl;
return 0;
}


Most if not all of those keys will cause the program to not display anything. This is because they do not have corresponding ASCII codes.

Of course, ya never know. A few might bring some unexpected results...

Yoshi
11-06-2001, 10:41 PM
Up, down, left, and right arrow shows 0 in my ASCII codes... :o

Hillbillie
11-06-2001, 10:47 PM
Well, there ya have it.

bluehead
11-07-2001, 03:57 PM
up is 72,
down is 80,
right is 77,
left is 75

If this is what you're looking for, no thanx needed. if not, Then i don't know

frenchfry164
11-07-2001, 07:38 PM
Okay, I got it to do letters. K = left H = up P = down M = right

Now when I press an arrow it returns zero the first time I press it, then it returns the letter. WTF? Why does it do this, and can I override it doing that? Or do I need to just tell the user to press left twice or down twice to move.

Magos
11-08-2001, 03:01 AM
I think it has something to do with upper and lower bytes... Well, I don't know so I should probably just shut up :).

Unregistered
11-08-2001, 09:30 AM
hi

simply use this code:

x=getch();
if(x==0) // is special key else is normal key (a, b, RETURN, ESC ....)
y=getch(); // number of y is the key pressed (up, down, F1, F2...)

i hope this helps.

bye


PLEASE HELP ME WITH MY MOUSE IN 800x600 24 BIT MODE!!!

ronin
11-08-2001, 03:14 PM
You can try this out if you like. A sort of in the works project. :)

#include <stdio.h>
#include <conio.h>

#define KEYBOARD 0x60
#define KCONTROL 0x61
#define KINTCONTROL 0x20

#define LARROW 75
#define RARROW 77

// continue from there

#define F_1 59
#define F_2 60

// continue from there

int key;

int getKey(void);

int main()
{

getKey();

switch(key)
{
case LARROW: printf("\nleft arrow key pressed.");
break;
case F_1: printf("\nF1 key pressed.");
break;
// and so on

}

return 0;
} // end main

int getKey(void)
{
printf("\nPress a key: ");
key = getch();

_asm
{
sti
in al,KEYBOARD
xor ah,ah
mov key,ax
in al,KCONTROL
or al,82h
out KCONTROL,al
and al,7fh
out KCONTROL,al
mov al,20h
out KINTCONTROL,al
}
return key;
}

I hope there are no typoes as this was rushed.

frenchfry164
11-08-2001, 07:02 PM
thats okay, I'm just going to make it loop the getch() again if it's zero. That way it don't flash, because the character and the HP/MP flash everytime you take a step for some reason.

ronin
11-08-2001, 07:21 PM
Ah, A RPG hound then? A 3D game?

Jeremy G
11-08-2001, 09:12 PM
did this, it gets the arrow keys and does *something* depending on which arrow key. Pretty simple, should be easy to modify for your use.

//code:
void cursor()
{
// getch();

int c = 1;
int ch = 0;

while (ch != c)
{
ch = getch();

// cout << ch << endl;
if(ch == 72) // if UP
cout << "^" << endl;
if(ch == 80) // if Down
cout << "d" << endl;
if(ch == 75) // if left
cout << "<" << endl;
if(ch == 77) // if right;
cout << ">" << endl;
}

}
// ^^ end code!