Results 1 to 6 of 6

Thread: How to write a Key Listener in Java?

  1. #1
    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.!!

  2. #2
    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

  3. #3
    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....

  4. #4
    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";
            }
            
        }
    }

  5. #5
    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.

  6. #6
    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.

Similar Threads

  1. How to write a Document Listener in Java?
    By pushpendra in forum Software Development
    Replies: 8
    Last Post: 18-03-2010, 01:22 PM
  2. How to write a Container Listener in Java?
    By Pratim in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:43 AM
  3. How to write a Component Listener in Java?
    By hariharan_00 in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:28 AM
  4. How to write an Action Listener in Java?
    By Beter 2 Burn Out in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:44 AM
  5. How to write a Caret Listener in Java?
    By Dilbert in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,884,723.86506 seconds with 16 queries