java.awt.event.KeyListener类的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(234)

本文整理了Java中java.awt.event.KeyListener类的一些代码示例,展示了KeyListener类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。KeyListener类的具体详情如下:
包路径:java.awt.event.KeyListener
类名称:KeyListener

KeyListener介绍

[英]The listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).

The listener object created from that class is then registered with a component using the component's addKeyListener method. A keyboard event is generated when a key is pressed, released, or typed (pressedn and released). The relevant method in the listener object is then invoked, and the KeyEvent is passed to it.
[中]用于接收键盘事件(击键)的侦听器接口。对处理键盘事件感兴趣的类要么实现此接口(及其包含的所有方法),要么扩展抽象KeyAdapter类(仅覆盖感兴趣的方法)。
然后使用组件的addKeyListener方法向组件注册从该类创建的侦听器对象。当按下、释放或键入某个键(按下并释放)时,会生成键盘事件。然后调用侦听器对象中的相关方法,KeyEvent被传递给它。

代码示例

代码示例来源:origin: org.swinglabs.swingx/swingx-core

@Override
  public void eventDispatched(AWTEvent aWTEvent) {
    if (aWTEvent instanceof KeyEvent) {
      KeyEvent evt = (KeyEvent)aWTEvent;
      for (KeyListener kl : getKeyListeners()) {
        int id = aWTEvent.getID();
        switch (id) {
          case KeyEvent.KEY_PRESSED:
            kl.keyPressed(evt);
            break;
          case KeyEvent.KEY_RELEASED:
            kl.keyReleased(evt);
            break;
          case KeyEvent.KEY_TYPED:
            kl.keyTyped(evt);
            break;
          default:
            System.err.println("Unhandled Key ID: " + id);    
        }
      }
    }
  }
};

代码示例来源:origin: JetBrains/jediterm

public void handleKeyEvent(@NotNull KeyEvent e) {
 final int id = e.getID();
 if (id == KeyEvent.KEY_PRESSED) {
  for (KeyListener keyListener : myCustomKeyListeners) {
   keyListener.keyPressed(e);
  }
 } else if (id == KeyEvent.KEY_TYPED) {
  for (KeyListener keyListener : myCustomKeyListeners) {
   keyListener.keyTyped(e);
  }
 }
}

代码示例来源:origin: net.sf.nachocalendar/nachocalendar

/**
 * Notifies all registered listeners about the event.
 *
 * @param event The event to be fired
 */
private void fireKeyListenerKeyPressed(final java.awt.event.KeyEvent event) {
  if (listenerList == null) {
    return;
  }
  final Object[] listeners = listenerList.getListenerList();
  for (int i = listeners.length - 2; i >= 0; i -= 2) {
    if (listeners[i] == java.awt.event.KeyListener.class) {
      ((java.awt.event.KeyListener) listeners[i + 1]).keyPressed(event);
    }
  }
}

代码示例来源:origin: net.sf.nachocalendar/nachocalendar

/**
 * Notifies all registered listeners about the event.
 *
 * @param event The event to be fired
 */
private void fireKeyListenerKeyReleased(final java.awt.event.KeyEvent event) {
  if (listenerList == null) {
    return;
  }
  final Object[] listeners = listenerList.getListenerList();
  for (int i = listeners.length - 2; i >= 0; i -= 2) {
    if (listeners[i] == java.awt.event.KeyListener.class) {
      ((java.awt.event.KeyListener) listeners[i + 1]).keyReleased(event);
    }
  }
}

代码示例来源:origin: net.imagej/ij

public void keyTyped (KeyEvent e) {
  if (keyListener!=null)
    keyListener.keyTyped(e);
}

代码示例来源:origin: sdedit/sdedit

/**
 * Undoes the last action, if any has been performed.
 */
public void undo() {
  try {
    if (undoManager.canUndo()) {
      undoManager.undo();
      for (KeyListener kl : getKeyListeners()) {
        kl.keyTyped(null);
        kl.keyReleased(null);
      }
    }
  } catch (CannotUndoException e) {
  }
}

代码示例来源:origin: net.sf.nachocalendar/nachocalendar

/**
 * Notifies all registered listeners about the event.
 * 
 * @param event
 *            The event to be fired
 */
private void fireKeyListenerKeyPressed(final java.awt.event.KeyEvent event) {
  if (listenerList == null) {
    return;
  }
  final Object[] listeners = listenerList.getListenerList();
  for (int i = listeners.length - 2; i >= 0; i -= 2) {
    if (listeners[i] == java.awt.event.KeyListener.class) {
      ((java.awt.event.KeyListener) listeners[i + 1]).keyPressed(event);
    }
  }
}

代码示例来源:origin: net.sf.nachocalendar/nachocalendar

/**
 * Notifies all registered listeners about the event.
 * 
 * @param event
 *            The event to be fired
 */
private void fireKeyListenerKeyReleased(final java.awt.event.KeyEvent event) {
  if (listenerList == null) {
    return;
  }
  final Object[] listeners = listenerList.getListenerList();
  for (int i = listeners.length - 2; i >= 0; i -= 2) {
    if (listeners[i] == java.awt.event.KeyListener.class) {
      ((java.awt.event.KeyListener) listeners[i + 1]).keyReleased(event);
    }
  }
}

代码示例来源:origin: imagej/ImageJA

