Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:56:12+00:00 2026-06-15T01:56:12+00:00

When I start up my applet, I get no response from the key listener.

  • 0

When I start up my applet, I get no response from the key listener. How do I fix this? Below is the code.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;

public class Main extends Applet {
private static final long serialVersionUID = 1L;
public String mode = "";
public Graphic gr;

public void init() {
    this.setSize(400, 400);
    gr = new Graphic();
    this.add(gr);
    gr.addKeyListener(new MyKeyListener());
    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
                    gr.requestFocus();
            gr.repaint();
        }

    };
    new Timer().scheduleAtFixedRate(timerTask, 0, 1000 / 5);

}

public void paint(Graphics g) {

}

private class MyKeyListener extends KeyAdapter {
    public MyKeyListener(){
        System.out.println("HELLO");
    }
    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            gr.direction = Graphic.Direction.up;
            gr.move();

            break;
        case KeyEvent.VK_DOWN:
            gr.direction = Graphic.Direction.down;
            gr.move();
            break;
        case KeyEvent.VK_LEFT:
            gr.direction = Graphic.Direction.left;
            gr.move();
            break;
        case KeyEvent.VK_RIGHT:
            gr.direction = Graphic.Direction.right;
            gr.move();
            break;
        }
    }

    public void keyReleased(KeyEvent e) {

    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:56:13+00:00Added an answer on June 15, 2026 at 1:56 am
        import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Shape;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class TTest extends Applet{
        @Override
        public void init() {
            // TODO Auto-generated method stub
            super.init();
            this.setSize(400, 400);
            addKeyListener(new KeyListener());
    
        }
        class KeyListener extends KeyAdapter{
            public KeyListener(){
    
            }
    
            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                super.keyPressed(e);
                switch(e.getKeyCode()){
                case KeyEvent.VK_UP:
                    System.out.println("Pressed up arrow!");
                    break;
                case KeyEvent.VK_DOWN:
                    System.out.println("Pressed down arrow!");
                    break;
                default:
                    System.out.println("You pressed: " + e.getKeyCode());
                }
            }
    
        }
    }
    

    You must add key event listener to applet itself not to graphic object. However, I think it is ok to add mouse event listener to objects apart from applet

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible for a signed Java Applet or Web Start app to write
I start a download using PHP with this code: <? $_REQUEST['file'] = urldecode($_GET['file']); header(Content-type:
the code below explains the problem in detail #this returns error Net::HTTPBadResponse url =
I have a simple java applet that retrieves an image from a server and
I have a java applet trying to pass some GET data to a php
I'm writing a status checker, it is a java web-start applet. I can run
I am having issues with my java web start applet in netbeans. I cannot
This is my first project in Java. I'm creating batch FTP video uploader applet
My run script start.bat java -server -Xmx1024m -Xbootclasspath/p:/Java/Server Applet/bin applet.Server When I execute it
I've decided to start experimenting with Ruby to get out of my little Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.