PDA

View Full Version : Key Input



Ruski
07-09-2002, 10:12 AM
How can you catch a key input? I mean you've got a menu, 1. New Game, 2. Exit Game and you get to choose 1 or 2. I only know how to make it with the cin but I kinda want it on KeyPress, not type a number and press enter, just press the key and an action happens.
Thanks :)

shaik786
07-10-2002, 02:36 AM
What compiler are you using? check to see if it supports getch()

Ruski
07-10-2002, 02:40 AM
Dev-C++, cant check cuz it's got no help files...

shaik786
07-10-2002, 11:26 PM
Try including getch() in your code and make. If your linker gives no error, means this function is supported, else no.
Also, search the web for documentation on your compiler.

Ruski
07-11-2002, 02:38 AM
I'm a beginner, so what do you mean including? How?
Thanks :)

shaik786
07-11-2002, 05:21 AM
What version of DEV-C++?

Kyoto Oshiro
07-12-2002, 10:48 PM
Ruski,
For doing that, you just set your variable equal to getch(); Such as...


#include <iostream.h>
#include <conio.h>
int main()
{
int Keypressed;
cout<< "Main Menu\n\n";
cout<< "1. New Game\n";
cout<< "2. Exit\n";
Keypressed = getch();

if (Keypressed == 1)
// etc.
} // end of main


Or, I would make the variable you are inputting a string. I know that it is irritating having a little pause, but try entering a letter when asking for an integer. Not pretty, the screen fills up with repeats of your question.
Hope that helps, post again for clarification or more examples.

sriharsha
07-13-2002, 12:20 AM
Hello Friend!!
Whenever u accept a key using getch(), the prototype u need to define is char,insted of integer,though u r accepting int. Then try converting that into integer,which is so simple. the example code is given here.

#include<stdio.h>
#include<conio.h>
void main()
{
int key;
printf("\n1.New Game");
printf("\n2.Exit Game");
key=getch();
if ((int)key==49)
printf("New Game activated");
if ((int)key==50)
printf("Exit Game activated");
}

here (int)key is typecasts the charcter to int. The ASCII equivalent to 1 is 49 and that of 2 is 50. U can even check by putting them in a switch case.

Hope u understood the concept. If any querries, mail me at sriharsha_kvr@yahoo.com

Ruski
07-15-2002, 03:37 AM
Thanks :)

ChrisMUK
07-21-2002, 03:40 AM
Right im also looking for something to do that..... i got that part working but how can i then get it to go to say the game?

Im not making a game just an example.

Hammer
07-21-2002, 04:34 PM
>void main()
This is incorrect, use should use "int main(void)".

>int key;
>if ((int)key==49)
>here (int)key is typecasts the charcter to int.
Not exactly, key is already an int because that's how is was declared. Therefore the cast isn't necessary.

>if ((int)key==49)
Rather than coding the ASCII value of the character, you could use the character constant, like so:
>if (key=='1')
This makes things easier to read.

>i got that part working but how can i then get it to go to say the game?
Does this example help?:


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

void TheGame(void)
{
printf("The game has been activated\n");
}

int main(void)
{
int key, StillGoing = 1;

while (StillGoing)
{
printf("--- Menu ---\n1.New Game\n2.Exit Game\n>");
switch (key = getch())
{
case '1': TheGame(); break;
case '2': StillGoing = 0; break;
default: printf("Option Invalid: %c\n", key); break;
}
}

printf ("Exiting\n");
return(0);
}

VBprogrammer
07-21-2002, 06:17 PM
void main()

LOL, nothing annoys a C programmer more than that...well us assembly programmers maybe, but thats a whole other kettle of fish.

Hershlag
07-21-2002, 07:27 PM
that's because C programmers are under the dilusion that they are the super, low-level geniuses. when an assembler freak comes along they are blown away by reality. C is really like straddling the fence. Assembler or C++ only in my book! C is an outdated version of C++! :)

Sorry, this is my first time trolling. :D

Hammer
07-22-2002, 04:33 AM
>that's because C programmers are under the dilusion that they are the super, low-level geniuses.
Yes.. and? :D

>when an assembler freak comes along they are blown away by reality.
Not really, it's two different worlds, I don't think a direct comparison works. But I do appreciate the assembler lanuage, and the people that know a lot about it.

>C is really like straddling the fence.
What fence?!

>C is an outdated version of C++! :)
Oh no, not the C vs C++ argument! :D

>Sorry, this is my first time trolling.
Well, I'm not going to provoke you by replying to this thread. Oh dammit, I just did :D :)

Ruski
07-22-2002, 06:11 AM
Thanks for the help :)

johnc
07-23-2002, 08:19 PM
ASM or C++ For Me.

C programmers think they are low level geniuses.


ahh C++ is way easier then C, when it gives you all this object oreinted stuff where u can make guis in a flash.

Ruski
07-24-2002, 02:50 AM
What do you mean ? :confused:

VBprogrammer
07-24-2002, 01:24 PM
Since when did easier == better?

Ruski
07-25-2002, 02:53 AM
Originally posted by VBprogrammer
Since when did easier == better?
Since this second :) j/k. Btw: can I borrow your icon file? The one you use in your posts? That robot walking or whatever? Please?

VBprogrammer
07-25-2002, 01:33 PM
Hmm...I like my walking floppy disk and i can't be bothered resizing another gif file. I'll PM you when i get bored of it, ok?

Ruski
07-26-2002, 01:47 AM
Yea :) And when you get bored of it you can have my new one I just got yesterday.. :)