本文整理汇总了Java中java.awt.event.KeyListener类的典型用法代码示例。如果您正苦于以下问题:Java KeyListener类的具体用法?Java KeyListener怎么用?Java KeyListener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
KeyListener类属于java.awt.event包,在下文中一共展示了KeyListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processKeyEvent
import java.awt.event.KeyListener; //导入依赖的package包/类
@Override
protected void processKeyEvent(KeyEvent e) {
KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e);
if (enter.equals(ks)) {
// Prevent JComponent.processKeyBindings() to be called (it is called from
// JComponent.processKeyEvent() ), notify only registered key listeners
int id = e.getID();
for (KeyListener keyListener : getKeyListeners()) {
switch(id) {
case KeyEvent.KEY_TYPED:
keyListener.keyTyped(e);
break;
case KeyEvent.KEY_PRESSED:
keyListener.keyPressed(e);
break;
case KeyEvent.KEY_RELEASED:
keyListener.keyReleased(e);
break;
}
}
if (!e.isConsumed() && id == KeyEvent.KEY_PRESSED) {
synchronized(listeners) {
List<CellEditorListener> list = new ArrayList<CellEditorListener>(listeners);
for (CellEditorListener listener : list) {
listener.editingStopped(new ChangeEvent(this));
}
}
}
e.consume();
} else {
super.processKeyEvent(e);
}
}
示例2: updateProviderOnSave
import java.awt.event.KeyListener; //导入依赖的package包/类
public KeyListener updateProviderOnSave() {
return new KeyAdapter() {
@Override
public void keyPressed(KeyEvent ke) {
if (isSave(ke)) {
onSave();
}
}
};
}
示例3: listenToKeysListener
import java.awt.event.KeyListener; //导入依赖的package包/类
private KeyListener listenToKeysListener() {
final KeyAdapter adapter = new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
String word = nameField.getText();
if (word.length() < 18) {
hotelNameTitle.setText(word);
hotelNameTitle.repaint();
} else {
JOptionPane.showMessageDialog(modalFrame, "Hotel name maximum 18 charachter allowed!",
JOptionPane.MESSAGE_PROPERTY, JOptionPane.WARNING_MESSAGE);
}
super.keyTyped(e);
}
};
return adapter;
}
示例4: validatorListener
import java.awt.event.KeyListener; //导入依赖的package包/类
private KeyListener validatorListener() {
final KeyAdapter adapter = new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
final EmailValidator validator = new EmailValidator();
if(validator.validate(emailField.getText())) {
markerLbl.setVisible(false);
infoLabel.setText("");
isValid = true;
}
else {
markerLbl.setVisible(true);
infoLabel.setText("Invalid email format!");
}
}
};
return adapter;
}
示例5: containsInGameComponents
import java.awt.event.KeyListener; //导入依赖的package包/类
/**
* Checks if this {@code Canvas} contains any in game components.
*
* @return {@code true} if there is any in game components.
*/
public boolean containsInGameComponents() {
KeyListener[] keyListeners = getKeyListeners();
if (keyListeners.length > 0) {
return true;
}
MouseListener[] mouseListeners = getMouseListeners();
if (mouseListeners.length > 0) {
return true;
}
MouseMotionListener[] mouseMotionListeners = getMouseMotionListeners();
if (mouseMotionListeners.length > 0) {
return true;
}
return false;
}
示例6: removeInGameComponents
import java.awt.event.KeyListener; //导入依赖的package包/类
/**
* Removes components that is only used when in game.
*/
public void removeInGameComponents() {
// remove listeners, they will be added when launching the new game...
KeyListener[] keyListeners = getKeyListeners();
for (KeyListener keyListener : keyListeners) {
removeKeyListener(keyListener);
}
MouseListener[] mouseListeners = getMouseListeners();
for (MouseListener mouseListener : mouseListeners) {
removeMouseListener(mouseListener);
}
MouseMotionListener[] mouseMotionListeners = getMouseMotionListeners();
for (MouseMotionListener mouseMotionListener : mouseMotionListeners) {
removeMouseMotionListener(mouseMotionListener);
}
for (Component c : getComponents()) {
removeFromCanvas(c);
}
}
示例7: fireKeyPressed
import java.awt.event.KeyListener; //导入依赖的package包/类
/**
* Description of the Method
*
* @param e Description of the Parameter
*/
public void fireKeyPressed(KeyEvent e) {
for (Enumeration<KeyListener> en = listeners.elements(); en.hasMoreElements(); ) {
KeyListener l = en.nextElement();
l.keyPressed(new KeyEvent((java.awt.Component) e.getSource(), KeyEvent.KEY_PRESSED, System.currentTimeMillis(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), e.getKeyLocation()));
}
}
示例8: fireKeyReleased
import java.awt.event.KeyListener; //导入依赖的package包/类
/**
* Description of the Method
*
* @param e Description of the Parameter
*/
public void fireKeyReleased(KeyEvent e) {
for (Enumeration<KeyListener> en = listeners.elements(); en.hasMoreElements(); ) {
KeyListener l = en.nextElement();
l.keyReleased(new KeyEvent((java.awt.Component) e.getSource(), KeyEvent.KEY_RELEASED, System.currentTimeMillis(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), e.getKeyLocation()));
}
}
示例9: addListener
import java.awt.event.KeyListener; //导入依赖的package包/类
/**
* Adds an event listener to the Panel.
*
* @param eventListener
* The event listener.
*
* @throws IllegalArgumentException
* If the event listener isn't supported by this function.
*/
public void addListener(final EventListener eventListener) {
if (eventListener instanceof KeyListener) {
this.addKeyListener((KeyListener) eventListener);
return;
}
if (eventListener instanceof MouseListener) {
this.addMouseListener((MouseListener) eventListener);
return;
}
if (eventListener instanceof MouseMotionListener) {
this.addMouseMotionListener((MouseMotionListener) eventListener);
return;
}
throw new IllegalArgumentException("The " + eventListener.getClass().getSimpleName() + " is not supported.");
}
示例10: removeListener
import java.awt.event.KeyListener; //导入依赖的package包/类
/**
* Removes an event listener from the Panel.
*
* @param eventListener
* The event listener.
*
* @throws IllegalArgumentException
* If the event listener isn't supported by this function.
*/
public void removeListener(final EventListener eventListener) {
if (eventListener instanceof KeyListener) {
this.removeKeyListener((KeyListener) eventListener);
return;
}
if (eventListener instanceof MouseListener) {
this.removeMouseListener((MouseListener) eventListener);
return;
}
if (eventListener instanceof MouseMotionListener) {
this.removeMouseMotionListener((MouseMotionListener) eventListener);
return;
}
throw new IllegalArgumentException("The " + eventListener.getClass().getSimpleName() + " is not supported.");
}
示例11: removeListener
import java.awt.event.KeyListener; //导入依赖的package包/类
public void removeListener() {
for (MouseListener ml : com.getMouseListeners()) {
com.removeMouseListener(ml);
}
for (MouseMotionListener mml : com.getMouseMotionListeners()) {
com.removeMouseMotionListener(mml);
}
for (KeyListener kl : com.getKeyListeners()) {
com.removeKeyListener(kl);
}
reset();
com.repaint();
}
示例12: addKeyListener
import java.awt.event.KeyListener; //导入依赖的package包/类
/**
* Maps {@code Component.addKeyListener(KeyListener)} through queue
*/
public void addKeyListener(final KeyListener keyListener) {
runMapping(new MapVoidAction("addKeyListener") {
@Override
public void map() {
getSource().addKeyListener(keyListener);
}
});
}
示例13: addKeyListener
import java.awt.event.KeyListener; //导入依赖的package包/类
@Override
public synchronized void addKeyListener(KeyListener keyListener) {
textArea.addKeyListener(keyListener);
}
示例14: addSearchKeyListener
import java.awt.event.KeyListener; //导入依赖的package包/类
public void addSearchKeyListener(KeyListener listener) {
txtSearch.addKeyListener(listener);
}
示例15: setupSearch
import java.awt.event.KeyListener; //导入依赖的package包/类
private void setupSearch() {
// Remove the default key listeners
KeyListener[] keyListeners = getListeners(KeyListener.class);
for (int i = 0; i < keyListeners.length; i++) {
removeKeyListener(keyListeners[i]);
}
// Add new key listeners
addKeyListener(
new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int modifiers = e.getModifiers();
int keyCode = e.getKeyCode();
if (((modifiers > 0) && (modifiers != KeyEvent.SHIFT_MASK)) || e.isActionKey()) {
return;
}
char c = e.getKeyChar();
if (!Character.isISOControl(c) && (keyCode != KeyEvent.VK_SHIFT)) {
searchTextField.setText(String.valueOf(c));
displaySearchField();
}
}
}
);
// Create a the "multi-event" listener for the text field. Instead of
// adding separate instances of each needed listener, we're using a
// class which implements them all. This approach is used in order
// to avoid the creation of 4 instances which takes some time
SearchFieldListener searchFieldListener = new SearchFieldListener();
searchTextField.addKeyListener(searchFieldListener);
searchTextField.addFocusListener(searchFieldListener);
searchTextField.getDocument().addDocumentListener(searchFieldListener);
}