Hello,

I build my java desktop application using NetBeans 6.7, visually. So I click on the Frame -> Events -> KeyTyped and I add a new handler.
The problem is the key events are not detected! My components are Swing components. Why is this happening?? :(

private void formKeyTyped(java.awt.event.KeyEvent evt) {

        System.out.println("key typed "+evt.getKeyCode());
    }

Please somebody help!

Re: KeyTyped event not detected! 80 80

Please somebody help... this is unusual... why does the form not detect the keyboard?

Re: KeyTyped event not detected! 80 80

It is most likely a focus issue. Read the tutorial on writing key listeners: http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

If you need to respond to specific keys being typed, it's usually easier to use a key binding instead of a key listener.

Re: KeyTyped event not detected! 80 80

Indeed it's about focus and about the component... I finally got it, thanks for answering. I add a windows listenter + requested focus to the RIGHT component. For example this is not working:

this.requestFocus();
        this.addKeyListener(
             new KeyAdapter() {
            @Override
             public void keyTyped(KeyEvent e){
                 System.out.println("key "+e.getKeyChar()); }
           } 
      );

but this is working:

this.getContentPane().requestFocus();
        this.getContentPane().addKeyListener(
             new KeyAdapter() {
            @Override
             public void keyTyped(KeyEvent e){
                 System.out.println("key "+e.getKeyChar()); }
           } 
      );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.