Jump to content
  • Advertisement
Sign in to follow this  
utilae

How to convert key pressed into string?

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using C++. I have a message pump, where I test if a key is down, like below:
switch(uMsg)
{
     case WM_KEYDOWN:
     {
         if(wParam=='A') 
	 {
                object.setKeyPressed("A"); 
         }       
     }
//etc . . .
}

I basically want to record what key is pressed, so that I can add the letter onto the end of a string, eg <press Y> Y <press e> Ye <press s> Yes Is there a way to convert the wParam (in the code above) to the character it stands for, rather then having a massive long if statement?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by doho
object.setKeyPressen(wParam);

The problem with this method is that it provides bogus ASCII values. Additionally it dosn't handle "shift" correctly (and the OP's method dosn't either without extra processing).

Share this post


Link to post
Share on other sites
Quote:
Original post by Andrew Russell
Quote:
Original post by doho
object.setKeyPressen(wParam);

The problem with this method is that it provides bogus ASCII values. Additionally it dosn't handle "shift" correctly (and the OP's method dosn't either without extra processing).

I was just trying to show how to avoid the list of if statements.

Share this post


Link to post
Share on other sites
Quote:
Original post by doho
Quote:
Original post by Andrew Russell
Quote:
Original post by doho
object.setKeyPressen(wParam);

The problem with this method is that it provides bogus ASCII values. Additionally it dosn't handle "shift" correctly (and the OP's method dosn't either without extra processing).

I was just trying to show how to avoid the list of if statements.

...Yes, but it dosn't actually work (unless you add a smaller, but still large number of if statements to filter the input).

Share this post


Link to post
Share on other sites
Quote:
Original post by Andrew Russell
Quote:
Original post by doho
Quote:
Original post by Andrew Russell
Quote:
Original post by doho
object.setKeyPressen(wParam);

The problem with this method is that it provides bogus ASCII values. Additionally it dosn't handle "shift" correctly (and the OP's method dosn't either without extra processing).

I was just trying to show how to avoid the list of if statements.

...Yes, but it dosn't actually work (unless you add a smaller, but still large number of if statements to filter the input).

Yea you are right. WM_CHAR is the way to go.

Share this post


Link to post
Share on other sites
Ok, I'm not sure I understand.

Does TranslateMessage() turn the data that is stored in wParam to the character it represents, eg '1' will be come "1" and VK_ESCAPE will become nothing, eg "".

So if I had a string sText, I could add the "1" onto the end of the string. Could someone give me a code example?

Share this post


Link to post
Share on other sites
OK. TranslateMessage checks a message to see if it's a WM_KEYUP message, and if it is translates it to a WM_CHAR message and adds that into your message queue (you will still get your WM_KEYDOWN and WM_KEYUP messages as normal).

Your code for the message loop would look something like:

MSG msg;

while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


Then to process your shiny new WM_CHAR message would look something like this:

switch(uMsg)
{
case WM_CHAR:
object.setKeyPressed(wParam);
break;
}


You won't get WM_CHAR messages for things that arn't charcters, so you don't need to worry about handling them. It also handles users pressing shift and caps-lock and such for you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!