Jump to content
  • Advertisement
Sign in to follow this  
japro

[java] Java Keyboard/Mouse input "serialization" issue

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

So wrote this little applet:
http://progsch.net/applets/proto.html

The problem is that the controls seem to be entierly broken on Macs, and sometimes they also behave weirdly on my Linux boxes. What happens often on linux is that keys seem to get stuck and only get "unstuck" if they are tapped again (i get the same thing in minecraft btw.). Also very rarely the mouse dragging stops working entirely. Mac users generally report it to not respond to any controls at all. I somehow once got it to respond on my mothers macbook by pretty much just randomly pressing keys/changing focus/clicking around, but I have no idea what triggered it.

I use the following input serializer class:
[source lang="java"]
package net.progsch;

import java.awt.Point;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayDeque;
import java.util.Queue;

public class InputSerializer implements KeyListener, MouseListener, MouseMotionListener {
private boolean [] keys = new boolean[65536];
private Point mousePos = new Point();
private Queue<InputEvent> events = new ArrayDeque<InputEvent>();

public InputSerializer() {
for(int i = 0;i<65536;++i)
keys = false;
}

public InputEvent poll() {
return events.poll();
}

public Point getMousePosition() {
return mousePos;
}

public boolean isKeyDown(int code)
{
return keys;
}

@Override
public void keyPressed(KeyEvent arg0) {
events.add(arg0);
keys[arg0.getKeyCode()] = true;
}

@Override
public void keyReleased(KeyEvent arg0) {
events.add(arg0);
keys[arg0.getKeyCode()] = false;
}

@Override
public void keyTyped(KeyEvent arg0) {
events.add(arg0);
}

@Override
public void mouseDragged(MouseEvent arg0) {
events.add(arg0);
}

@Override
public void mouseMoved(MouseEvent arg0) {
events.add(arg0);
mousePos = arg0.getPoint();
}

@Override
public void mouseClicked(MouseEvent arg0) {
events.add(arg0);
}

@Override
public void mouseEntered(MouseEvent arg0) {
events.add(arg0);
}

@Override
public void mouseExited(MouseEvent arg0) {
events.add(arg0);
}

@Override
public void mousePressed(MouseEvent arg0) {
events.add(arg0);
}

@Override
public void mouseReleased(MouseEvent arg0) {
events.add(arg0);
}
}
[/source]

And the part processing the events looks like:
[source lang="java"]
private void step() {

if(input.isKeyDown(KeyEvent.VK_A))
alpha += 0.02;
if(input.isKeyDown(KeyEvent.VK_D))
alpha -= 0.02;
if(input.isKeyDown(KeyEvent.VK_W) && beta<0)
beta += 0.02;
if(input.isKeyDown(KeyEvent.VK_S) && beta > -Math.PI/2)
beta = Math.max(beta-0.02, 0.0001-Math.PI/2);

InputEvent e;
while((e = input.poll())!=null)
{
if(e instanceof KeyEvent)
{
KeyEvent ke = (KeyEvent) e;
if(ke.getKeyCode() == KeyEvent.VK_SPACE && ke.getID() == KeyEvent.KEY_PRESSED)
{
map.setSeed((int)(Math.random()*1000000));
map.generate();
}
}
else if(e instanceof MouseEvent)
{
MouseEvent me = (MouseEvent) e;
if(me.getID() == MouseEvent.MOUSE_PRESSED && (me.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK)!=0)
{
mouseDownPos = me.getPoint();
}
if(me.getID() == MouseEvent.MOUSE_DRAGGED && (me.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK)!=0 && Math.cos(beta)>0.001)
{
offx -= Math.cos(alpha)*(double)(me.getX()-mouseDownPos.x)/SCALE - Math.sin(alpha)/Math.cos(beta)*(double)(me.getY()-mouseDownPos.y)/SCALE;
offy -= Math.sin(alpha)*(double)(me.getX()-mouseDownPos.x)/SCALE + Math.cos(alpha)/Math.cos(beta)*(double)(me.getY()-mouseDownPos.y)/SCALE;
mouseDownPos = me.getPoint();
}
}
}
}[/source]

I'm at a loss since googling for "java input problem" or similar is way too unspecific :D

Share this post


Link to post
Share on other sites
Advertisement
I'm guessing that the "step()" method doesn't run from the AWT Event Dispatch thread, so it looks like a synchronization problem.

Just considering the "keys" array, try something like this:

[source lang="java"]

public boolean isKeyDown(int code) {
synchronized(this) {
return keys;
}
}

@Override
public void keyPressed(KeyEvent arg0) {
events.add(arg0);

synchronized(this) {
keys[arg0.getKeyCode()] = true;
}
}

@Override
public void keyReleased(KeyEvent arg0) {
events.add(arg0);

synchronized(this) {
keys[arg0.getKeyCode()] = false;
}
}
[/source]

Also with regards to focus, just check which component you adding the listeners too. Is it the JFrame or some other container?
That component will need focus in order to dispatch any events.

Share this post


Link to post
Share on other sites
My guess is that your InputSerializer class is losing focus. Only the actual class with input focus will receive keyboard messages. Add a focus listener to it to see if this is happening.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!