public void keyTyped (KeyEvent e) {
  if (keyListener!=null)
    keyListener.keyTyped(e);
}

代码示例来源:origin: sdedit/sdedit

/**
 * Redoes the last action, if any has been performed.
 */
public void redo() {
  try {
    if (undoManager.canRedo()) {
      undoManager.redo();
      for (KeyListener kl : getKeyListeners()) {
        kl.keyTyped(null);
        kl.keyReleased(null);
      }
    }
  } catch (CannotRedoException e) {
  }
}

代码示例来源:origin: org.codehaus.jtstand/jtstand-desktop

public void eventDispatched(AWTEvent aWTEvent) {
    if (aWTEvent instanceof KeyEvent) {
      KeyEvent evt = (KeyEvent)aWTEvent;
      for (KeyListener kl : getKeyListeners()) {
        int id = aWTEvent.getID();
        switch (id) {
          case KeyEvent.KEY_PRESSED:
            kl.keyPressed(evt);
            break;
          case KeyEvent.KEY_RELEASED:
            kl.keyReleased(evt);
            break;
          case KeyEvent.KEY_TYPED:
            kl.keyTyped(evt);
            break;
          default:
            System.err.println("Unhandled Key ID: " + id);    
        }
      }
    }
  }
};

代码示例来源:origin: xyz.cofe/docking-frames-core

public boolean keyPressed( DockElement element, KeyEvent event ){
    if( element == AbstractDockable.this ){
      for( KeyListener listener : getKeyListeners() ){
        listener.keyPressed( event );
      }
      return event.isConsumed();
    }
    else{
      return false;
    }
  }
};

代码示例来源:origin: com.googlecode.gstreamer-java/gstreamer-java

@Override
  public void keyReleased(KeyEvent evt) {
    for (KeyListener l : videoComponent.getKeyListeners()) {
      l.keyReleased(evt);
    }
  }
});

代码示例来源:origin: net.sf.nachocalendar/nachocalendar

/**
 * Notifies all registered listeners about the event.
 *
 * @param event The event to be fired
 */
private void fireKeyListenerKeyTyped(final java.awt.event.KeyEvent event) {
  if (listenerList == null) {
    return;
  }
  final Object[] listeners = listenerList.getListenerList();
  for (int i = listeners.length - 2; i >= 0; i -= 2) {
    if (listeners[i] == java.awt.event.KeyListener.class) {
      ((java.awt.event.KeyListener) listeners[i + 1]).keyTyped(event);
    }
  }
}

代码示例来源:origin: org.swinglabs.swingx/swingx-all

@Override
  public void eventDispatched(AWTEvent aWTEvent) {
    if (aWTEvent instanceof KeyEvent) {
      KeyEvent evt = (KeyEvent)aWTEvent;
      for (KeyListener kl : getKeyListeners()) {
        int id = aWTEvent.getID();
        switch (id) {
          case KeyEvent.KEY_PRESSED:
            kl.keyPressed(evt);
            break;
          case KeyEvent.KEY_RELEASED:
            kl.keyReleased(evt);
            break;
          case KeyEvent.KEY_TYPED:
            kl.keyTyped(evt);
            break;
          default:
            System.err.println("Unhandled Key ID: " + id);    
        }
      }
    }
  }
};

代码示例来源:origin: sc.fiji/fiji-lib

@Override
public final void keyPressed(KeyEvent e) {
  if (isThisTool())
    listener.keyPressed(e);
}

代码示例来源:origin: xyz.cofe/docking-frames-core

public boolean keyReleased( DockElement element, KeyEvent event ){
  if( element == AbstractDockable.this ){
    for( KeyListener listener : getKeyListeners() ){
      listener.keyReleased( event );
    }
    return event.isConsumed();
  }
  else{
    return false;
  }
}

代码示例来源:origin: net.sf.nachocalendar/nachocalendar

/**
 * Notifies all registered listeners about the event.
 * 
 * @param event
 *            The event to be fired
 */
private void fireKeyListenerKeyTyped(final java.awt.event.KeyEvent event) {
  if (listenerList == null) {
    return;
  }
  final Object[] listeners = listenerList.getListenerList();
  for (int i = listeners.length - 2; i >= 0; i -= 2) {
    if (listeners[i] == java.awt.event.KeyListener.class) {
      ((java.awt.event.KeyListener) listeners[i + 1]).keyTyped(event);
    }
  }
}

代码示例来源:origin: org.bidib.jbidib.swinglabs.swingx/swingx-core

@Override
  public void eventDispatched(AWTEvent aWTEvent) {
    if (aWTEvent instanceof KeyEvent) {
      KeyEvent evt = (KeyEvent)aWTEvent;
      for (KeyListener kl : getKeyListeners()) {
        int id = aWTEvent.getID();
        switch (id) {
          case KeyEvent.KEY_PRESSED:
            kl.keyPressed(evt);
            break;
          case KeyEvent.KEY_RELEASED:
            kl.keyReleased(evt);
            break;
          case KeyEvent.KEY_TYPED:
            kl.keyTyped(evt);
            break;
          default:
            System.err.println("Unhandled Key ID: " + id);    
        }
      }
    }
  }
};

代码示例来源:origin: com.googlecode.gstreamer-java/gstreamer-java

@Override
public void keyPressed(KeyEvent evt) {
  for (KeyListener l : videoComponent.getKeyListeners()) {
    l.keyPressed(evt);
  }
}

相关文章