Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Hilfe bei KeyListener

Empfohlene Antworten

Veröffentlicht

Hallo Leute!

Ich habe leider mehrere Probleme mit dem Keylistener.

1. will ich den KeyListener auf ein JFrame setzten. Das ist syntaktisch auch noch korrekt. Dann hab ich zum Testen in der Methode "keyPressed" - mir einfach nur die gedrückte Taste ausgeben lassen. Allerdings wird die Methode niemals aufgerufen.


jframe.addKeyListener(this);


public void keyPressed(KeyEvent ke) {

	System.out.println(ke.getKeyChar());

}

2. habe ich vor in meinem JFrame einige Buttons - ähnlich wie bei Word - mit Shortcuts (z. B. [sTRG] + = Speichern, usw.) aufzurufen und dafür will ich nicht einzeln auf die buttons einen KeyListener legen, (weil dazu noch ein Focus auf denen liegen muss), sondern nur 1x im JFrame.

Meine Frage also - wie schaut der CODE aus, so wie ich es gerne wünsche. Oder einige Tipps wären auch schon recht hilfreich. Und noch dazu ... wie frage ich 2 gedrückte Tasten gleichzeitig ab? Für Beispiel siehe in Punkt 2.

Greetz

Seh ich das richtig, dass deine Main-Klasse das KeyListenerInterface implementiert ? :eek

Hier mal n Vorschlag zum Aufbau mit beiden Punkten... weiß ja net wie deine Klasse komplett aussieht:

public class TestFenster extends JFrame {

private JPanel Jpan1;

private JButton Jbtn1;

public TestFenster() {

Jpan1 = new JPanel();
Jpan1.setLayout(new FlowLayout());

Jbtn1 = new JButton("Klick - mich");
Jbtn1.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent arg0) {

buttonFunc();
}

});

Jpan1.add(Jbtn1);
add(Jpan1);

setDefaultCloseOperation(EXIT_ON_CLOSE);

pack();
setSize(300,300);

setVisible(true);
}


private static void buttonFunc() {

System.out.println("ButtonFunktion ausgeführt..");
}

public static void main(String[] args) {

//EventQueue einfügen, die Events vor den Listenern abfängt
Toolkit.getDefaultToolkit().getSystemEventQueue().push(
new EventQueue() {
protected void dispatchEvent(AWTEvent event) {
if (event instanceof KeyEvent) {
KeyEvent keyEvent = (KeyEvent) event;

// Strg + S
if ((keyEvent.getID() == KeyEvent.KEY_PRESSED) &&
((keyEvent).getKeyCode() == KeyEvent.VK_S) &&
(keyEvent.getModifiers() == 2)) {

System.out.println("Shortcut benutzt..");
buttonFunc();
}
}
super.dispatchEvent(event);
}
});

new TestFenster();
}

}[/PHP]

Greets JFlash

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.