Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing keystrokes in a panel in java

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;
}
like image 979
rgksugan Avatar asked Dec 22 '22 03:12

rgksugan


1 Answers

Actually, what you want is a KeyboardManager to install a KeyEventPostProcessor, or even an alternate dispatch for various components. I had the same issues in a NB app as you did.

Here's the article I used: (developer.com)

I did this to capture enter key events from any control in a panel (to initiate "OK" button behavior automatically):

    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventPostProcessor(new EnterKeyListener());

And the listener:

    class EnterKeyListener implements KeyEventPostProcessor {

    @Override
    public boolean postProcessKeyEvent(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            //Activate the refresh button:
            fireSearch();
            return true;    //halt further processing
        }
        return false;
    }
}
like image 105
Daver Avatar answered Jan 01 '23 13:01

Daver