Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to write a Key Listener in Java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-02-2010
Member
 
Join Date: Aug 2006
Posts: 300
How to write a Key Listener in Java?
  

Hi friends.
Last time you guys did a very helpful work for me by providing the proper solutions. Hoping the same this time..!!! Actually, I am trying to get the focus at my form when the user is typing at the keyboard. i know that a Key Listener would be useful for me, but i don't know how to use that.!! So, please tell me how to write a Key Listener in Java.?? Hope that someone have got my point and will reply me soon.!!

Reply With Quote
  #2  
Old 13-02-2010
Member
 
Join Date: Aug 2006
Posts: 235
Re: How to write a Key Listener in Java?

Before going for the coding of the key listener, I would recommend you to know more about that listener. Otherwise you will find very confusing, while writing the coding. Key events tell you when the user is typing at the keyboard. When the user presses or releases keyboard keys, key events are fired by the component with the keyboard focus. For a key press to affect a component, the component must have the keyboard focus. A component generally gains the focus by the user clicking it, tabbing between components, or otherwise interacting with a component. Hope that you have got some important points from this post.!!
__________________
3.2 (northwood)
2gig ram
ATI AIW X800xt 256mb
Gigabyte GA-8knxp 875p Chipset
Optiwrite 8X DVD Burner
Win XP PRO Sp2 (Works Perfectly)
2 SATA Raptor 74gig Raid 0
2 7200 IDE 320gig HD
Reply With Quote
  #3  
Old 13-02-2010
Member
 
Join Date: Jul 2006
Posts: 286
Re: How to write a Key Listener in Java?

There are some steps that you should follow for making a component to get the keyboard focus. That steps are mentioned as follows :
  1. While using the keyboard focus, you will have to make sure that the component's isFocusable method returns true. By doing this the component are allowed to receive the focus.
  2. Also you will have to make sure that the component requests the focus when appropriate. You'll probably need to implement a mouse listener that calls the requestFocusInWindow method when the component is clicked, if you are using the custom components.
__________________
IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....
Reply With Quote
  #4  
Old 13-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,521
Re: How to write a Key Listener in Java?

I have provided you with a sample of coding that is useful for key event handling. Just have a look on this coding :
Code:
public class KeyEventTrial ...  implements KeyListener ... {
	typingArea = new JTextField(25);
	typingArea.addKeyListener(this);
    ...
    public void keyTyped(KeyEvent ke) {
	displayInfo(e, "KEY TYPED: ");
    }

    public void keyPressed(KeyEvent ke) {
	displayInfo(e, "KEY PRESSED: ");
    }

    public void keyReleased(KeyEvent ke) {
	displayInfo(e, "KEY RELEASED: ");
    }
    ...
    private void displayInfo(KeyEvent ke, String keyStatus){

        int id = ke.getID();
        String keyString;
        if (id == KeyEvent.KEY_TYPED) {
            char c = ke.getKeyChar();
            keyString = "key character = '" + c + "'";
        } else {
            int keyCode = ke.getKeyCode();
            keyString = "key code = " + keyCode
                    + " ("
                    + KeyEvent.getKeyText(keyCode)
                    + ")";
        }
        
        int modifiersEx = ke.getModifiersEx();
        String modString = "extended modifiers = " + modifiersEx;
        String tmpString = KeyEvent.getModifiersExText(modifiersEx);
        if (tmpString.length() > 0) {
            modString += " (" + tmpString + ")";
        } else {
            modString += " (no extended modifiers)";
        }
        
        String actionString = "action key? ";
        if (e.isActionKey()) {
            actionString += "YES";
        } else {
            actionString += "NO";
        }
        
        String locationString = "key location: ";
        int location = e.getKeyLocation();
        if (location == KeyEvent.KEY_LOCATION_STANDARD) {
            locationString += "standard";
        } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
            locationString += "left";
        } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
            locationString += "right";
        } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
            locationString += "numpad";
        } else {  locationString += "unknown";
        }
        
    }
}
Reply With Quote
  #5  
Old 13-02-2010
Member
 
Join Date: May 2008
Posts: 2,297
Re: How to write a Key Listener in Java?

You should also know about the methods that are used for Key Listener. The corresponding adapter class is KeyAdapter of KeyListener Interface. The following methods are used for the KeyListener Interface :
  • keyTyped(KeyEvent) - This method is called just after the user types a Unicode character into the listened-to component.
  • keyPressed(KeyEvent) - After the user presses a key while the listened-to component has the focus, this method is called.
  • keyReleased(KeyEvent) - This method is called just after the user releases a key while the listened-to component has the focus.
Reply With Quote
  #6  
Old 13-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,005
Re: How to write a Key Listener in Java?

I think that you should also know the methods of KeyEvent Class, which is very important in Key Listener. The KeyEvent class inherits many useful methods from the InputEvent class. Also the KeyEvent class inherits some methods from ComponentEvent and AWTEvent classes. The following are some useful methods that are used in KeyEvent Class :
  • int getKeyChar() - This method obtains the Unicode character associated with this event.
  • String getKeyText(int) - This method return text descriptions of the event's key code.
  • boolean isActionKey() - This method returns true if the key firing the event is an action key.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to write a Key Listener in Java?"
Thread Thread Starter Forum Replies Last Post
How to write a Document Listener in Java? pushpendra Software Development 8 18-03-2010 01:22 PM
How to write a Container Listener in Java? Pratim Software Development 4 12-02-2010 06:43 AM
How to write a Component Listener in Java? hariharan_00 Software Development 4 12-02-2010 06:28 AM
How to write an Action Listener in Java? Beter 2 Burn Out Software Development 5 12-02-2010 05:44 AM
How to write a Caret Listener in Java? Dilbert Software Development 5 12-02-2010 05:33 AM


All times are GMT +5.5. The time now is 11:33 PM.