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 5 of 5

Thread: disabling keys

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default disabling keys

    hen user press can key than the code below gets the key user just pressed. the problem is that i want to only limit user to press alphabet and numbers. so i want to disable weird char ex left, right, up, @,$,!,%..... as you can see this will take up alot of if statments. is there better way to do this?

    if(keys == KeyEvent.VK_SHIFT || keys == KeyEvent.VK_CAPS_LOCK || keys ==KeyEvent.VK_LEFT ||
       keys ==KeyEvent.VK_RIGHT || keys ==KeyEvent.VK_DOWN || keys ==KeyEvent.VK_UP   .....)  //more button to disable
    {
    }
    else{  //store the char in array			
    	username[z] = e.getKeyChar();
    	z++;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: disabling keys

    Use a Collection to hold the keys to be ignored and use one of the collection's methods to see if the key just pressed is in the collection.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: disabling keys

    Also posted at Disabling keys - Dev Shed

    Are the desired keys values contiguous Ie in a range? for example a-z and A-Z.
    Then a simple range test would select them.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: disabling keys

    Also posted at disabling keys - Java
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: disabling keys

    and.....

    come to think of it arent you also NormR?

    posting same answer at Disabling keys - Dev Shed

    also anser can be found at http://bytes.com/topic/java/answers/...ys#post3749288

Similar Threads

  1. Please help with Arrow Keys
    By rtucker1023 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2013, 04:58 PM
  2. [SOLVED] quick keys
    By Gerardgrundy in forum Java IDEs
    Replies: 7
    Last Post: November 5th, 2012, 03:59 PM
  3. Overwrite what Keys do.
    By Melawe in forum Java Theory & Questions
    Replies: 4
    Last Post: May 17th, 2012, 11:23 PM
  4. Disabling Check Boxes created by different method.
    By xdega in forum AWT / Java Swing
    Replies: 3
    Last Post: April 23rd, 2012, 11:06 AM
  5. Disabling undo button in Word.
    By Silversurfer21 in forum Java Theory & Questions
    Replies: 0
    Last Post: September 5th, 2011, 09:03 AM