Get key

Pages: 12
Hello. I'm new enough to be called a beginner, but i have a good understanding of C++ so far. I'm creating a game but i dont want it to make you have to type long lines of words to do actions. Instead, i want to integrate "hotkeys" and such. All i need is a list of every key-code so i can pick and shoose what key i want to do what. I dont want to press a key and press enter, i want to have the key be pressed and the action done subsequently after. Help is very much appreciated.

EX:
attack might be 'A'
move might be: AWSD,
etc.

Thank you for your time!

PS:

btw, im using Windows 7, if that makes any difference.
Last edited on
nvm, now i just need my program to recognize a key has been pressed, store the value of the key that has been entered, and do an action with the key as a variable.
Get a lib like SFML: http://www.sfml-dev.org

Also interesting reading:

http://cplusplus.com/forum/articles/28558/
... I just want keys to be pressed, recognized by my program and then an action taken... NOT GRAPHICS.
I know that's what you claim to want.

More interesting reading: http://cplusplus.com/forum/articles/28558/
ok, let me clarify: I'm a beginner, so i'm just making small projects for my self to excersize my C++ skills. I am not makeing a game that i will play/sell or whatever. I just want to see what i can do, and get better at writing C++.
NOOOOOOO

I just want the statement that recognizes a key has been pressed, saves the key as a character. Please stop giving me video game stuff.
I'm confused.

IWishIKnew wrote:
I'm creating a game


IWishIKnew wrote:
Please stop giving me video game stuff.



Also:

ok, let me clarify: I'm a beginner, so i'm just making small projects for my self to excersize my C++ skills


I know, which is why I recommend you read this link: http://cplusplus.com/forum/articles/28558/

You're asking for guidance on how to turn a screw with a hammer. I'm telling you to use a screwdriver and you're saying that's not what you want.
Last edited on
@Disch
What is your problem? The OP has clearly rejected your vision of reality.

Don't tell people what they are trying to do. You can very strongly suggest that they are making a mistake, but if they disagree, then you are wrong.

@IWishIKnew
You need to use one of the following methods:
1. Use the Windows API directly to disable line-buffered input and get individual key presses
2. Use a library like PDCurses to do this for you

If you google my name and things like "SetConsoleMode" and "ReadConsole" you'll get some helpful examples.

What you want to do, BTW, is not trivial -- it has a bit of a learning curve -- but it is not too difficult either.

Good luck!
@op: Are you on windows mate?
Duoas wrote:
Don't tell people what they are trying to do.


Did I? He said pretty clearly in his first post that he was making a game. So I told him to get a lib which makes making games easier. I don't think I jumped to any unreasonable conclusions.

Although granted, I got headstrong, opinionated and pushy. Honestly I thought it was more funny than anything else, but I suppose it wasn't funny for him.

So my apologies, IWishIKnew. I was a jerk and I'm sorry.

Duoas has some good advice, but again I feel I must stress that I disagree with the path you've chosen. IMO you're just making things harder on yourself.
Last edited on
@IWishIKnew: The short version is you can't, the long version is you can but shouldn't in the console.
Haha nothing wrong with doing this in the console. There's just not a quick and easy and cross platform solution, with windows if you just want a keypress it's pretty simple, GetAsyncKeyState()'ll do it, heck or you can just poll for events.
No, playing with the console is not an easy, cross-platform thing to do. Libraries like NCurses help a lot, but you must always cater to peculiarities. Things are easier in a GUI environment, like Windows or X, and significantly easier with cross-platform libraries like SFML.

I actually tend to agree with Disch, though I am not so hard over on the anti-console hobbyism. While it is easy enough to read from the first post, the OP's third post made it quite explicit that he did not want graphical solutions, but that didn't seem to daunt a couple of people from, essentially, telling him that he did not know what he really wanted and sending him back to Disch's anti-console article again.

One of the oldest and most venerated type of games, and good beginner's excercise, are Zork/Adventure games -- console games. From his first posts, it seems to me that the OP is actually trying to do something rather smart with the way the user interacts with it.

Anyway, I hope we haven't chased him away with all this...

Edited for grammar.
Last edited on
If you're just looking for a basic solution, look at _getch(), as defined in conio.h. It returns that last char pressed, which you can pass to your "event handler".

Later on, if you want to do more complicated things, you can check out the Console API as previously mentioned.

The sample from the MSDN entry is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// crt_getch.c
// compile with: /c
// This program reads characters from
// the keyboard until it receives a 'Y' or 'y'.

#include <conio.h>
#include <ctype.h>

int main( void )
{
   int ch;

   _cputs( "Type 'Y' when finished typing keys: " );
   do
   {
      ch = _getch();
      ch = toupper( ch );
   } while( ch != 'Y' );

   _putch( ch );
   _putch( '\r' );    // Carriage return
   _putch( '\n' );    // Line feed  
}

the OP's third post made it quite explicit that he did not want graphical solutions, but that didn't seem to daunt a couple of people from, essentially, telling him that he did not know what he really wanted


The question I asked myself in this situation is "why doesn't he want to use a graphical lib"?

I can only think of two reasons:

1) He thinks it will be too difficult (which I don't think it would be)

2) He doesn't want to use additional libs (in which case even a console solution like Ncurses is out of the picture)


Granted I handled it poorly. Perhaps I should have asked him "why not" rather than redirecting him again. Or just said my piece once and left it at that.

Anyway, I hope we haven't chased him away with all this...


Me too. I was out of line and I regret it. =(
#IWishIKnew

Look here, under my answer, to see if this would be of any help.
http://cplusplus.com/forum/general/53439/
@ultifinitus

Thanks for the tip. Can you help me with the syntax and tell me what polling is? I'm taking C++ 1 step at a time.

@rude/critical/opinionated people

For those of you who are critisizing my methods, forgive me, for I know not what the hell i'm doing because i'm ONLY a beginner and im not getting into graphics JUST yet. I'd like to learn the fundamentals first. Thank you for your patience.

@Duoas- "Libraries like NCurses help a lot"

Can you explain how these libs work? I dont want to download somthing without knowing what's in it. (I like to keep my computer as safe as humanly possible, and that's a hard thing to do, lol)

@whitenite1 and possibly everyone else

So, i understand how to get the key, but to the C++ language, what defines a "key"? I have a list of the virtual key codes but i'm not sure how C++ libs would take them... I've been searching online for keylogging examples (because as you might know, key logging gets keys of every kind) and I played with one of the harmless open source codes and experimented with it. I still dont quite understand what each key is to C++.

ex: VKC for "x" is 0x58. Would this:

1
2
3
4
if (AsyncKeyState(chara) == 0x58)
{
    cout<< "x"<< endl;
}


display the letter x when x is pressed? Or is it some number?

I appreciate your time and effort.
Last edited on
imma try the _getch() and see how that works. Ill post if i've solved my problem.
Pages: 12