Keyboard Question

rogue
Unregistered
 
Post: #1
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-
Quote this message in a reply
Member
Posts: 22
Joined: 2003.06
Post: #2
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
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.
Quote this message in a reply
rogue
Unregistered
 
Post: #4
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
Quote this message in a reply
w_reade
Unregistered
 
Post: #5
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
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 Wink
Quote this message in a reply
rogue
Unregistered
 
Post: #7
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. Cool
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Keyboard Handling skyhawk 10 6,385 Oct 26, 2010 08:47 AM
Last Post: skyhawk
  keyboard limitations daveh84 8 4,312 Feb 13, 2009 02:48 PM
Last Post: ferum
  Cocoa Keyboard help Iceman 5 4,022 Aug 1, 2005 06:06 PM
Last Post: nabobnick