Kurz:"alternative" Tasten mit Java Swing lesen
Wie kann ich verschiedene Tasten wie ESC, ENTER, STRG, ALT mit Swing lesen?
Kurz:"alternative" Tasten mit Java Swing lesen
Wie kann ich verschiedene Tasten wie ESC, ENTER, STRG, ALT mit Swing lesen?
Nun, KeyStroke
identifiziert Aktionen, wird er Ihnen auf verschiedene wichtige Ereignisse zu handeln.
Was Sie tun müssen, ist Karte Aktionen jeder Taste, etwa so:
// Create key stoke and bind the key stroke to an action
component.getInputMap().put(KeyStroke.getKeyStroke("alt"), "actionName");
// Add the action to the component
component.getActionMap().put("actionName",
new AbstractAction("actionName") {
public void actionPerformed(ActionEvent evt) {
//do something here
}
}
);
Die Aktion aufgerufen wird, sobald der Tastenhub gedrückt wird.
Auch kann es hilfreich sein, etwa KeyStroke
an zu lesen: http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/KeyStroke.html
Ich hoffe, das hilft und dass ich verstanden, was du mit ‚lesen verschiedene Schlüssel‘
Sie müssen das Sun-Tutorial unter key bindings lesen.
Hier eine Demo ist Ihnen zu helfen, die Tastatur des gedrückten Taste (Oracle website) erfassen:
auf der Tastaturpublic class KeyEventDemo ... implements KeyListener ... {
...//where initialization occurs:
typingArea = new JTextField(20);
typingArea.addKeyListener(this);
//Uncomment this if you wish to turn off focus
//traversal. The focus subsystem consumes
//focus traversal keys, such as Tab and Shift Tab.
//If you uncomment the following line of code, this
//disables focus traversal and the Tab events
//become available to the key event listener.
//typingArea.setFocusTraversalKeysEnabled(false);
...
/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
/** Handle the key-pressed event from the text field. */
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
/** Handle the key-released event from the text field. */
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}
...
private void displayInfo(KeyEvent e, String keyStatus){
//You should only rely on the key char if the event
//is a key typed event.
int id = e.getID();
String keyString;
if (id == KeyEvent.KEY_TYPED) {
char c = e.getKeyChar();
keyString = "key character = '" + c + "'";
} else {
int keyCode = e.getKeyCode();
keyString = "key code = " + keyCode
+ " ("
+ KeyEvent.getKeyText(keyCode)
+ ")";
}
...//Display information about the KeyEvent...
}
}
Sowohl gute Antworten gemeint, aber Sie haben mehr zu die Stelle. Danke vielmals! –