Results 1 to 6 of 6

Thread: Multiple Key press

  1. #1
    Senior Member Regular Contributor Swiftless's Avatar
    Join Date
    Apr 2005
    Location
    Australia
    Posts
    118

    Multiple Key press

    When I press W, my camera translates the scene inwards. When I press A, the camera translates the scene to the left. S and D do the opposites. When I press W and then S or D, it stops translating inward and starts translating across. How can I make it so that when I press W and S, it translates both across and inward at the same time?

    Here is my camera code:
    Code :
    void camera (void) {
    	int posz, posx;
    	posz = zpos + 1;
    	posx = xpos + 1;
    	ypos = map[posx][posz];
    	ypos += 2;
    	glRotatef(xrot,1.0,0.0,0.0);  
    	glRotatef(yrot,0.0,1.0,0.0); 
    	glTranslated(-xpos,-ypos,-zpos);
    	glutPostRedisplay();
    }
    Here is my keypress code:
    Code :
    void normalKey (unsigned char key, int x, int y) {
    	float yrotrad;
    	switch (key) {
    		case 'w' :
    			yrotrad = (yrot / 180 * 3.141592654f);
    			xpos += float(sin(yrotrad)) * 0.2;
    			zpos -= float(cos(yrotrad)) * 0.2;
    			break;
     
    		case 'a' :
    			yrotrad = (yrot / 180 * 3.141592654f);
    			xpos -= float(cos(yrotrad)) * 0.2;
    			zpos -= float(sin(yrotrad)) * 0.2;
    			break;
    }
    And here is my main code:
    Code :
    	glutIgnoreKeyRepeat(0);
    	glutKeyboardFunc(normalKey);
    	glutSpecialFunc(pressKey);
    	glutSpecialUpFunc(releaseKey);
    (I am using GLUT in C++)

  2. #2
    Senior Member Regular Contributor
    Join Date
    Aug 2003
    Posts
    369

    Re: Multiple Key press

    I think you'd have to stop using glut and use an API that can handle simultaneous key presses, like DirectX or SDL. (I'm not sure about SDL, as I haven't used it but it should be there.) I believe that even the win32 can't handle simultaneous key presses.

  3. #3
    Senior Member Regular Contributor Swiftless's Avatar
    Join Date
    Apr 2005
    Location
    Australia
    Posts
    118

    Re: Multiple Key press

    Umm, where did that come from? Because I know for a fact that GLUT can handle them. I downloaded some code for a tutorial on GLUT and keypress and it does it perfectly (from LightHouse 3D). I am just having trouble making it possible with my already existing code.

    I also know that Win32 windows can handle multiple keypresses I used them in Visual Basic, im not sure about SDL either, and I don't like Direct X.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jan 2001
    Location
    Ukraine
    Posts
    101

    Re: Multiple Key press

    Take a look at glutKeyboardUpFunc(). Use it in conjunction with glutKeyboardFunc() - in latter set press flag in former - reset it, and use flag value in your display function. You always will know whether you key is pressed or released and will be able to implement all appropriate movements...

  5. #5
    Guest

    Re: Multiple Key press

    The problem is because your keypress code is using a switch statement which is only telling the program what to do when you press one key OR the other - not both.

    What happens after the 'break' statement in your switch is that it drops out of the code and doesn't perform anything else. So it will only allow you to push one key at a time! It will only run the code for the 'a' key if it didn't run the code for the 'w' key!

    What you really need to use is an IF statement that says, if 'w' key is pressed then increase/decrease your forward movement value. Then you need another IF that says, if 'a' is pressed then increase/decrease your sideways movement value.

    That way the two values work independantly of each other and should respond to your key pressed accordingly.

  6. #6
    Senior Member Regular Contributor Swiftless's Avatar
    Join Date
    Apr 2005
    Location
    Australia
    Posts
    118

    Re: Multiple Key press

    Hmm, I will give it a go. But then how does the Lighthouse 3D tutorial work it uses the case function, and i switched over to it because it meant less code.

    Hmm, just tried it and it doesn't work.

    Hmm.... I am out of ideas.

Similar Threads

  1. Boucing Ball on key press
    By amolkarale in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-09-2014, 08:53 PM
  2. Detecting key press and key release
    By in forum OpenGL: Linux
    Replies: 2
    Last Post: 10-27-2003, 07:40 PM
  3. Glut Key Press
    By LostInTheWoods in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-09-2002, 05:45 PM
  4. Weird! No response to key press
    By xingyili in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-12-2001, 07:15 PM
  5. key press
    By lee in forum OpenGL: Linux
    Replies: 3
    Last Post: 07-29-2001, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean