The basic problem is one of context. input is declared as a local variable within the context of the constructor, so it can not be accessed from any other context

 public void test()
    {
        //...
        TextField input=new TextField("");
        //...
    }

To be honest, this is pretty basic Java 101, you should start by having a look at Understanding Class Members. This is a concept you should already understand before embarking on GUI development (IMHO)

Observations

  • Avoid KeyListener (generally), especially with text components
  • Use an ActionListener instead
  • AWT is out-of-date, consider Swing or, better yet, JavaFX
  • Avoid null layouts, they will waste your time
  • As a general rule, you should not be extending directly from top level containers like Frame. They lock you into a single use case, reduce re-usability and you're not really adding any new functionality to the class anyway
Answer from MadProgrammer on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › awt › event › KeyEvent.html
KeyEvent (Java Platform SE 8 )
3 weeks ago - The event is passed to every KeyListener or KeyAdapter object which registered to receive such events using the component's addKeyListener method. (KeyAdapter objects implement the KeyListener interface.) Each such listener object gets this KeyEvent when the event occurs.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-keylistener-in-awt
Java KeyListener in AWT - GeeksforGeeks
April 28, 2025 - //Java program to demonstrate keyPressed, // keyReleased and keyTyped method import java.awt.*; import java.awt.event.*; public class KeyListenerExample extends Frame implements KeyListener { private TextField textField; private Label displayLabel; // Constructor public KeyListenerExample() { // Set frame properties setTitle("Typed Text Display"); setSize(400, 200); setLayout(new FlowLayout()); // Create and add a TextField for text input textField = new TextField(20); textField.addKeyListener(this); add(textField); // Create and add a Label to display typed text displayLabel = new Label("Type
🌐
Matt Bognar
homepage.divms.uiowa.edu › ~slonnegr › 13 › qref11 › java.awt.event.KeyEvent.html
Class java.awt.event.KeyEvent
Java in a Nutshell Online Quick Reference for Java 1.1 ... public class KeyEvent extends InputEvent { // Public Constructors public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar); public KeyEvent(Component source, int id, long when, int modifiers, int keyCode); // Constants public static final char CHAR_UNDEFINED; public static final int KEY_FIRST; public static final int KEY_LAST; public static final int KEY_PRESSED; public static final int KEY_RELEASED; public static final int KEY_TYPED; public static final int VK_0; public static final int VK_1; publ
🌐
Android Developers
developer.android.com › api reference › keyevent
KeyEvent | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Find elsewhere
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.desktop › share › classes › java › awt › event › KeyEvent.java
jdk/src/java.desktop/share/classes/java/awt/event/KeyEvent.java at master · openjdk/jdk
* @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html">Tutorial: Writing a Key Listener</a> ... /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */ public static final int VK_KANA_LOCK = 0x0106; ... * @deprecated as of JDK1.1; use {@link #KeyEvent(Component, int, long, int, int, char)} instead
Author   openjdk
🌐
TutorialsPoint
tutorialspoint.com › awt › awt_key_event.htm
AWT KeyEvent Class
On entering the character the Key event is generated.There are three types of key events which are represented by the integer constants. These key events are following Following is the declaration for java.awt.event.KeyEvent class: Following are the
🌐
Bureau of Economic Geology
beg.utexas.edu › lmod › agi.servlet › doc › detail › java › awt › event › KeyEvent.html
java.awt.event Class KeyEvent
The event is passed to every KeyListener or KeyAdapter object which registered to receive such events using the component's addKeyListener method. (KeyAdapter objects implement the KeyListener interface.) Each such listener object gets this KeyEvent when the event occurs.
🌐
CodeHS
codehs.com › tutorial › 19325
Tutorial: Java Swing Keyboard Events | CodeHS
Click on one of our programs below to get started coding in the sandbox · In this tutorial, students will look at keyboard events in Java Swing. The tutorial assumes some understanding of Java and basic Java Swing. Students will create a rectangle object and use keyboard inputs to alter that object
🌐
Coderanch
coderanch.com › t › 670067 › java › Dispatching-keyPressed-keyReleased-events-software
Dispatching keyPressed and keyReleased events in software. (Swing / AWT / SWT forum at Coderanch)
However, when I click and hold down the mouse button on the puppeteer, the puppeteer receives a MouseEvent sent to its mousePressed MouseListener method which, in turn, creates a KeyEvent with the KEY_PRESSED parameter that is given to the top-most JFrame's dispatchEvent method.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 408356 › keylistener
java - Keylistener [SOLVED] | DaniWeb
As for your solution you need to add an empty implementation of keyTyped() in your class since you have inherited java.awt.event.KeyListener ,you will need to implement all methods specified in it. I'm really happy now becouse my code accually compiles, I did what you said so now I have this · public void keyPressed (KeyEvent c) { int key = c.getKeyCode (); System.out.println (key); int space = 32; if (key == space) { System.out.println ("Space was pressed"); } } public void keyReleased (KeyEvent e) { } public void keyTyped (KeyEvent e) { } But for some reason it wont react, I found out on google that space is apparently number 32 or somthing?
🌐
Coderanch
coderanch.com › t › 334892 › java › KeyEvent-array
KeyEvent array? (Swing / AWT / SWT forum at Coderanch)
Isn't that applying the == operator to a KeyEvent? I thought that is what I was doing, only using an array. Could someone take a look at my code and tell me what I've done wrong? Thanks ! ... [ December 05, 2002: Message edited by: Jennifer Sohl ] [ December 05, 2002: Message edited by: Jennifer Sohl ] ... If you look in the JavaDocs for KeyEvent KeyEvent.VK_BACK_SPACE is an int.
🌐
Toptal
toptal.com › developers › keycode
JavaScript Key Code Event Tool | Toptal®
KeyCode.Info allows users to press any key and instantly get the JavaScript Key or Key Code KeyboardEvent. Check out the Tool and Event List.
🌐
Glitch
support.glitch.com › coding help
KeyEvent not responding in Java? - Coding Help - Glitch Community Forum
December 16, 2020 - import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.concurrent.ThreadLocalRandom; public class Game { public static void main(String[] args) { new Game().start(); } …
🌐
Processing
processing.github.io › processing-javadocs › core › processing › event › KeyEvent.html
KeyEvent
java.lang.Object · processing.event.Event · processing.event.KeyEvent · public class KeyEvent extends Event · ALT, CTRL, KEY, META, MOUSE, SHIFT, TOUCH · getAction, getFlavor, getMillis, getModifiers, getNative, isAltDown, isControlDown, isMetaDown, isShiftDown ·