Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 313117

Summary: KeyListener.keyReleased() of Text widgets is called when key is pressed
Product: [RT] RAP Reporter: Michael Klein <michael.klein>
Component: RWTAssignee: Project Inbox <rap-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 1.3   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Michael Klein CLA 2010-05-17 07:59:12 EDT
Build: RAP 1.3 M7

When a KeyListener is registered on a Text widget, the keyReleased method is called immediately when a key is pressed.

Here is a snipped to reproduce the behaviour:



public class KeyReleasedBug implements IEntryPoint {

    public int createUI() {
        Display display = new Display();
        Shell shell = new Shell(display, SWT.TITLE);
        createContent(shell);
        shell.layout();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return 0;
    }

    private void createContent(Shell shell) {
        shell.setLayout(new GridLayout(1, false));

        Text text = new Text(shell, SWT.BORDER);
        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        text.addKeyListener(new KeyListener() {
        	
			public void keyReleased(KeyEvent e) {
            	System.err.println("keyReleased");
            }
			
			public void keyPressed(KeyEvent e) {
            	//System.err.println("keyPressed");
            }
        });
    }

}
Comment 1 RĂ¼diger Herrmann CLA 2010-05-27 15:58:38 EDT
This behavior is intended. KeyListeners were introduced to support cell editors and are limited to this use case. Network latency makes it hard in most cases to transmit every single key up/down event to the server and wait for the response.
To get notified about changes in a text widget, you may resort to ModifyListener and VerifyListener.