How do I capture the value for a pressed key?

Started by
12 comments, last by CmdJoeM 21 years, 4 months ago
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
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
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
The only problem is how do you capture that number inside of the WM_KEYDOWN?
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]
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
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.
case WM_KEYDOWN:    switch(lParam)    {         case ''0'':               /* Handle 0 */         case ''1'':               /* Handle 1 */          .....    }
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]
_______________________________________________________________________Hoo-rah.
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.
Also remember that the numpad will give you ''VK_NUMPAD0'' or some such instead of ''0''. :/
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]
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

This topic is closed to new replies.

Advertisement