Keyboard.poll()

Started by kochakaden, December 21, 2008, 18:06:59

Previous topic - Next topic

kochakaden

I'm trying to use Keyboard.poll(), but it's almost never registering my keyboard presses.


Here's a snippet of the keyboard handling routine, which gets called 60 times per second:

public void update()
	{
		//System.out.println("KB poll...");System.out.flush();
		Keyboard.poll();
		System.out.println("" + new Date() + Keyboard.isKeyDown(Keyboard.KEY_5));
		checkForQuit();
		for ( Map.Entry<Integer, Control> entry : mappings.entrySet() )
		{
			if ( Keyboard.isKeyDown(entry.getKey()) )
			{
				System.out.println("ON: " + entry.getValue().getFullyQualifiedName());System.out.flush();
				entry.getValue().setState("On");
			}
			else
			{
				// System.out.println("OFF: " + entry.getValue().getFullyQualifiedName());
				entry.getValue().setState("Off");
			}
		}

	}



In the following capture, it is polling the keyboard 60 times per second.  Truncated for readability:

Sun Dec 21 09:52:57 PST 2008: false
Sun Dec 21 09:52:57 PST 2008: false
Sun Dec 21 09:52:57 PST 2008: false
...
Sun Dec 21 09:52:58 PST 2008: false
...
Sun Dec 21 09:52:59 PST 2008: false
...
Sun Dec 21 09:53:00 PST 2008: false
...
Sun Dec 21 09:53:01 PST 2008: false
...
Sun Dec 21 09:53:02 PST 2008: false
...
Sun Dec 21 09:53:03 PST 2008: false
...
Sun Dec 21 09:53:04 PST 2008: false
...
Sun Dec 21 09:53:05 PST 2008: true
...
Sun Dec 21 09:53:06 PST 2008: true
Sun Dec 21 09:53:06 PST 2008: false


During this entire capture, I've been mashing the 5 key over and over again. When the key finally registers, it stays registered as pressed for 43 polls (about 2/3 of a second) even though I am pressing the key anywhere from 5-10 times per second, and then never registers again, even if I mash the key over and over for 5 minutes straight.

Once, I got lucky, and the 5 key registered twice in less than a second.  From that point, they keyboard worked perfectly until I quit the program.  The next time I ran it, I got a broken keyboard interface again.

Is there some kind of special setup that I'm missing?  Or maybe the keyboard doesn't work properly when audio is in use?  Or maybe it has something to do with the timers?  I'm really confused here :(

Matzon

you probably should be using the event mechanism.
isKeyDown tests whether the key is down when you check.
check:
Keyboard.next()
Keyboard.getEventKey()
Keyboard.getEventKeyState()
Keyboard.getEventCharacter()

kochakaden

Quote from: Matzon on December 21, 2008, 18:29:20
you probably should be using the event mechanism.
isKeyDown tests whether the key is down when you check.
check:
Keyboard.next()
Keyboard.getEventKey()
Keyboard.getEventKeyState()
Keyboard.getEventCharacter()

Ok, I'll try that.  Is this the proper way to do things now?  I just tried running the same code using my old lwjgl 0.99 libraries and it worked perfectly.  When I tried it again with the 2.0 libraries, it screwed up like before...

The thing is, I'm building an arcade emulator, and all of the emulated games use polling to read button input.
So I'd rather use poll() if I can while the game is running, and then just check key state when the emulated game actually reads the input ports...

princec

poll() should work fine really - however if you are using the LWJGL Display, it calls it for you automatically and you won't need to call it yourself.

Cas :)