Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CmdJoeM

How do I capture the value for a pressed key?

This topic is 5794 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

In my college course I am required to make a calulator that has buttons. My problem is how can I capture the value of the key pressed, like 0-9, after us WM_KEYDOWN to display the correct number in the diplay? Thank You, Joe

Share this post


Link to post
Share on other sites
Advertisement
Warning: This is almost certainly not the best way to handle the situation, but I think it''ll work.

OK, you must in some sense already have the ascii code for the key that was pressed. The ascii code for ''0'' is 48, and in the ascii system, the numbers are arranged starting at 0 and moving up to 9. So, if you subract 48 from the ascii code, you''ll get the numeric value of the character.

Hope that helps,

John B

Share this post


Link to post
Share on other sites
Huh? You've already got the number haven't you? Since you're using it "to display the correct number in the diplay[sic]"

Sorry, I must be missing something here - could you re-phrase the question perhaps?

John B

[edited by - JohnBSmall on December 7, 2002 7:45:30 PM]

Share this post


Link to post
Share on other sites
I THINK this is right:

The WM_KEYDOWN message contains the lParam parameter, which you can check against a specific list of possible key values. Don''t remember how you check it, but you should be able to find it in MSDN. I''d get out my Windows book right now, but my room is sort of cluttered with all my new computers (see the Lounge if you want to read about it).

Hope that helps.

Share this post


Link to post
Share on other sites

case WM_KEYDOWN:
switch(lParam)
{
case ''0'':
/* Handle 0 */
case ''1'':
/* Handle 1 */
.....
}

Share this post


Link to post
Share on other sites
i'm not sure if this is technically "correct" for windows programming, but i've defined this macro:

#define keystate(key) (GetKeyState(key)&0x80)

that way, in your message switch, it's something like

case WM_KEYDOWN:
// this is for 0
if(keystate('0'))
{
}
break;

only letters and numbers use the ASCII key codes, others like the arrows use things like VK_UP and VK_PRINTSCREEN.

[edit] ook, rotos posted as i did... well, same basic idea..

[edited by - Drakex on December 7, 2002 9:20:09 PM]

Share this post


Link to post
Share on other sites
Maybe I''m doing this wrong.

My code is:

void CCalculatorDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{

switch(LPARAM)
{
case 0:
MessageBox("Hi","Hi",MB_OK);
break;
}
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

I keep getting the error illegal error at this type of expression. What I''m doing is a test I want a message box to pop up when the 0 key is hit. Any more help would be greatly appreicated.

Share this post


Link to post
Share on other sites
Also remember that the numpad will give you ''VK_NUMPAD0'' or some such instead of ''0''. :/

Share this post


Link to post
Share on other sites
Well, without looking very hard, I can see 3 things wrong with that code.
1) LPARAM is a type. If you want a variable you'll have to declare it somewhere (call it lParam or something).

2) case 0: would enter the code if a key with virtual key-code 0 was pressed. It should be case '0': (in single quotes). If you want to support the number key-pad as well, you should have:
case '0':
case VK_NUMPAD0:
3) Your call to MessageBox is missing the first parameter (the window handle).

John B

[edited by - JohnBSmall on December 8, 2002 4:59:16 PM]

Share this post


Link to post
Share on other sites

  • 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!