JDK-4839127 : keyReleased events are fired improperly
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2003-03-28
  • Updated: 2003-03-28
  • Resolved: 2003-03-28
Related Reports
Duplicate :  
Description

Name: jl125535			Date: 03/27/2003


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)


FULL OS VERSION :
Linux galahad 2.4.20 #2 Mon Dec 16 22:53:44 CET 2002 i686 unknown
(RedHat 7.1)

A DESCRIPTION OF THE PROBLEM :
When adding a KeyListener to any kind of component, keyReleased events are fired when the key on the keyboard is held down.

This is different in windows. When holding a keyboard button down, keyPressed and keyTyped are fired, but keyReleased is only fired when releasing the button (behaviour as wished).

1.4.2 on Solaris 8 also exhibits the desired behavior.

Thus, on Linux it is difficult to write suitable keyListeners for instance for games, since you can never react approriate when you do not know if a key is held or not. A solution using a mechanism that will use timestamps in order to check every n milliseconds whether an event has been fired or not is way to poor to implement, sorry.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile the code below and press a key

EXPECTED VERSUS ACTUAL BEHAVIOR :
keyReleased should only be fired when the key is actually released.
keyReleased is fired when the key is held down

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


public class Test
{	
	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		JFrame fr = new JFrame();
		JLabel l = new JLabel("a label");
		fr.getContentPane().add(l);
		fr.pack();
		fr.show();
		l.requestFocus();
		
		fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
		l.addKeyListener(new KL() );
	}
	
	private static class KL implements KeyListener
	{
		public void keyPressed(KeyEvent e)
		{
			System.out.println("pressed");
		}
		
		public void keyReleased(KeyEvent e)
		{
			System.out.println("released");
		}
		
		public void keyTyped(KeyEvent e)
		{
			System.out.println("typed");
		}
	}
}

---------- END SOURCE ----------
(Review ID: 182446) 
======================================================================