下面列出了java.awt.event.KeyEvent#getSource() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == ' ') { // NOI18N
JTree tree = (JTree) e.getSource();
TreePath path = tree.getSelectionPath();
if (path == null) {
return;
}
Node node = Visualizer.findNode(path.getLastPathComponent());
if (node == null) {
return;
}
boolean isSelected = model.isNodeSelected(node);
model.setNodeSelected(node, !isSelected);
tree.repaint();
e.consume();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (e.getSource() instanceof JTree) {
JTree t = (JTree) e.getSource();
TreePath path = t.getSelectionPath();
if (path != null) {
CheckNode node = (CheckNode) path.getLastPathComponent();
boolean isSelected = !(node.isSelected());
node.setSelected(isSelected);
((DefaultTreeModel) tree.getModel()).nodeChanged(node);
tree.revalidate();
tree.repaint();
}
e.consume();
}
}
}
private void newAutocomplete(KeyEvent e)
{
final JTextComponent input = (JTextComponent) e.getSource();
final String inputText = input.getText();
final String nameStart = inputText.substring(0, input.getSelectionStart()) + e.getKeyChar();
if (findAutocompleteName(nameStart))
{
// Assert this.autocompleteName != null
final String name = this.autocompleteName;
SwingUtilities.invokeLater(() ->
{
try
{
input.getDocument().insertString(
nameStart.length(),
name.substring(nameStart.length()),
null);
input.select(nameStart.length(), name.length());
}
catch (BadLocationException ex)
{
log.warn("Could not autocomplete name.", ex);
}
});
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER ) {
if ( e.getSource() instanceof JTree ) {
JTree tree = (JTree) e.getSource();
TreePath path = tree.getSelectionPath();
if ( toggle( path )) {
e.consume();
}
}
}
}
/**
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
*/
public void keyPressed(KeyEvent e) {
if (e.getSource() == this.jButton_Cancel && e.getKeyCode() == KeyEvent.VK_ENTER) {
this.cancel = true;
terminate();
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
terminate();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER) {
if (e.getSource() instanceof JTree) {
JTree tree = (JTree) e.getSource();
TreePath path = tree.getSelectionPath();
if (toggle(path)) {
e.consume();
}
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
if (table.isEditing()) {
e.consume();
table.removeEditor();
if (e.getSource() != table) {
((JComponent)e.getSource()).removeKeyListener(this);
}
}
}
}
public void eventDispatched(AWTEvent event) {
if (!(event instanceof KeyEvent)) {
return;
}
KeyEvent keyEvent = (KeyEvent)event;
if (keyEvent.getKeyCode() == KeyEvent.VK_ALT) {
Toolkit.getDefaultToolkit().removeAWTEventListener(this);
KeyEvent kev = new KeyEvent(
(Component)keyEvent.getSource(), KeyEvent.KEY_RELEASED, keyEvent.getWhen(),
keyEvent.getModifiers(), keyEvent.getKeyCode(), keyEvent.getKeyChar()
);
KeyboardPopupSwitcher.processInterruption(kev);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE) {
JTree tree = (JTree) e.getSource();
TreePath path = tree.getSelectionPath();
if (path != null) {
FileNode node = (FileNode) path.getLastPathComponent();
node.setSelected(!node.isSelected());
tree.repaint();
e.consume();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if ( e.getSource() instanceof JTree ) {
JTree tree = (JTree) e.getSource();
TreePath path = tree.getSelectionPath();
if ( toggle( path )) {
e.consume();
}
}
}
}
public void processKeyEvent(KeyEvent evt) {
if (evt.getID() == KeyEvent.KEY_TYPED) {
if (Utilities.getJavadocCompletionSelectors().indexOf(evt.getKeyChar()) >= 0) {
JTextComponent comp = (JTextComponent) evt.getSource();
delegate.defaultAction(comp);
if (Utilities.getJavadocCompletionAutoPopupTriggers().indexOf(evt.getKeyChar()) >= 0) {
Completion.get().showCompletion();
}
evt.consume();
}
}
}
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
if(arg0.getSource() == jtf_inputMess){
jbt_trans.doClick();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
TranslationKeyField field = (TranslationKeyField) e.getSource();
String key = field.getValue();
addTranslation(key);
}
}
@Override
public void keyTyped(KeyEvent e)
{
if (!hiscoreConfig.autocomplete())
{
return;
}
final JTextComponent input = (JTextComponent) e.getSource();
final String inputText = input.getText();
// Only autocomplete if the selection end is at the end of the text.
if (input.getSelectionEnd() != inputText.length())
{
return;
}
// Character to be inserted at the selection start.
final String charToInsert = Character.toString(e.getKeyChar());
// Don't attempt to autocomplete if the name is invalid.
// This condition is also true when the user presses a key like backspace.
if (INVALID_CHARS.matcher(charToInsert).find()
|| INVALID_CHARS.matcher(inputText).find())
{
return;
}
// Check if we are already autocompleting.
if (autocompleteName != null && autocompleteNamePattern.matcher(inputText).matches())
{
if (isExpectedNext(input, charToInsert))
{
try
{
// Insert the character and move the selection.
final int insertIndex = input.getSelectionStart();
Document doc = input.getDocument();
doc.remove(insertIndex, 1);
doc.insertString(insertIndex, charToInsert, null);
input.select(insertIndex + 1, input.getSelectionEnd());
}
catch (BadLocationException ex)
{
log.warn("Could not insert character.", ex);
}
// Prevent default behavior.
e.consume();
}
else // Character to insert does not match current autocompletion. Look for another name.
{
newAutocomplete(e);
}
}
else // Search for a name to autocomplete
{
newAutocomplete(e);
}
}
private boolean preDispatchKeyEvent(KeyEvent ke) {
if (((AWTEvent) ke).isPosted) {
Component focusOwner = getFocusOwner();
ke.setSource(((focusOwner != null) ? focusOwner : getFocusedWindow()));
}
if (ke.getSource() == null) {
return true;
}
// Explicitly set the key event timestamp here (not in Component.dispatchEventImpl):
// - A key event is anyway passed to this method which starts its actual dispatching.
// - If a key event is put to the type ahead queue, its time stamp should not be registered
// until its dispatching actually starts (by this method).
EventQueue.setCurrentEventAndMostRecentTime(ke);
/**
* Fix for 4495473.
* This fix allows to correctly dispatch events when native
* event proxying mechanism is active.
* If it is active we should redispatch key events after
* we detected its correct target.
*/
if (KeyboardFocusManager.isProxyActive(ke)) {
Component source = (Component)ke.getSource();
Container target = source.getNativeContainer();
if (target != null) {
ComponentPeer peer = target.getPeer();
if (peer != null) {
peer.handleEvent(ke);
/**
* Fix for 4478780 - consume event after it was dispatched by peer.
*/
ke.consume();
}
}
return true;
}
java.util.List<KeyEventDispatcher> dispatchers = getKeyEventDispatchers();
if (dispatchers != null) {
for (java.util.Iterator<KeyEventDispatcher> iter = dispatchers.iterator();
iter.hasNext(); )
{
if (iter.next().
dispatchKeyEvent(ke))
{
return true;
}
}
}
return dispatchKeyEvent(ke);
}
private boolean preDispatchKeyEvent(KeyEvent ke) {
if (((AWTEvent) ke).isPosted) {
Component focusOwner = getFocusOwner();
ke.setSource(((focusOwner != null) ? focusOwner : getFocusedWindow()));
}
if (ke.getSource() == null) {
return true;
}
// Explicitly set the key event timestamp here (not in Component.dispatchEventImpl):
// - A key event is anyway passed to this method which starts its actual dispatching.
// - If a key event is put to the type ahead queue, its time stamp should not be registered
// until its dispatching actually starts (by this method).
EventQueue.setCurrentEventAndMostRecentTime(ke);
/**
* Fix for 4495473.
* This fix allows to correctly dispatch events when native
* event proxying mechanism is active.
* If it is active we should redispatch key events after
* we detected its correct target.
*/
if (KeyboardFocusManager.isProxyActive(ke)) {
Component source = (Component)ke.getSource();
Container target = source.getNativeContainer();
if (target != null) {
ComponentPeer peer = target.getPeer();
if (peer != null) {
peer.handleEvent(ke);
/**
* Fix for 4478780 - consume event after it was dispatched by peer.
*/
ke.consume();
}
}
return true;
}
java.util.List<KeyEventDispatcher> dispatchers = getKeyEventDispatchers();
if (dispatchers != null) {
for (java.util.Iterator<KeyEventDispatcher> iter = dispatchers.iterator();
iter.hasNext(); )
{
if (iter.next().
dispatchKeyEvent(ke))
{
return true;
}
}
}
return dispatchKeyEvent(ke);
}
@Override
public void keyPressed(final KeyEvent ke) {
final Object source = ke.getSource();
if (! (source instanceof DisplayCanvas)) return;
final DisplayCanvas dc = (DisplayCanvas)source;
final Roi roi = dc.getFakeImagePlus().getRoi();
final int mod = ke.getModifiers();
switch (ke.getKeyCode()) {
case KeyEvent.VK_C:
// copy into ImageJ clipboard
// Ignoring masks: outside is already black, and ImageJ cannot handle alpha masks.
if (0 == (mod ^ (Event.SHIFT_MASK | Event.ALT_MASK))) {
// Place the source image, untransformed, into clipboard:
final ImagePlus imp = getImagePlus();
if (null != imp) imp.copy(false);
} else if (0 == mod || (0 == (mod ^ Event.SHIFT_MASK))) {
CoordinateTransformList<CoordinateTransform> list = null;
if (hasCoordinateTransform()) {
list = new CoordinateTransformList<CoordinateTransform>();
list.add(getCoordinateTransform());
}
if (0 == mod) { //SHIFT is not down
final AffineModel2D am = new AffineModel2D();
am.set(this.at);
if (null == list) list = new CoordinateTransformList<CoordinateTransform>();
list.add(am);
}
ImageProcessor ip;
if (null != list) {
final TransformMesh mesh = new TransformMesh(list, meshResolution, o_width, o_height);
final TransformMeshMapping mapping = new TransformMeshMapping(mesh);
ip = mapping.createMappedImageInterpolated(getImageProcessor());
} else {
ip = getImageProcessor();
}
new ImagePlus(this.title, ip).copy(false);
}
ke.consume();
break;
case KeyEvent.VK_F:
// fill mask with current ROI using
if (null != roi && M.isAreaROI(roi)) {
Bureaucrat.createAndStart(new Worker.Task("Filling image mask") {
@Override
public void exec() {
getLayerSet().addDataEditStep(Patch.this);
if (0 == mod) {
addAlphaMask(roi, ProjectToolbar.getForegroundColorValue());
} else if (0 == (mod ^ Event.SHIFT_MASK)) {
// shift is down: fill outside
try {
final Area localRoi = M.areaInInts(M.getArea(roi)).createTransformedArea(at.createInverse());
final Area invLocalRoi = new Area(new Rectangle(0, 0, getOWidth() , getOHeight()));
invLocalRoi.subtract(localRoi);
addAlphaMaskLocal(invLocalRoi, ProjectToolbar.getForegroundColorValue());
} catch (final NoninvertibleTransformException e) {
IJError.print(e);
return;
}
}
getLayerSet().addDataEditStep(Patch.this);
try { updateMipMaps().get(); } catch (final Throwable t) { IJError.print(t); } // wait
Display.repaint();
}
}, project);
}
// capturing:
ke.consume();
break;
default:
super.keyPressed(ke);
break;
}
}
@Override
public void keyTyped(KeyEvent e)
{
if (!hiscoreConfig.autocomplete())
{
return;
}
final JTextComponent input = (JTextComponent)e.getSource();
final String inputText = input.getText();
// Only autocomplete if the selection end is at the end of the text.
if (input.getSelectionEnd() != inputText.length())
{
return;
}
// Character to be inserted at the selection start.
final String charToInsert = Character.toString(e.getKeyChar());
// Don't attempt to autocomplete if the name is invalid.
// This condition is also true when the user presses a key like backspace.
if (INVALID_CHARS.matcher(charToInsert).find()
|| INVALID_CHARS.matcher(inputText).find())
{
return;
}
// Check if we are already autocompleting.
if (autocompleteName != null && autocompleteNamePattern.matcher(inputText).matches())
{
if (isExpectedNext(input, charToInsert))
{
try
{
// Insert the character and move the selection.
final int insertIndex = input.getSelectionStart();
Document doc = input.getDocument();
doc.remove(insertIndex, 1);
doc.insertString(insertIndex, charToInsert, null);
input.select(insertIndex + 1, input.getSelectionEnd());
}
catch (BadLocationException ex)
{
log.warn("Could not insert character.", ex);
}
// Prevent default behavior.
e.consume();
}
else // Character to insert does not match current autocompletion. Look for another name.
{
newAutocomplete(e);
}
}
else // Search for a name to autocomplete
{
newAutocomplete(e);
}
}
protected @Override boolean processKeyBinding(KeyStroke ks,
KeyEvent e,
int condition,
boolean pressed) {
if (Utilities.isMac()) {
int mods = e.getModifiers();
boolean isCtrl = (mods & KeyEvent.CTRL_MASK) != 0;
boolean isAlt = (mods & KeyEvent.ALT_MASK) != 0;
if (isAlt && (e instanceof MarkedKeyEvent)) {
mods = mods & ~ KeyEvent.CTRL_MASK;
mods = mods & ~ KeyEvent.CTRL_DOWN_MASK;
mods |= KeyEvent.ALT_MASK;
mods |= KeyEvent.ALT_DOWN_MASK;
KeyEvent newEvent = new MarkedKeyEvent (
(Component) e.getSource(), e.getID(),
e.getWhen(), mods, e.getKeyCode(), e.getKeyChar(),
e.getKeyLocation());
KeyStroke newStroke = null;
if( null != ks ) {
newStroke = e.getID() == KeyEvent.KEY_TYPED ?
KeyStroke.getKeyStroke (ks.getKeyChar(), mods) :
KeyStroke.getKeyStroke (ks.getKeyCode(), mods,
!ks.isOnKeyRelease());
}
boolean result = super.processKeyBinding (newStroke,
newEvent, condition, pressed);
if (newEvent.isConsumed()) {
e.consume();
}
return result;
} else if (!isAlt) {
return super.processKeyBinding (ks, e, condition, pressed);
} else {
return false;
}
} else {
return super.processKeyBinding (ks, e, condition, pressed);
}
}
private boolean preDispatchKeyEvent(KeyEvent ke) {
getOnTypeaheadFinishedHandler().accept(ke);
if (((AWTEvent) ke).isPosted) {
Component focusOwner = getFocusOwner();
ke.setSource(((focusOwner != null) ? focusOwner : getFocusedWindow()));
}
if (ke.getSource() == null) {
return true;
}
// Explicitly set the key event timestamp here (not in Component.dispatchEventImpl):
// - A key event is anyway passed to this method which starts its actual dispatching.
// - If a key event is put to the type ahead queue, its time stamp should not be registered
// until its dispatching actually starts (by this method).
EventQueue.setCurrentEventAndMostRecentTime(ke);
/**
* Fix for 4495473.
* This fix allows to correctly dispatch events when native
* event proxying mechanism is active.
* If it is active we should redispatch key events after
* we detected its correct target.
*/
if (KeyboardFocusManager.isProxyActive(ke)) {
Component source = (Component)ke.getSource();
Container target = source.getNativeContainer();
if (target != null) {
ComponentPeer peer = target.getPeer();
if (peer != null) {
peer.handleEvent(ke);
/**
* Fix for 4478780 - consume event after it was dispatched by peer.
*/
ke.consume();
}
}
return true;
}
java.util.List<KeyEventDispatcher> dispatchers = getKeyEventDispatchers();
if (dispatchers != null) {
for (java.util.Iterator<KeyEventDispatcher> iter = dispatchers.iterator();
iter.hasNext(); )
{
if (iter.next().
dispatchKeyEvent(ke))
{
return true;
}
}
}
return dispatchKeyEvent(ke);
}