10

I would like a specific example on how to turn caps lock on if it is off.

I know how to toggle the key, I have been using this:

toolkit.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, Boolean.TRUE);

That will change the state of the key whether it is on or off. But I want to make sure it is on at the beginning of the application.

(The final goal is having the keyboard LEDs flash in certain sequences, which works better if I have a certain starting state.)

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Mike George
  • 521
  • 3
  • 11
  • 27
  • 1
    Have you looked at `getLockingKeyState()`? http://download.oracle.com/javase/1.5.0/docs/api/java/awt/Toolkit.html#getLockingKeyState(int) – NPE Sep 15 '11 at 17:49
  • I actually tried using that but I had trouble putting it in an if else statement for some reason. I would like to use it this way. I was able to use that as a boolean and tried to do it that way. Do you have an example where you would check the state of the caps lock toggle and if it was already on you would just continue else use the method I listed above to turn it on. Thanks for your help. I will look at your link now too. – Mike George Sep 15 '11 at 18:16
  • What are you trying to achieve by doing this? It seems (whatever it is), that you are approaching it the wrong way. – Andrew Thompson Sep 15 '11 at 18:19
  • 2
    Im actually just doing a fun project for AP Computer science and I just want the lights to flash on the keyboard in certain sequences and in order to get the best effect all the keys have to start by either being toggled on or off. – Mike George Sep 15 '11 at 19:08

3 Answers3

19

You can use getLockingKeyState to check if Caps Lock is currently set:

boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

However, it's unnecessary -- setLockingKeyState doesn't toggle the state of the key, it sets it. If you pass it true it will set the key state to on regardless of the original state:

Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
0
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication52;

import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 *
 * @author DSF Inc - Admin
 */
public class JavaApplication52 extends JFrame {

    JavaApplication52() {
        setLayout(null);

    
        JTextField t = new  JTextField();
        t.setBounds(0,0,300,20);
        add(t);
    
        t.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

                if (isOn == true) {
                    System.err.println("ON");
                } else {
                    System.err.println("OFF");
                }
            }
        });

        setSize(300, 400);
        setVisible(true);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JavaApplication52 fr = new JavaApplication52();
    }
}
0
Toolkit toolkit = Toolkit.getDefaultToolkit();

robik.keyPress(KeyEvent.VK_SPACE); // PRESS ANY KEY // Any key needs to be pressed
Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);
robik.keyRelease(KeyEvent.VK_SPACE);
cyberbrain
  • 3,433
  • 1
  • 12
  • 22
Erd
  • 1
  • 1