|
| ||||||||||
| Tags: inputevent class, java, key events, key listener, keyboard focus |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| How to write a Key Listener in Java?
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
| ||||
| ||||
| 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 ramATI 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
| ||||
| ||||
| 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 :
__________________ 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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 :
|
|
#6
| ||||
| ||||
| 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 :
|
![]() |
|
| Thread Tools | Search this Thread |
| |
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 an Item Listener in Java? | shivendra | Software Development | 4 | 13-02-2010 12:13 AM |
| How to write a Container Listener in Java? | Pratim | Software Development | 4 | 12-02-2010 05:43 AM |
| How to write a Component Listener in Java? | hariharan_00 | Software Development | 4 | 12-02-2010 05:28 AM |
| How to write an Action Listener in Java? | Beter 2 Burn Out | Software Development | 5 | 12-02-2010 04:44 AM |