Jump to content
  • Advertisement

Archived

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

rick_appleton

How do you do keyboard input

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

I've been thinking of a good way to handle the keyboard input in a game for some time now. I don't really like the standard way of doing it with GetKeyState(..), and I thought it would be cool if I could just register a key and a function bound to that key somewhere. I found this article and it uses function pointers to do exactly what I wanted. But.... My keys will almost always be calling member functions of some class or other (ie.: spacebar = ship.fire() ), and the function pointers won't point to those, unless I know what class it points to first. But I DON'T know that. Besides which, I would like to reuse this code without modification in other projects. So what I want is a class that queries the keyboard input, and can call any member function of any class (presuming a void function(void)) without needing extra specific code in the class itself or in the called class. Does anyone know of a way to do this? If not, how do you guys do your keyboard input in an encapsulated way? edit: I already read through www.function-pointers.org for help, but couldn't find a solution there. They even say that what I want to do is impossible. [edited by - rick_appleton on September 21, 2003 9:55:02 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well, I find it easy enough just to use DirectInput and check for keys being pressed that way. I think the latest version of DirectInput has some kind of mapping ability, but I''m not sure of the specifics. NeHe''s method just stores a pointer to a function in an array and then calls it when a key''s pressed. So this just involves a void variable, which you''ll have to set at run time. It can than be called normally. Make sure that if you do this, that the pointer points to an existant function, or you will or course crash your program

Share this post


Link to post
Share on other sites
Puffy_Taco: I understand that. But the problem is that this can not be directly used with member functions. C++ treats normal functions/static member functions differently from non-static member functions. That is giving me the problems.

Share this post


Link to post
Share on other sites
Check out the boost library (www.boost.org) and look up functors (At least, that''s what I think they''re called). They can handle member function pointers. Either that or make you''re own, check out the Enginuity II article for that.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!