-
Key Input
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 :)
-
What compiler are you using? check to see if it supports getch()
-
Dev-C++, cant check cuz it's got no help files...
-
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.
-
I'm a beginner, so what do you mean including? How?
Thanks :)
-
-
Ruski,
For doing that, you just set your variable equal to getch(); Such as...
Code:
#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.
-
Try this!!!
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 [email protected]
-
-
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.
-
>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?:
Code:
#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);
}
-
LOL, nothing annoys a C programmer more than that...well us assembly programmers maybe, but thats a whole other kettle of fish.
-
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
-
>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 :)
-