Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  EventQueue on key released?(ANSWERD), simple Keyboard class example posted  (Read 2030 times)
0 Members and 1 Guest are viewing this topic.
Offline HermanssoN

Senior Newbie





« Posted 2009-10-04 09:55:16 »

Hello.

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

basicly I wan't to know if this check is valid:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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??
        }
      }

Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #1 - Posted 2009-10-04 11:53:35 »

Hi

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

HTH

Endolf

Offline HermanssoN

Senior Newbie





« Reply #2 - Posted 2009-10-04 14:03:10 »

Thnx.

I will make up for my laziness and post my resultning keyboad class  Roll Eyes
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
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 Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HermanssoN

Senior Newbie





« Reply #3 - Posted 2009-10-04 19:25:42 »

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:
1  
2  
3  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
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  Huh
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!