0
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

public class Test implements KeyListener

I have a JTextField:

private static JTextField tf = new JTextField();

In the main method I have:

tf.addKeyListener(new Test());

Then I have the abstract methods KeyTyped, KeyPressed, KeyReleased but the problem is that the code in this if statement: if(e.getKeyCode() == KeyEvent.VK_ENTER) isn't activating.

@Override
public void keyTyped(java.awt.event.KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_ENTER){
         System.out.println("This is working");
    }
}

If I take this out:

if(e.getKeyCode() == KeyEvent.VK_ENTER)

And leave the System.out.println(); it activates but I wan't to write code for several conditions so I can create a specific function for different keys.

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

/**
 * Created by Kyle on 7/15/2017.
 */
 public class KeyEvent implements KeyListener {
 @Override
 public void keyTyped(java.awt.event.KeyEvent e) {
    if(e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER){
        System.out.println("Test");
    }
 }

 @Override
 public void keyPressed(java.awt.event.KeyEvent e) {

 }

 @Override
 public void keyReleased(java.awt.event.KeyEvent e) {

 }

 public static void main(String[] args){
    JFrame f = new JFrame();
    f.setSize(500, 600);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.addKeyListener(new KeyEvent());
    f.setVisible(true);
    }
 }
Kyle Goertzen
  • 143
  • 1
  • 1
  • 8
  • This simply means that the KeyEvent which is passed to the method is not `KeyEvent.VK_ENTER`... – Nir Alfasi Jul 15 '17 at 17:18
  • Maybe you... didn't press the enter key? Post a complete minimal example reproducing the problem, and tell us precisely what you're doing to test it. – JB Nizet Jul 15 '17 at 17:18
  • 1
    This could beyour answer: https://stackoverflow.com/questions/4419667/detect-enter-press-in-jtextfield – Earl Dumarest Jul 15 '17 at 17:30
  • @JBNizet I edited the post to show you a simpler example with the same problem that when I press enter nothing happens, I will look into your answer Earl Dumarest now – Kyle Goertzen Jul 15 '17 at 17:33
  • @EarlDumarest hey Earl, I added this to the JTextField is there a way to write a statement for a certain key being pressed in stead of any key being pressed? tf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.print("Hi"); } }); – Kyle Goertzen Jul 15 '17 at 17:40
  • @EarlDumarest thank you for linking me that question from the past. It worked great for me! – Kyle Goertzen Jul 15 '17 at 17:54

1 Answers1

0

keyTyped event will work for something that can be printed - the Unicode character represented by the key is sent by the keyboard to system input.

You could use keyReleased method to capture the ENTER key event so that it's called only when the key is released.

OTM
  • 656
  • 5
  • 8