KeyListener only sometimes works - java

I am writing a game and I have just tried to add the KeyListener. I have experience with java including KeyListeners but I for some reason cannot figure out why this code only works some of the time.
Here is my listener function:
public void Listener() {
System.out.println("[INFO] Listener() Ran.");
KeyListener kl = new KeyListener() {
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()=='a'){
System.out.println("[DEBUG] A Pressed.");
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
};
panel.addKeyListener(kl);
System.out.println("[DEBUG] panel added KeyListener.");
}
This code works probably only 1 out of 10 times that I run it. Maybe even less. Any ideas on why this is?

The getKeyChar should be called in the keyTyped. The getKeyCode() == KeyEvent.VK_A in the other both methods.

Related

Java AutoComplete : Popup on every KeyPress

I am using Java AutoComplete library to make an IDE. I was wondering how can I make the popup window show on every KeyPress. I have tried :
autoCompletion.setAutoCompleteSingleChoices(false);
textArea.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
autoCompletion.doCompletion();
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
});
But it just doesn't work how I wanted. I need it to work this way :
Demonstration here
Any help is highly appreciated. Thanks :)

Double click JTable

The cells in my JTable become editable only on the second click. When I debugged I noticed that on the second click the mouse released event is not fired. I saw a lot of answers for this problem with create a setSingleClick(1)... but it doesn't to work. I think that if i can get that second mouseReleased event to get fire i might be able to make it work. Does anybody has any sugestions?
table.addMouseListener(new TableMouseListener()) ;
class TableMouseListener extends MouseAdapter{
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
}
Try something like this:
container_table.addMouseListener(new MouseAdapter() {
public void mouseClicked (MouseEvent me) {
if (me.getClickCount() == 2) {
//Double clicked
}
}
});
This way, you know that the 'container_table' has been clicked twice, and then you can get the selected row, and make things with it.
Hope it helps.

Create an actionlistener(?) in Java

So I searched Stackoverflow, but couldn't find any actual answer that I got. If there's already an answer to this question, please tell me.
I have a class with a showDescription method. This prints a string variable.
I require this method to be called whenever the "d" key is pressed, in the main method. So, what would the code be to implement the key press/down event?
Do this if you have a swing application:
f.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == KeyEvent.VK_D) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
System.out.println("woot!");
}
}
#Override
public void keyReleased(KeyEvent e) {
}
});
you can read more here and here
If you have a console application then use:
Read Input until control+d

Java - keyboard state after leaving window

The title might be a little misleading, didnt know how to put my problem short.
Basically what im doing is im using keyboardlistener to find out which keys are down and according to that im moving my game character.
The problem is, when you click out of the window, while holding down a key my listener doesnt register the keyReleased event.
I tried to fix it by using mouse listener and the mouseExited event, but that doesnt fix it all the time, sometimes it does sometimes it doesnt.
Heres my implementation:
Keyboard:
public void mouseLeftWindow()
{
for(int i =0;i<KEY_COUNT;i++)
{
keys[i] = false;
}
}
#Override
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = true;
}
}
#Override
public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = false;
}
}
where keys[] is a boolean[] describing, which codes are pressed
mouse:
#Override
public void mouseExited(MouseEvent e)
{
mouseMoved(e);
keyboard.mouseLeftWindow();
}
Your program will listen for further key events even when your mouse exited the component. That means you set everything to false on exit but if a key is still pressed it will be set to true immediately again. I think you are looking for a FocusListener instead of a MouseListener.
addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
}
#Override
public void focusLost(FocusEvent e) {
keyboard.mouseLeftWindow();
}
});

Keys & Buttons compatibiliy Java

I have made my own version of Tetris in Java and i have added the possibility of moving the shapes both with JButtons and with certain keyboard keys. The code snippet i used is the following:
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
moveLeft();
}
});
rightButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
moveRight();
}
});
rotateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent E) {
rotateMovingShape();
}
});
myPanel.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == event.VK_A)
{
moveLeft();
}
if (keyCode == event.VK_D)
{
moveRight();
}
if (keyCode == event.VK_S)
{
rotateMovingShape();
}
}
});
The problem i have is that after i use the JButtons, i cannot longer control the shapes with the keyboard keys. I suspect it has something to do with gaining/losing focus, but i am not sure. Could anyone tell me what's going on?
You get that problem, because you use KeyListener, instead of that you need to use Key Bindings. For example:
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A,0), "aPressed");
component.getActionMap().put("aPressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("a key");
}
});
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0), "dPressed");
component.getActionMap().put("dPressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("d key");
}
});
// other bindings
where component is your JPanel.
A KeyListener only receives key events when the component has the keyboard focus. Clicking on the buttons transfers the focus to them and away from your panel, so you don't get the events. Any one of the following approaches will solve this:
Call setFocusable(false); on the buttons so that they won't steal the focus.
Add the KeyListener to the buttons too.
Use key bindings instead of a KeyListener, so that you can catch the key press whether the component has focus or not.

Categories

Resources