EventQueue on key released?(ANSWERD), simple Keyboard class example posted

Hello.

Does JInput send an event when a key is released??

basicly I wan’t to know if this check is valid:


m_keyboard.poll();
		m_keyboardQueue = m_keyboard.getEventQueue();
		Event event = new Event();
		while( m_keyboardQueue.getNextEvent(event))
		{
			float data = event.getComponent().getPollData();
			if(data != 0.0f )
			{
				//Key is down, this check will work for sure


			}
			else
			{
				//Key was released, does this work??
			}
		}

Hi

try it :), but yes, it should raise an event when ever a components value changes.

HTH

Endolf

Thnx.

I will make up for my laziness and post my resultning keyboad class ::slight_smile:


package Input;

import java.util.HashMap;

import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Event;
import net.java.games.input.EventQueue;
import net.java.games.input.Keyboard;
import net.java.games.input.Component.Identifier;
import net.java.games.input.Component.Identifier.Key;
import net.java.games.input.Controller.Type;

public class CKeyboard
{
	Keyboard	m_keyboard;
	EventQueue  m_keyboardQueue;
	HashMap<Identifier, Boolean> m_keyDown;
	public CKeyboard()
	{
		
		Init();
	}
	private void Init()
	{
		Controller[] inputDevices = ControllerEnvironment.getDefaultEnvironment().getControllers();
		m_keyboard = null;
		for( Controller c : inputDevices)
		{
			if( c.getType() == Type.KEYBOARD)
			{
				m_keyboard = (Keyboard)c;
				break;
			}
		}
		if( m_keyboard != null)
		{
			
			m_keyDown = new HashMap<Identifier, Boolean>();
			for( Component c : m_keyboard.getComponents())
				m_keyDown.put(c.getIdentifier(), false);
			
		}
	}
	public void capture()
	{
	    if( m_keyboard == null )
			return;
		
		m_keyboard.poll();
		m_keyboardQueue = m_keyboard.getEventQueue();
		Event event = new Event();
		while( m_keyboardQueue.getNextEvent(event))
		{
			float data = event.getComponent().getPollData();
			if(data != 0.0f )
			{
				m_keyDown.put(event.getComponent().getIdentifier(), true);
			}
			else
			{
				m_keyDown.put(event.getComponent().getIdentifier(), false);
			}
		}
	}
	public boolean isKeyDown( Key k)
	{
		return m_keyDown.get(k);
	}
}


I made a quick test and it seems to work fine.
I haven’t worked with HasMaps before, they are quite nice :slight_smile:

I also have a question about mouse input.

I can’t seem to get the correct relative mouse position.

if I use event queue for the mouse it almost works, but when I hold my mouse still I still get realtive movemnt of atleast 1.0f;

if I use this approatch:


m_mouse.poll();
m_deltaMouse.x = m_mouse.getX().getPollData()
m_deltaMouse.y = m_mouse.getY().getPollData()

the values are constant 0.0 in both X and Y.

I use windows xp.

my test app looks like this:


public static void main( String[]args)
	{
		
	    JFrame f = new JFrame("This is a test");
	    f.setSize(1024, 768);
	   
	    Container content = f.getContentPane();
	    content.setBackground(Color.white);
	    f.addWindowListener(new ExitListener());
	    f.setVisible(true);
	    
	    while( true )
	    {
	    	Input.getInstance().capture();
	    	//Keyboard works fine
                if( Input.getInstance().getKeyboard().isKeyDown(Identifier.Key.Q))
	    	{
	    		System.exit(0);
	    		break;
	    	}
                 //event mode work's poor, "imidate" mode don't work at all"
	    	System.out.println("delta mouse x : "+Input.getInstance().getMouse().getDelta().x );
	    	System.out.println("delta mouse y : "+Input.getInstance().getMouse().getDelta().y );
	    	 
                 //Mouse has no dead zone....
	    	System.out.println("\ndead zone x : "+Input.getInstance().getMouse().getDeadZone().x );
	    	System.out.println("\ndead zone y : "+Input.getInstance().getMouse().getDeadZone().y );
	    }
		
	}

}
 class ExitListener extends WindowAdapter {
	  public void windowClosing(WindowEvent event) 
	  {
	    System.exit(0);
	  }
	  
	}

btw: I don’t check mouse and keyboard poll in the same loop, does the getEventQueue() “steal events”, feels likt that’s not the case ???