Problem with KeyReleased()

Hi all,

I have a problem, and need some help if possible.
Here is the code which reproduce it (on my pc under windows XP)

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
 
public class Arrow implements KeyListener {
	private JFrame frame;
	boolean up, down, left, right;
	
	public Arrow() {
		frame = new JFrame();
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.setLocationRelativeTo(null);
		frame.setSize(200,200 );
		frame.addKeyListener( this );
		frame.setVisible( true );
		up = down = left = right = false; }
	
	public void keyPressed( KeyEvent e ) {
		switch( e.getKeyCode() ) {
			case KeyEvent.VK_UP:
				if( ! up ) System.out.println( "up" );
				up = true;
				break;
			case KeyEvent.VK_DOWN:
				if( ! down ) System.out.println( "down" );
				down = true;
				break;
			case KeyEvent.VK_LEFT:
				if( ! left ) System.out.println( "left" );
				left = true;
				break;
			case KeyEvent.VK_RIGHT:
				if( ! right ) System.out.println( "right" );
				right = true;
				break;
			default: }}
 
	public void keyReleased(KeyEvent e) {
		switch( e.getKeyCode() ) {
			case KeyEvent.VK_UP:
				System.out.println( "up released" );
				up = false;
				break;
			case KeyEvent.VK_DOWN:
				System.out.println( "down released" );
				down = false;
				break;
			case KeyEvent.VK_LEFT:
				System.out.println( "left released" );
				left = false;
				break;
			case KeyEvent.VK_RIGHT:
				System.out.println( "right released" );
				right = false;
				break;
			default: }}
 
	// KeyTyped()
	public void keyTyped( KeyEvent e ) {}
	
	// Méthode main()
	public static void main( String[] arg ) {
		new Touches(); }
 
}

To summarize, this class displays the arrow keys pressed and released
If i press UP and RIGHT, I have the following logs:
right
up

I release only UP, and here are the logs:
up released
right released

…but RIGHT is still pressed.

At first, uninstall / reinstall the JRE (1.6.0_14) has solved the problem.
The next day, the bug has returned.
Since, i uninstalled, reinstalled and tested all versions of JRE 1.6 (ie the update 11, 12, 13, 14), bug is still here.

I formatted windows and reinstalled it last Friday : it works
Yesterday, the bug has returned.
Between today and Friday i just installed FlashPlayer + Firefox & Reboot
Yesterday, I tryed several versions of JRE, without success.

I think my problem come from my computer, but i don’t know where neither what to do…
Any idea ? Can you reproduce my bug ?

Thanks for help, and sorry for my poor English, it is not my native language

This problem has nothing to do with your system or installed software. It is a (well known) Java problem. I already reported it to Sun. Let’s see, what they say. But I guess, many people have reported it before me.

You can try to use the input abstraction in JAGaToo. It pretty much workarounds the problem. Don’t be afraid of the LWJGL- or JInput implementations. They are just compile-time dependencies.

Marvin

Thank you for your answer, I’ll try to study JAGaToo or JInput

However, it seems this code works without problem on other PC…
Indeed, I have seen bug reports on this kind of problem, but for the most part, they have been reported on Solaris or linux, and come from the auto-repeat.

It is worth than i suspected indeed…
The bug is still here with JInput…

I downloaded the API, drag the dlls/jar in the bin folder of my JDK, and tested connected devices.
When I identified the keyboard, here is the result of my tests

num pad 1 pressed, the field changes to ON
num pad 2 pressed, the field changes to ON
num pad 1 OR num pad 2 released, the 2 fields changes to OFF…

And actually, I have tested 3 buttons pressed, it’s even more bizarre
num pad 1 pressed, the field changes to ON
num pad 2 pressed, the field changes to ON
num pad 3 pressed, the field changes to ON
num pad 3 or 2 or 1 released, the field changes to OFF
any of the remaining 2 keys released, the 2 remaining fields changes to OFF

I don’t know what to do anymore.
I am under windows XP SP3, laptop, dual core centrino.

Probleme resolved, after 2 weeks !

In fact, i’m working on a laptop, connected to a “switch”.
This “switch” is also connected to a PC, then with the same keyboard, mouse, and monitor, i can switch between 2 computers.

If i don’t use the keyboard connected to the switch, it works very well, using JInput or not.

Thanks for your help, JInput will be very usefull later, so this discussion is not lost anyway :slight_smile: