KeyEvent simple en java
Tuesday, 24 de February de 2009
Este código me fué de mucha ayuda cuando recién empecé a ver algo de java en el instituto, espero que le sirva a las nuevas generaciones de programadores.
Acá tenemos un keyListener, su función es captar los eventos del teclado,tanto con o sin modificadores, esto se refiere a si el usuario usa SHIFT o no.
El código está funcionando, es autónomo, para ejecutar el ejemplo se debe agregar al proyecto a darle RUN.
JAVA:
-
package keyListener;
-
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.BorderLayout;
-
import java.awt.Dimension;
-
-
implements KeyListener,
-
JTextArea displayArea;
-
JTextField typingArea;
-
-
public KeyEventDemo() {
-
-
button.addActionListener(this);
-
-
typingArea.addKeyListener(this);
-
-
displayArea.setEditable(false);
-
-
}
-
-
/** Handle the key typed event from the text field. */
-
displayInfo(e, "KEY TYPED: ");
-
}
-
-
/** Handle the key pressed event from the text field. */
-
displayInfo(e, "KEY PRESSED: ");
-
}
-
-
/** Handle the key released event from the text field. */
-
displayInfo(e, "KEY RELEASED: ");
-
}
-
-
/** Handle the button click. */
-
//Limpia los componentes de texto.
-
displayArea.setText("");
-
typingArea.setText("");
-
-
//Vuelve el foco al área de tipeo.
-
typingArea.requestFocusInWindow();
-
}
-
-
String keyString, modString, tmpString,
-
actionString, locationString;
-
-
int id = e.getID();
-
char c = e.getKeyChar();
-
keyString = "key character = '" + c + "'";
-
} else {
-
int keyCode = e.getKeyCode();
-
keyString = "key code = " + keyCode
-
+ " ("
-
+ ")";
-
}
-
-
int modifiers = e.getModifiersEx();
-
modString = "modificadores = " + modifiers;
-
if (tmpString.length()> 0) {
-
modString += " (" + tmpString + ")";
-
} else {
-
modString += " (sin modificadores)";
-
}
-
-
actionString = "¿Tecla de acción? ";
-
if (e.isActionKey()) {
-
actionString += "SI";
-
} else {
-
actionString += "NO";
-
}
-
-
locationString = "Ubicación tecla: ";
-
int location = e.getKeyLocation();
-
locationString += "standard";
-
locationString += "izquierda";
-
locationString += "derecha";
-
locationString += "numpad";
-
} else {
-
// (location == KeyEvent.KEY_LOCATION_UNKNOWN)
-
locationString += "desconocido";
-
}
-
-
displayArea.append(s + nuevaLinea
-
+ "Â Â Â " + keyString + nuevaLinea
-
+ "Â Â Â " + modString + nuevaLinea
-
+ "Â Â Â " + actionString + nuevaLinea
-
+ "Â Â Â " + locationString + nuevaLinea);
-
displayArea.setCaretPosition(displayArea.getDocument().getLength());
-
}
-
-
/**
-
* Se crea la GUI y se muestra.
-
*/
-
private static void createAndShowGUI() {
-
//Se crea y setea la ventana.
-
-
//Se crea y setea el content pane.
-
newContentPane.setOpaque(true); //content panes deben ser opacos
-
frame.setContentPane(newContentPane);
-
-
//Muestra la ventana
-
frame.pack();
-
frame.setVisible(true);
-
}
-
-
//Creando y mostrando la gui de esta aplicación.
-
public void run() {
-
createAndShowGUI();
-
}
-
});
-
}
-
}
Comparte esto en :
Programación, java


Encuentrame en Flickr