Results 1 to 2 of 2
  1. #1
    pghazanfari is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Java keypress detection help

    I created a keysystem which records which keys have been pressed which I use at another point in my program. The problem is, I can't get it to tell if the key has been pressed after already having been released. It worked for me in c# xna game studio, but in Java it detects like 100 presses before stopping detection because it is already down.

    Here's my code:

    Key.java
    Java Code:
    package Controls;
    
    public class Key 
    {
    	private int keycode;
    	protected boolean isdown;
    	protected boolean isdownafterrelease;
    	protected boolean released;
    	protected long previoustime;
    	protected long timebetween;
    	
    	public enum KeyEvent { Press, Released };
    	
    	public Key(int code, long timeBetween)
    	{
    		keycode = code;
    		isdown = false;
    		isdownafterrelease = false;
    		released = true;
    		timebetween = timeBetween;
    		previoustime = 0;
    	}
    	
    	public Key(int code)
    	{
    		this(code, 0);
    	}
    	
    	public int getKeyCode()
    	{ return keycode; }
    	
    	public boolean isDown()
    	{ return isdown; }
    	
    	public boolean isDownAfterRelease()
    	{ return isdownafterrelease; }
    	
    	public void Update(KeyEvent e)
    	{
    		if (e == KeyEvent.Press)
    		{
    			if (System.currentTimeMillis() - previoustime > timebetween)
    			{
    				if (released)
    					isdownafterrelease = true;
    				else
    					isdownafterrelease = false;
    					
    				isdown = true;
    				released = false;
    				previoustime = System.currentTimeMillis();
    			}
    		}
    		else
    		{
    			isdown = false;
    			isdownafterrelease = false;
    			released = true;
    		}
    	}
    }
    KeySystem.java
    Java Code:
    package Controls;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;
    
    public class KeySystem implements KeyListener
    {
    	private Key[] keys;
    	
    	public KeySystem(long timeBetween)
    	{
    		keys = new Key[5];
    		keys[0] = new Key(KeyEvent.VK_W);
    		keys[1] = new Key(KeyEvent.VK_A);
    		keys[2] = new Key(KeyEvent.VK_S);
    		keys[3] = new Key(KeyEvent.VK_D);
    		keys[4] = new Key(KeyEvent.VK_SPACE);
    	}
    	
    	public KeySystem()
    	{
    		this(0);
    	}
    	
    	private Key getKey(int code)
    	{
    		for (Key k : keys)
    			if (k.getKeyCode() == code)
    				return k;
    		
    		return null;
    	}
    
    	public void keyPressed(KeyEvent e)
    	{
    		Key temp = getKey(e.getKeyCode());
    		if (temp != null)
    			temp.Update(Key.KeyEvent.Press);
    	}
    	
    	public void keyTyped(KeyEvent e)
    	{
    		
    	}
    	
    	public void keyReleased(KeyEvent e)
    	{
    		Key temp = getKey(e.getKeyCode());
    		if (temp != null)
    			temp.Update(Key.KeyEvent.Released);
    	}
    
    	public ArrayList<Key> pressedKeys()
    	{
    		ArrayList<Key> temp = new ArrayList<Key>();
    		for (Key k : keys)
    			if (k.isDown())
    				temp.add(k);
    		
    		return temp;
    	}
    
    	public static boolean isKeyDown(int code)
    	{
    		return IControllableObject.system.getKey(code).isDown();
    	}
    	
    	public static boolean isKeyDownAfterRelease(int code)
    	{
    		return IControllableObject.system.getKey(code).isDownAfterRelease();
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,773
    Rep Power
    25

    Default

    Can you post some working code for testing.
    Or show the println() debug output that demos what's happening?
    I don't see any debug code in what you've posted.

Similar Threads

  1. KeyPress for my Button
    By bigj in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 05:27 PM
  2. help on motion detection
    By MarkWilson in forum Advanced Java
    Replies: 4
    Last Post: 12-07-2009, 07:45 AM
  3. request detection
    By mtyoung in forum Advanced Java
    Replies: 6
    Last Post: 02-05-2009, 04:46 AM
  4. USB Detection
    By alanixu in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 05:04 PM
  5. COM Port Detection in Java
    By talasilasumanth in forum Forum Guides
    Replies: 1
    Last Post: 09-10-2008, 04:18 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
  •