Results 1 to 4 of 4
  1. #1
    batabek is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default bug in caps lock detection code

    Hi all,

    I am trying to detect caps lock key's on/off state using Toolkit's getLockingKeyState() method. Whenever I call this in a key or focus listener's method such as in the following it does not work correctly. It always return true:

    Java Code:
    textField_1.addFocusListener(new FocusAdapter() {
    	@Override
    	public void focusGained(FocusEvent arg0) {
    			lblCapsLock.setVisible(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));
    	}
    });
    What I am trying to achieve is to notify the user via enabling a label when the key is on.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: bug in caps lock detection code

    Java Code:
       public static void main(String args[]) {
          boolean lock = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
          System.out.println(lock);
       }
    Running the above gives the correct result when run on Win 7 (via Eclipse).

    Stick some debugging in your focusGained() to print out the value of the lock.
    Please do not ask for code as refusal often offends.

  3. #3
    batabek is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: bug in caps lock detection code

    In Windows 7 & XP it is working perfectly, but our main development environment is Red Hat Enterprise Linux (RHEL 6.2). So this code is not working correctly on that. In debug mode in Eclipse, it is working correctly(since halting the program gives some timing to aquire caps lock's state) but in release mode it's not. I have found a workaround using Thread.sleep but it is not a good idea to use in our production code. So is there anyone who come across this problem in RHEL ?
    Last edited by batabek; 08-07-2012 at 08:19 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Code Detection
    By Sakura Lime in forum New To Java
    Replies: 24
    Last Post: 01-25-2012, 09:37 AM
  2. Changing USER input to caps when printing
    By jh7468 in forum New To Java
    Replies: 3
    Last Post: 01-16-2011, 05:07 AM
  3. Replies: 2
    Last Post: 12-08-2010, 04:34 PM
  4. lock desktop using java code
    By meghana in forum Forum Guides
    Replies: 3
    Last Post: 03-30-2010, 06:24 AM
  5. Force Caps in JTable...
    By markw8500 in forum Advanced Java
    Replies: 0
    Last Post: 07-09-2008, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •