Advertisement

keyboard problem (Newbie)

Started by April 25, 2002 02:09 AM
6 comments, last by werdy666 22 years, 4 months ago
Hi! i am making a console program that takes the user through afew different menu screens. then it gets to a question where they have to type an answer. After displaying wether they got it right or not, the program jumps back to the first menu. However, occasionally it kinda like skips the main menu and jumps to the second menu. At the moment i have a main menu which has stuff like exit game, start game, and highscores. When i press s to start a new game it displays a new menu asking what level i would like to play at. I press 1, then it asks me a question where i use getline(cin,guess) to get keyboard input. Sometimes i can actually see the different letters that i have pressed through the main menu and the level choice menu. Like when it is waiting for me to type an answer and hit enter it will have "s1" already on the line. Then after it has told me if i have got the question right or not, it is supposed to jump back to the main menu. However, sometimes it jumps to the level choice menu instead of the main menu. how can i stop this??? i am using GetAsyncKeyState for the menu keypresses at the moment. There doesn''t seem to be a command to clear the keyboard buffer of all keypresses! Any help would be very much appreciatted! Werdy666
how do you use GetAsyncKeyState() ?

could you post the few lines of code ?
Advertisement
some code.....

text("choose your difficulty level!" , 30, 4);
text("1. easy 2. Medium 3. HARD!!", 25, 6);

do
{
if (keystate(49) == TRUE) level = 1;
if (keystate(50) == TRUE) level = 2;
if (keystate(51) == TRUE) level = 3;
}while (level == 0);

this is my code for the level choice menu screen. text funtion works fine.

the keystate function is....

bool keystate(int KeyType)
{
if (GetAsyncKeyState(KeyType) != 0)
{
return (true);
}
else
{
return (false);
}
}

I actually got it from another post from gamedev.

One thing i have noticed is that if i minimise the console window and type say 1 while the desktop is in focus, the console program detects it and goes through to the next screen.

Would i be just better off programming this little game in windows? or should i stick with win32 console?

Thanks for any help or advice you can give!

Werdy666
umm.. I'm above AP.. was too lazy to type in name & pwd


hhm.... I found the query for a pressed key in one source
file of LaMothe's done like this:

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)


put into your function it was:

bool keystate(int vk_code)
{
return (((GetAsyncKeyState(vk_code) & 0x8000) !=0) ? true : false)
}



I don't know if the missing & x8000 is the problem.
Try it, and let me know.

....................................................................................
EDIT:

No, it's at least not the only problem.
See my 3rd post below!
....................................................................................


When exactly do you read in a whole line?
Reading a line with cin, the characters are always displayed on the text screen, right?



However, some tips:

...if (keystate(49) == TRUE)...

you don't need to pick up the character's ASCII values of a book or so,
the compiler turns the chars into its corresponding number.
Just type: keystate( '1' )
The compiler will turn '1' into 49 for you.

I see, you're using a lowercase "bool" with your function.
So I assume, your C++ compiler has "bool", "true", "false" keywords defined by default.
Where "true" and "false" are constants of the type "bool".

TRUE and FALSE are defined by you.
Don't use TRUE together with bool.
Since "true" actually is also just stored as a number != 0, it works, of course.
But they don't belong together, you already have "true" and "false" defined,
so, use them.
(I think every recent compiler has bool defined, so, you won't need your TRUE
and FALSE definitions any longer)

And, for keystate() already returns a logical value, namely "true" or "false",
you don't need to compare the result with another logical value.
"if" decides on logical expressions, and keystate(x) is such one.
So, " if (keystate(x)) " does the same thing (I guess you knew this, though).

Maybe "KeyDown" is a better name for the function.
A decision based on " if ( KeyDown(key_code) ) " seems much more
intuitive than " if ( keystate(key_code) ) " or " keystate(code) == true "
(the words "keystate" and "true" don't sound that meaningful in conjunction,
do they? )



I'm not at home yet. When I'll have arrived, and still noone
helped you with your key-input problem, you can mail the entire code
to me, if you want. (or you could just post it, unless it's top secret )


hope anything of my post was useful at all.




[edited by - UnshavenBastard on April 25, 2002 8:22:24 AM]
edit: lots of typos

DOHH!!!

hahaha why didn't I see it at once...
didn't do such key asking thingys for some time....


GetKeyState does NOT wait for a pressed key, nor its releasement.

If you have a loop with keypressed? queries in it,
and you hit a key, your loop will, as you intended it to do,
exit, because the variable is no longer == 0.

THEN, your program will IMMEDIATELY continue with the next
piece of code.
If that's again a keypressed? query loop, you are still holding down
the key, because the computer is damn much faster than your finger

After you queried a keydown event, and took some action corresponding to what this
key is supposed to do,
you must always wait for the releasement of _that_ key!!
(for this kind of purpose)

IE.:


      int level = 0;do{    if (KeyDown('1'))    {        level = 1;        while (KeyDown('1')){};    // wait, until the key is no longer pressed    }        if (KeyDown('2'))    {        level = 2;        while (KeyDown('2')){};    // wait, until the key is no longer pressed    }        if (KeyDown('3'))    {        level = 3;        while (KeyDown('3')){};    // wait, until the key is no longer pressed    }        }while (level == 0);      




(you could write some functions that automatically wait for
the releasement of a key, after it was pressed, and some action done,
to not having to type this again and again...)


Hope this did the trick.

- Steve (UnshavenBastard)



[edited by - UnshavenBastard on April 25, 2002 1:01:53 PM]
hi i think KeyDown() is what i need! Thankyou for your help. It is very much appreciatted. I am just writing this little game to practice programming in C/C++.

Question.

for KeyDown() what header do i need to include?

And is there such a command or something that will clear all the keyboard buffer???

If you want I could send u the code and maybe if you could suggest some things and give me some hints on how to do things better. I know my main game loop is a total mess! lol But i am learning even if its not going exactly the way i want it to! I thought if i got to know some basic C/C++ programming with a console then when i try to tackle win32 it would be alot easier.

I''m still learning C/C++. Yes i am mixing both languages.

Again thankyou for you repsonse Unshaven Bastard!
Advertisement
hey, just read my before-previous post *entirely* :-)
because...

A)
there you'll find, that KeyDown() ist just a suggestion
for a better name instead of keystate().

// bool keystate(int vk_code)

bool KeyDown(int vk_code) // just a different (more meaningful) name for a bool funtion
{
return (((GetAsyncKeyState(vk_code) & 0x8000) !=0) ? true : false);
}

(forgot the semicolon in the other post here at end of line)


B)
there are some further tips

(read on after the EDIT: block)


I think there's no need for you to clear the keyboard buffer.
The main issue was that waiting for the releasement if a key.
Tell me if it works.
In case not, we can look for other mistakes


Yes, it's definately a good idea to learn the language in console mode, first.
Windows programming is awful pain in the a.. that I don't belive any newbie nor anyone else
is looking forward to...

(I like to mix C++ & C, too)



[edited by - UnshavenBastard on April 25, 2002 9:25:22 AM]

has the mail arrived?



This topic is closed to new replies.

Advertisement