joepa37
joepa37

Reputation: 23

Java - Caps Lock status on form focus gained

Well, I have the following code to check the status of caps lock, when I change the status of the caps lock with focu in the form there is no problem but when I minimize the form and change the status of caps lock and back to maximize the form not update the status of the caps lock. Any ideas?

/**
 *
 * @author joepa37
 */
public class FocusExample extends javax.swing.JFrame {

    public FocusExample() {
        initComponents();
    }

    @SuppressWarnings("unchecked")                     
    private void initComponents() {

        capsLockLbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridLayout());

        capsLockLbl.setText("CAPS LOCK : ");
        getContentPane().add(capsLockLbl);

        pack();
    }                    

    private void formKeyPressed(java.awt.event.KeyEvent evt) {                                
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_CAPS_LOCK) {
            updateStatus();
        }
    }                               

    private void formFocusGained(java.awt.event.FocusEvent evt) {                                 
        updateStatus();
    }                                

    private void updateStatus(){
        boolean isCapsLockOn = java.awt.Toolkit.getDefaultToolkit ().getLockingKeyState ( java.awt.event.KeyEvent.VK_CAPS_LOCK );
        capsLockLbl.setText(isCapsLockOn ? "CAPS LOCK ON" : "CAPS LOCK OFF");
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FocusExample().setVisible(true);
            }
        });
    }

    private javax.swing.JLabel capsLockLbl;         
}

Upvotes: 3

Views: 416

Answers (1)

joepa37
joepa37

Reputation: 23

I found a solution, is not so professional but it works. What I did is add a robot to press to times caps lock when the form comes into focus. Two times to return to the original state that the user left the last time, apparently it updates the event perfectly.

this.addWindowListener(new WindowAdapter() {
    @Override
    public void windowActivated(WindowEvent we) {
        try {
            java.awt.Robot r = new java.awt.Robot();

            //first time to update the event
            r.keyPress(KeyEvent.VK_CAPS_LOCK);
            r.keyRelease(KeyEvent.VK_CAPS_LOCK);

            //second time to return to the last time status
            r.keyPress(KeyEvent.VK_CAPS_LOCK);
            r.keyRelease(KeyEvent.VK_CAPS_LOCK);

        } catch (Exception e) {}

      updateStatus();

    }
}
);

Upvotes: 1

Related Questions