Thread: Disabling keys

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Disabling keys


    when 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?


    Code:
    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
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Also posted at http://www.javaprogrammingforums.com/whats-wrong-my-code/29124-disabling-keys.html
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    Originally Posted by NormR
    Also posted at http://www.javaprogrammingforums.com/whats-wrong-my-code/29124-disabling-keys.html

    and.....

    also at http://bytes.com/topic/java/answers/949218-disabling-keys#post3749288


    and i got my answer no thanks to you.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you post the answer here so anyone else that is interested will have it?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2
    sure


    Code:
    if( (keys >= KeyEvent.VK_0 && keys <= KeyEvent.VK_9)
     || (keys >= KeyEvent.VK_A && keys <= KeyEvent.VK_Z))
    {
      // do stuff
    } 
    else 
    {
      // Tell the user he pressed a deactivated key. Or don't. Your choice.
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I guess you didn't understand post#3 at
    http://www.javaprogrammingforums.com/whats-wrong-my-code/29124-disabling-keys.html

IMN logo majestic logo threadwatch logo seochat tools logo