Hello,
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.
e.g.
switch(int)
{
case VK_A or DIK_A:
return "a";
break;
ect..
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!
do
{
//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
break;
}
} while(true); //loop forever!
//pause our console and wait for user to press any key
system("pause");
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
if(KEY_DOWN(VK_ESCAPE))
{
//send them off to the main menu
location = 1;
//Give the user some time to let go of the escape key!
Sleep(1000);
}
//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];
fgets(key,sizeof(key),stdin);
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.
sincerely,
A brain dead guy. (=- = )