Keyboard Question
Hello all, I'm new to this board and new to mac game programming as well.
I did however write a couple of small games for the dark side, using Direct3D and all the really weird windows stuff. I've started working on a fast paced "2.5D" shoot em up game in X using OpenGL and the GLUT tools in X in plain old C. Now here's my question:
At the moment I'm using the GLUT GlutKeyboardFunc(...) for getting user events, but I'm not really happy with that, because GLUT uses the OS' keyboard repeat rate and only reacts when a key is pressed. I'd rather have a KeyboardFunc which detects KEY_DOWN and KEY_UP events and lets me specify myself about the timing of events. I hope I made myself clear.
Any ideas on how to do that? Should I cut all the GLUT stuff out, and do the OS things like getting my window up and running, getting the focus and keys myself?
Your help is much appreciated.
thanks -ROGUE-
I did however write a couple of small games for the dark side, using Direct3D and all the really weird windows stuff. I've started working on a fast paced "2.5D" shoot em up game in X using OpenGL and the GLUT tools in X in plain old C. Now here's my question:
At the moment I'm using the GLUT GlutKeyboardFunc(...) for getting user events, but I'm not really happy with that, because GLUT uses the OS' keyboard repeat rate and only reacts when a key is pressed. I'd rather have a KeyboardFunc which detects KEY_DOWN and KEY_UP events and lets me specify myself about the timing of events. I hope I made myself clear.
Any ideas on how to do that? Should I cut all the GLUT stuff out, and do the OS things like getting my window up and running, getting the focus and keys myself?
Your help is much appreciated.
thanks -ROGUE-
I don't know OpenGL and GLUT, however Cocoa lets you do the "OS things" with little pain. Look at the Cocoa porting of NeHe's examples (http://nehe.gamedev.net/). For example, you can look at lesson 7.
You can do this just fine with GLUT.
glutKeyboardUpFunc registers a function just like glutKeyboardFunc, but called when a key is released. Similarly glutSpecialUpFunc.
You can disable keyboard repeat using glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);, and reset it with glutSetKeyRepeat(GLUT_KEY_REPEAT_DEFAULT);. Make sure you reset it or you'll be making a trip to the System Preferences -- it doesn't get automatically reset when your app quits.
glutKeyboardUpFunc registers a function just like glutKeyboardFunc, but called when a key is released. Similarly glutSpecialUpFunc.
You can disable keyboard repeat using glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);, and reset it with glutSetKeyRepeat(GLUT_KEY_REPEAT_DEFAULT);. Make sure you reset it or you'll be making a trip to the System Preferences -- it doesn't get automatically reset when your app quits.
Thanks Junyz and SadCookie.
If I got this right, I can disable repeat and move my vessel or trigger photon torpedos as long as the key is down, and as fast as I like, and reenable the key repeating on exit of the game? Thats cool. Another thing I realized is the following: I usse four keys for moving and four for shooting. Say im pressing down the key for shooting to the left. Bam, bam, bam, off go the torpedos. But as soon as I hit another key, one that isn't used for the game at all, it stops shooting. For your info, I'm checking for keys with a switch statement.
Keep it coming, and thanks a lot
luke
If I got this right, I can disable repeat and move my vessel or trigger photon torpedos as long as the key is down, and as fast as I like, and reenable the key repeating on exit of the game? Thats cool. Another thing I realized is the following: I usse four keys for moving and four for shooting. Say im pressing down the key for shooting to the left. Bam, bam, bam, off go the torpedos. But as soon as I hit another key, one that isn't used for the game at all, it stops shooting. For your info, I'm checking for keys with a switch statement.
Keep it coming, and thanks a lot
luke
You can only detect a certain number of normal keys down at a time, I'm afraid. Special keys, like the modifier keys and (?) the arrow keys, will always be detetcted correctly however many other keys are held down.
Checking for key down and key up (rather than key pressed or key repeat) will solve the multiple keys at once problem, up to about 5-10 keys at least
thats great, cookie. can't wait to get back home to actually fiddle with my code again...
I don't think I'll run into 'too many keys' problems, though. the max keys you're allowed to use in my game is three: shoot in only one direction at any given time and move diagonally using two keys.
I don't think I'll run into 'too many keys' problems, though. the max keys you're allowed to use in my game is three: shoot in only one direction at any given time and move diagonally using two keys.

