How does KeyListener work?

I have two(relevant) classes which ive made trying to figure out how this works.
package com.blockgoblin31.game;

import javax.swing.*;
import java.awt.*;

public class Main {
public static int xSize;
public static int ySize;
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea area = new JTextArea();
JScrollPane pane = new JScrollPane(area);
pane.setPreferredSize(new Dimension(375, 125));
area.setEditable(false);
frame.getContentPane().add(pane);
Game game = new Game(area, frame);
frame.addKeyListener(game);
frame.pack();
frame.setVisible(true);
System.out.println("running");
}
}
package com.blockgoblin31.game;

import javax.swing.*;
import java.awt.*;

public class Main {
public static int xSize;
public static int ySize;
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea area = new JTextArea();
JScrollPane pane = new JScrollPane(area);
pane.setPreferredSize(new Dimension(375, 125));
area.setEditable(false);
frame.getContentPane().add(pane);
Game game = new Game(area, frame);
frame.addKeyListener(game);
frame.pack();
frame.setVisible(true);
System.out.println("running");
}
}
package com.blockgoblin31.game;

import com.blockgoblin31.game.input.KeyContainer;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;

public class Game implements KeyListener {

private JTextArea area;

private JFrame frame;

public Game(JTextArea area, JFrame frame) {
this.area = area;
this.frame = frame;
}

public boolean close = false;

@Override
public void keyTyped(KeyEvent e) {
turn(new KeyContainer(e));
System.out.println("Text");
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}

public void turn(KeyContainer key) {
if (key.isKey("A")) {
area.append("A Pressed");
area.setCaretPosition(area.getDocument().getLength());
}
else if (key.isKeyCombination("C", new KeyContainer.ModifierType[]{KeyContainer.ModifierType.CONTROL})) frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
package com.blockgoblin31.game;

import com.blockgoblin31.game.input.KeyContainer;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;

public class Game implements KeyListener {

private JTextArea area;

private JFrame frame;

public Game(JTextArea area, JFrame frame) {
this.area = area;
this.frame = frame;
}

public boolean close = false;

@Override
public void keyTyped(KeyEvent e) {
turn(new KeyContainer(e));
System.out.println("Text");
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}

public void turn(KeyContainer key) {
if (key.isKey("A")) {
area.append("A Pressed");
area.setCaretPosition(area.getDocument().getLength());
}
else if (key.isKeyCombination("C", new KeyContainer.ModifierType[]{KeyContainer.ModifierType.CONTROL})) frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
21 Replies
JavaBot
JavaBot12mo ago
This post has been reserved for your question.
Hey @blockgoblin31! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
blockgoblin31
blockgoblin31OP12mo ago
Running prints and the window is created but after that nothing else happens most of this is me trying random stuff from the tutorial linked from the KeyListener page on the oracle docs
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
Ive done that frame.addKeyListener(game); line 18 of the first giant code block
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
yes, that line is called(the println for "running" works), but none of the other printlns ive scattered around work say the one in keyTyped
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
It also didn’t work when I didn’t have any other components Just the jframe and keylistener
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
so what here would be focusable? I tried adding the listener to the JScrollPane and JTextArea too in the example they added it to a JTextField, which makes sense I guess though I thought I tried that and it didnt work ok when I do that it works now... Is there a way to get it to work without needing a textbox?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
I've been looking at that, when I said "the tutorial" I meant the file linked at the bottom there and I dont understand how I'd convert an Action into code somewhere else with key bindings, or what an Action is really
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
ok thanks I'll try that out, but tomorrow, Ive done too much coding already today
JavaBot
JavaBot12mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
blockgoblin31
blockgoblin31OP12mo ago
so I've now tried doing it with keybindings, which is way nicer, but I can't figure out why one keybind works and the other doesnt
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "a");
pane.getActionMap().put("a", new KeyAction(game, "a"));
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK), "ctrl+c");
pane.getActionMap().put("ctrl+c", new KeyAction(game, "c", new KeyContainer.ModifierType[]{KeyContainer.ModifierType.CONTROL}));
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "a");
pane.getActionMap().put("a", new KeyAction(game, "a"));
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK), "ctrl+c");
pane.getActionMap().put("ctrl+c", new KeyAction(game, "c", new KeyContainer.ModifierType[]{KeyContainer.ModifierType.CONTROL}));
so with this the first action works but the second doesnt I've confirmed with debugger only the first one is getting triggered
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
oh yep that was it, worked fine with b
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP12mo ago
Maybe thats left alone because the focused component is a textbox, so it uses copy instead of passing it on either way ty again
JavaBot
JavaBot12mo ago
Post Closed
This post has been closed by <@501514065068294154>.
Want results from more Discord servers?
Add your server