I am new to posting, but have searched these wonderful forums for a couple years.

I am programming a game as a hobby. And I have been searching for a way to read what the user types. e.g. User types in "hello" and I can take that and send it to my server.
My program so far uses a huge switch/case to return a key.


case VK_A or DIK_A:
    return "a";


I cant seem to get Direct Input to do what I would like without that switch/case.
I have also tried GetAsyncKeyState() but it ends up like Direct Input.

I have tried _kbhits(), I can only get it to work on console. I have tried getch(), same thing:

//A really round about way of getting user input

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

using namespace std;

//declare our keys array
string keys[256];

//declare our poll function
string poll_keyboard();

int main()
        //add some letters to represent our key
	keys[27] = "esc";
	keys[97] = "a";
	keys[98] = "b";
	keys[99] = "c";
	keys[100] = "d";

        //start our main loop!
                //get our key that has been pressed
		string recieve = poll_keyboard();

                //say what key was pressed
		cout<< "Key Pressed: " << recieve << endl;

                //if our user presses escape
		if(recieve == "esc")
                        //break from the loop

	} while(true); //loop forever!

        //pause our console and wait for user to press any key

	return 0;

//our poll function!
string poll_keyboard()
        //declare our key and initialize what ever has been pressed
	unsigned key = getch();

        //return our key representation
	return keys[key];

In my game:

//does not work!!!

//if our user presses escape
        //send them off to the main menu
	location = 1;
        //Give the user some time to let go of the escape key!

//declare our key
unsigned key;

//get what key is pressed
key = getch();

//now we find what the key represents
string get = keys[key];

//if our key has something in it
if(get != "")
        //create a message to be posted
	string message = "You pressed: " + get;

        //convert our message to char format
	char *buffer;
	buffer = new char[message.length() + 1];
	strcpy(buffer, message.c_str());

        //post our message in a message box!
	MessageBox(NULL, buffer, "Key Pressed", MB_OK);

But as it is, it only works for console.

Fgets() I tried and failed. But with fgets() I have gotten closer than the others. only with a consecutive message of: ÌÌÌÌÌÌÌÌÌÌ ect.

char key[10];


string input = key;	

if(input != "")
	MessageBox(NULL, key, "Key Pressed", MB_OK);

I have looked into the WM_KEYDOWN/WM_KEYUP. But I come to the same conclusion as Direct Input and GetAsyncKeyState() along with a pre translate message function. If there is still a way to use WM_KEYDOWN/WM_KEYUP, please tell. I have racked my brain to the point of pain trying to understand WM_KEYDOWN/WM_KEYUP(and other ways of input, like sendkeys or something like that).

I have looked long and hard for many days searching Google without end. I have been searching on this website for hours on end as well(its now 1am lol). I am not sure if the keys I searched are correct: get user input keystroke c++ -console -linux win32. Ive also looked up as much info, as I could cram into my brain, on getch(), getchar(), fgets(), WM_KEYDOWN/WM_KEYUP, GetAsyncKeyState().

So my question is: Is there anything I can do to get the users input without a huge switch/case or the like?

My program is a win32 project(not a console). I am using Visual C++ 2008. Running on Windows 7 x64.

Thank you very much for any help.


A brain dead guy. (=- = )

Recommended Answers

All 10 Replies

You sure typed a lot of stuff, but I can't figure out what your problem actually is. It sounds like you tried every type of input possible, and nothing does what you want. So explain what you want, not just what didn't work.

Ah! My bad...

I am looking for something that will return what the user types. Like say the user types "abcdefg" I would like to be able to get what the user is typing the instant they type it.

Is there anything like this?

Sorry if I didn't say it to clearly.

Yes, everything you tried returns what was typed. That's what input functions are designed to do. If they don't,
1) You used the functions wrong
2) Your compiler is acting strange

I will go with 1. My compiler seems to be working just fine.

I am thinking that most of the functions I am looking for, and can only find, are for console applications and not WIN32 applications. Besides GetAsyncKeyState() and the windows messaging thing(WM_KEYDOWN/WM_KEYUP).

Is there truly nothing I can do except a huge...

string return_key()

  return "A";



Please I am begging for any kind of help. Q- Q

With what? Your description was so convoluted that we have no idea exactly what you are trying to do, just a bunch of half descriptions of what you don't want to do.

Be specific.

Ok I'll try to be as specific as I can.

I am trying to find a function or a way to receive a keystroke. If 'A' is pressed on the keyboard, I want to add it to a string.

//declare our users name
string name;


//if our user has entered less then 22 characters
if(name.length() <= 22)
  //check if user has pressed any key
  name += Get_Key();

Does that help at all? Or am I still not making any sense? :x

p.s. Sorry for all the nonsense in the above posts... I have been really worked up about finding any way do do this...

If you want to do it using win32 api console functions then here is a thread you might be interested in reading.

I assume you've seen this but just in case...

I only know the console functions so I don't think I can help.

If you want to do it using win32 api console functions then here is a thread you might be interested in reading.

Thank you for the link, but it seems to only work for console applications.

I assume you've seen this but just in case...

I only know the console functions so I don't think I can help.

I have looked at windows message thingy many, many times. But every time I do, my brain locks up... lol... So I got some help understanding a fraction of it.

This is what I implamented:

name += poll_keyboard(msg);


string poll_keyboard(MSG msg)
  TCHAR key;

    case WM_CHAR:
          //grab our key from wParam
          key = (TCHAR) msg.wParam; 

          //if our key is either a-z or A-Z or 0-9
          if(key > 96 && key < 123 || key > 64 && key < 91 || key > 47 && key < 58)
            //create a stream to convert our tchar to string
            stringstream stream;

            //declare a string to hold our converted tchar
            string str;

            //insert our tchar into the stream
            stream << key;

            //recieve our tchar as string from the stream
            stream >> str;

            //return our character
            return str;


And this did the trick. Its still a switch case, but its not a huge if/else thing.

Is this the best way to get my character?

p.s. name ends up getting 'aaaaaaaaaaaa' when I press the A key. Is there a way around this? I will look this up, but at least some help would be nice.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.