Click to See Complete Forum and Search --> : Read the keyboard in a win32API program.


arnie
January 22nd, 2004, 11:45 AM
Hi All,
I need to read keys pressed in my program. I tried to use
case WM_KEYDOWN:
hwnd::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags ))
{
switch(nChar)
{
case 102: MessageBox(hwnd,"You pressed the 6 key","",MB_OK|MB_ICONINFORMATION);
break;
}
hwnd::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags );

I get:
D:\Projects\Win32C\Main.c(102) : error C2143: syntax error : missing ';' before ':'
D:\Projects\Win32C\Main.c(131) : error C2143: syntax error : missing ';' before ':'

I don't have any edit box's or dialog box's to set focus to in the program. I just need to get input from the number keys pressed.
I could read key input with the VK_ type parameters but I can't find the ones for numbers.

Anyone know how to do this or where i can find a list of the VK_?

Thank,
AJ.

hankdane
January 22nd, 2004, 04:01 PM
I'm not sure about your compiler errors, although the construction:


case WM_KEYDOWN:
hwnd::OnKeyDown()


looks a little weird.

It appears what you want to do is react to certain keystrokes from the user. You have several options.

You can listen to the messages in your main message loop. You want to look for WM_KEYDOWN and/or WM_KEYUP (depending on when you want to react to the key). You won't get these messages in your windows process, there you will see it as a WM_CHAR message.

If there are only a few keys you want to react to, such as the number keys, you could also add them as accelerators. Insert an accelerator table into your resources and use TranslateAccelerator() in your message loop.

A third option is to use a journal hook, but I would explore the above methods first.

Sam Hobbs
January 23rd, 2004, 11:47 AM
Originally posted by arnie
I don't have any edit box's or dialog box's to set focus to in the program.You should. There is probably no reason not to except that I assume you are learning Windows programming and are trying to avoid creating a control. However since you don't tell us what you do have, it is difficult for us to suggest anything.

We don't know what lines 102 and 131 are, so we can't say what the error is. Error messages don't say anything if we don't know what line they are for.

Probably the main problem is that you are still learning C++ and are not sure of the correct syntax. It might be better to learn C++ without Windows programming first.

Sam Hobbs
January 23rd, 2004, 11:50 AM
Originally posted by hankdane
You can listen to the messages in your main message loop. You want to look for WM_KEYDOWN and/or WM_KEYUP (depending on when you want to react to the key). You won't get these messages in your windows process, there you will see it as a WM_CHAR message.I might misunderstand what you are saying, but as far as I know, anywhere that we get a WM_CHAR message, we also get a WM_KEYDOWN and a WM_KEYUP message.