WIN32 c++ keyboard input
Howdy,
How do you get input from a keyboard and assign it to an integer? I want to be able to... say... get input from the keyboard and use it as coordinates for Polygons and stuff like that.
If anyone can help me, it would be greatly appreciated.
Thanks,
Chris.
Well the normal (not easiest...) way of doing this is by using the KEYUP & KEYDOWN messages from the win32 API :)
Example:
In your main loop,
inside your winmain function you want to run a main loop - say, if you're in a game and in this loop you translate and dispatch your messages -- and check to see if the key you are looking for hs been pressed. So,
if(keys['g']){// g pressed, do something!}
hth, I know it is butt ugly -- but there are a million win32 tutorials on google -- many follow this kinda idea.
That said, if you're not after IO in win32 C/C++ -- I guess none of this will help you :S.
The other way to get input is to use a library -- which can make things somewhat easier.
~Shiny.
Example:
In your main loop,
bool keys[256] // use this to keep track of keys...hopefully keyboard doesn't have more than this!bool active = false; // random bool for example if prog is running... -- inside your wndproc function...switch(MSG) // switch on the message that wndproc gets...{ case WM_ACTIVATE: // check to see if window activated... { if (!HIWORD(wParam)) // see if minimized... { active=TRUE; // program is running, so set active true! } else { active=FALSE; // program done...active false. } return 0; // Return To The Message Loop } case WM_CLOSE: // should we close? { PostQuitMessage(0); // Send quit message to windows. return 0; } case WM_KEYDOWN: // Here's what you're interested in! -- check to see if someone pressed a key. { keys[wParam] = TRUE; // set the key that person pressed as true. return 0; } case WM_KEYUP: // This message arrives when person lets go of key. { keys[wParam] = FALSE; // set the right key as false. return 0; }}
inside your winmain function you want to run a main loop - say, if you're in a game and in this loop you translate and dispatch your messages -- and check to see if the key you are looking for hs been pressed. So,
if(keys['g']){// g pressed, do something!}
hth, I know it is butt ugly -- but there are a million win32 tutorials on google -- many follow this kinda idea.
That said, if you're not after IO in win32 C/C++ -- I guess none of this will help you :S.
The other way to get input is to use a library -- which can make things somewhat easier.
~Shiny.
Woah, I completely messed up there (deleted my worthless post). LOL. I thought you needed to get the mouse movement, not keyboard input. My bad! Thanks for fillin' in for me, Shiny.
Np man, but I don't think you really needed to delete your message -- after all, mouse movement is useful too -- not all that many games on pc -don't- use mouse in some way these days! Of course, win32 mouse? Well, direct Input FTW.
~Shiny.
~Shiny.
Thanks for that. But I can't seem to get it to work properly. I don't quite see how i can "integrate" it into my program. Sorry for being a nuisance!
I was trying to get it to work in something like this:
The only problem is that the integers aren't recognised outside of their case:(
Please let me know if i've got it ALL wrong. Any criticizm will be taken with good humour ( depending how late it is ;) ).
Sorry!
Thanks!
I was trying to get it to work in something like this:
switch(msg) { case WM_ACTIVATE: break; case WM_KEYDOWN: switch(wParam) { case VK_ESCAPE: DestroyWindow(hwnd); break; case VK_SPACE: bool rectangle = true; break; } break; case WM_PAINT: hDC = BeginPaint(hwnd, &Ps); int a, b, c, d; if(rectangle == true) { a = 10, b = 10, c = 200, d = 200; } Polygon(hDC, a, b, c, d); EndPaint(hwnd, &Ps); DeleteObject(NewBrush); break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); break; }}
The only problem is that the integers aren't recognised outside of their case:(
Please let me know if i've got it ALL wrong. Any criticizm will be taken with good humour ( depending how late it is ;) ).
Sorry!
Thanks!
You can't do it like that - not only are the variables only valid in their case block, they're only valid through one loop of the window proc, meaning the variables will be garbage the next time you get a message. You could make them all static, but I'd recommend looking at using your wndproc in a class. There's a tutorial on My Site showing how to do that. Then you can make all the relevant variables member variables and access them from wherever you like.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement