Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyListener and multiple key presses

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
EU
hi,

im messing around with JoGL, java and opengl.

ive come accross a problem when im moving around the screen. only one button press is recognised at a time. so i can move forward but cant strafe to the left and right or look up and down at the same time.

i know this is to do with the method im using from the KeyListener interface.
Code:
public void keyPressed(KeyEvent e)

is there another interface that will take in multiple key strokes at a time so i can do stuff with each of them, i havnt had any luck finding one yet.

thanks for your help.
 
The only way to have wo key pressed "at a time" is with Ctrl, Alt or something, that can be achieve with the getModifiers method from keyEvent.

Cheers,
Dian
 
it'll be later on before il be able to try that out. can you set the ctrl and alt to any key you want? would this get messy with more button presses. e.g. moving forward, looking up and then pressing shoot, or reload etc.

ive developed a similar application in c++ and that was fairly straight forward in regards to the button presses.

what would be your opinion on creating a thread pool that would take in the button presses and then pass them one at a time into the keypressed method?

that way id be able to change the 3d poitional cordinates and then the rotate values but ive a feeling this would be drain on resources.

thanks for the help
 
I don't think I'm following you.

Can you post an actual combination of keys and the result you're expecting? I don't know what do you mean with "looking up and the pressing shot".

Cheers,
Dian
 
its 3d graphics so you'd be walking forward. you could also be looking up at the same time as walking forward and then maybe firing gun shots for example.

the below code is just getting the values from the key presses.

Code:
public void keyPressed(KeyEvent e)
{
      if ( e.getKeyCode() == KeyEvent.VK_Z )
    	{
    		y += 0.5f;
    	}

    	if( e.getKeyCode() == KeyEvent.VK_X )
    	{
    		y -= 0.5f;
    	}

    	if( e.getKeyCode() == KeyEvent.VK_UP )
    	{
    		z -= 0.5f;
    	}

    	if( e.getKeyCode() == KeyEvent.VK_DOWN )
    	{
    		z += 0.5f;
    	}   	

    	if( e.getKeyCode() == KeyEvent.VK_LEFT )
    	{
    		x += 0.5f;
    	}

    	if( e.getKeyCode() == KeyEvent.VK_RIGHT )
    	{
    		x -= 0.5f;
    	}
}
 
Well, then I think it will be a little more complicated and you'll have to take a look at keyReleased event too.

For example, user pres up (looks upstairs) and forward (walks) and then just walks. This would be the event sequence

up keyPressed -> flag "head up" activated.
forward key Pressed -> flag "move forwars" activated.
up keyReleased -> flag "head up" deactivated.

Of course, you'd need another thread calculating the position and movement of you brave warrior depending on those flags.

This is just an idea, must be more ways, but this seems ok for me.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top