Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to check (reliable) if caps-lock is on?

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    378
    Thanks
    0
    Thanked 64 Times in 54 Posts

    Default How to check (reliable) if caps-lock is on?

    Hi.

    I want to find out whether caps-lock is currently enabled.
    I already google searched and found this method:
    		try {
    			result = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
    		} catch (UnsupportedOperationException e) {
    			result = false;
    		}
    Unfortunately I also found out, that it doesnt always work on some platforms (*cough* windows *cough*). And, in fact, it does not work for me.
    Source: Bug ID: JDK-4414164 Toolkit.getLockingKeyState() does not return caps lock state

    So, I would like to ask if anybody knew a better way.

    I could poll the keyboard state and keep track of caps-lock on my own, but I would like not to do this. There must be a better way.

    Thank you very much.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: How to check (reliable) if caps-lock is on?

    Sounds like you might have to use a library that uses JNI to do the native checking. A quick google search of "java check caps lock" turned up this: https://jna.java.net/javadoc/platfor...oardUtils.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    378
    Thanks
    0
    Thanked 64 Times in 54 Posts

    Default Re: How to check (reliable) if caps-lock is on?

    With that library I would have to check manually, right? Poll the key state for the capslock-key periodically and use a boolean variable, correct?

    Strange. I would have bet Java would be able to do this.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: How to check (reliable) if caps-lock is on?

    The problem is a bug in the windows JNI layer. It doesn't really surprise me that this doesn't work, as it relies on the underlying OS. What is your end goal?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    378
    Thanks
    0
    Thanked 64 Times in 54 Posts

    Default Re: How to check (reliable) if caps-lock is on?

    So, how does swing do it? Or does this bug affect swing as well?

    I am just writing a little text component myself and would like to detect if capslock is enabled or not.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: How to check (reliable) if caps-lock is on?

    Swing doesn't really "do" it at all. The underlying OS sends different characters as input depending on whether caps lock is activated. Swing doesn't care whether caps lock is on, just what the letters are.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    378
    Thanks
    0
    Thanked 64 Times in 54 Posts

    Default Re: How to check (reliable) if caps-lock is on?

    I see. Thank you for the help.

Similar Threads

  1. Combination lock
    By Pinares in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 26th, 2013, 05:56 PM
  2. Combination Lock Class help
    By dx8292 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 4th, 2012, 10:25 AM
  3. Lock up code to the click of a button!!!
    By Allen Walker in forum Java Theory & Questions
    Replies: 2
    Last Post: July 1st, 2011, 09:12 AM
  4. How to Count the CAPS
    By Sean137 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 14th, 2010, 07:30 AM