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 776981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:32:09+00:00 2026-05-14T19:32:09+00:00

I want to capture keystrokes when the focus in on a panel in java.

  • 0

I want to capture keystrokes when the focus in on a panel in java. What should i do?

I am using Netbeans as the IDE. I tried adding keyTyped event but it doesnot work.

Here goes my code

import com.lanadmin.Interface.ClientInterface;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.rmi.Naming;
import java.util.logging.Level;


public class RemoteViewer extends javax.swing.JInternalFrame {


    public RemoteViewer() {
        initComponents();
        rdpanel.setFocusable(true);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        rdpanel = new javax.swing.JPanel();
        setFocusable(true);

        rdpanel.setToolTipText("Remote Desktop");
        rdpanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rdpanelMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                rdpanelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                rdpanelMouseReleased(evt);
            }
        });
        rdpanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                rdpanelMouseMoved(evt);
            }
        });
        rdpanel.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                rdpanelFocusGained(evt);
            }
        });
        rdpanel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                rdpanelKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                rdpanelKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                rdpanelKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout rdpanelLayout = new javax.swing.GroupLayout(rdpanel);
        rdpanel.setLayout(rdpanelLayout);
        rdpanelLayout.setHorizontalGroup(
            rdpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 679, Short.MAX_VALUE)
        );
        rdpanelLayout.setVerticalGroup(
            rdpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 626, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rdpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rdpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        
    private void rdpanelMouseMoved(java.awt.event.MouseEvent evt) {                                   
        double xScale = Toolkit.getDefaultToolkit().getScreenSize().getWidth() / rdpanel.getWidth();
        double yScale = Toolkit.getDefaultToolkit().getScreenSize().getHeight() / rdpanel.getHeight();
        try {
            String current = Login.handler.currentClientAddress.getHostAddress();
            ClientInterface client = (ClientInterface) Naming.lookup("rmi://" + current + "/lan");
            client.mousemove((int) (evt.getX() * xScale), (int) (evt.getY() * yScale));
        } catch (Exception ex) {
            Log2File.logger.log(Level.SEVERE, null, ex);
            Log2File.fileHandler.flush();
        }
    }                                  
    private void rdpanelKeyPressed(java.awt.event.KeyEvent evt) {                                   
        System.out.println("keypressed");
    }                                  
    private void rdpanelKeyTyped(java.awt.event.KeyEvent evt) {                                 
        System.out.println("aaa");
    }                                
    private void rdpanelKeyReleased(java.awt.event.KeyEvent evt) {                                    
        System.out.println("keyreleased");
    }                                   
   private void rdpanelFocusGained(java.awt.event.FocusEvent evt) {                                    
        rdpanel.addKeyListener(new java.awt.event.KeyAdapter() {
           @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                rdpanelKeyPressed(evt);
            }
            @Override
            public void keyReleased(java.awt.event.KeyEvent evt) {
                rdpanelKeyReleased(evt);
            }
            @Override
            public void keyTyped(java.awt.event.KeyEvent evt) {
                rdpanelKeyTyped(evt);
            }
        });
    }                                   
    public javax.swing.JPanel rdpanel;
}
  • 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-05-14T19:32:09+00:00Added an answer on May 14, 2026 at 7:32 pm

    You need to add a new FocusListener and a new KeyListener to the panel. If you only want the keystrokes captured when the panel is in focus, get the FocusListener’s action to add the KeyListener and remove it.

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

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Most well-known Ajax frameworks like jQuery and mooTools add a… May 15, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer I solved my problem, I had to write a custom… May 15, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer list all the other cpp files after main.cpp. ie g++… May 15, 2026 at 5:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.