Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JosAH

Thread: keyListener problem is there like a VK_KeySet or something

  1. #1
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Exclamation keyListener problem is there like a VK_KeySet or something

    txtField is just a field....
    txtField.addKeyListener(new KeyListener()
    {
    public void keyTyped(KeyEvent e)
    {
    if (e.getKeyChar() == KeyEvent.VK_1|| e.getKeyChar() == KeyEvent.VK_2)
    {
    String me = txtField.getText();
    String me2 = me.substring(0, me.length()-1);
    txtField.setText(me2);
    }
    }
    public void keyReleased(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {}
    });

    my question is this is there a set i can use in my if statement instead of tiping the whole alphabet
    e.getKeyChar() == KeyEvent.VK_A || ... KeyEvent.VK_B || ...
    and so on to Z
    that i can compare something like this e.getKeyChar() == KeyEvent.VK_ALLALPHABET or something
    basically i want the listener to check if the input is a small letter and if its not a small letter it shud remove the last letter inputted in the txtField.
    then the substring doesnt work it doesent remove the unwanted char eg
    i tipe abcd then i tipe 1 instead of removing the 1 it changes the last tiped letter in this case d to 1
    thus the output i wanted shud be abcd when i tipe abcd1 but it outputs abc1

    can anyone solve these 2 problems please

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: keyListener problem is there like a VK_KeySet or something

    Please do not post multiple copies of the same question. I've deleted your duplicate threads.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: keyListener problem is there like a VK_KeySet or something

    Thanx i tried to remove the one but culdn't the reason for my second post was the 1st one had a spelling error in the title

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: keyListener problem is there like a VK_KeySet or something

    The values KeyEvent.VK_A ... KeyEvent.VK_Z are consecutive values (read the API documentation), so:

    Java Code:
    if (code >= KeyEvent.VK_A && code <= KeyEvent.VK_Z)
       // a letter was typed ...
    kind regards,

    Jos
    Shoss likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: keyListener problem is there like a VK_KeySet or something

    thanks alot that helped now i get this exception Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    if (e.getKeyChar() >= KeyEvent.VK_A && e.getKeyChar() <= KeyEvent.VK_Z)
    and
    if (e.getKeyCode() >= KeyEvent.VK_A && e.getKeyCode() <= KeyEvent.VK_Z)
    both throw this exception

  6. #6
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: keyListener problem is there like a VK_KeySet or something

    also can i say
    if (code <= KeyEvent.VK_A && code >= KeyEvent.VK_Z)
    // a letter was not typed ...

    Is my logic right here

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: keyListener problem is there like a VK_KeySet or something

    Quote Originally Posted by searcher View Post
    also can i say
    if (code <= KeyEvent.VK_A && code >= KeyEvent.VK_Z)
    // a letter was not typed ...

    Is my logic right here
    Nope, better make that:

    Java Code:
    if (!(code >= KeyEvent.VK_A && code <= KeyEvent.VK_Z))
    or, if you know your logic rules:

    Java Code:
    if (code < KeyEvent.VK_A || code > KeyEvent.VK_Z)
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: keyListener problem is there like a VK_KeySet or something

    thanks alot Jos for all the help i actually know the logic rules but its been such a long day of coding my brain is worn anyho still much apreciated

    kind regards,

    Searcher

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: keyListener problem is there like a VK_KeySet or something

    basically i want the listener to check if the input is a small letter and if its not a small letter it shud remove the last letter inputted in the txtField.
    That's a job for a DocumentFilter, not a KeyListener.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. A problem with KeyListener
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 07-22-2011, 02:29 AM
  2. Problem with Keylistener, some help pls
    By syon in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2011, 01:31 AM
  3. AWT KeyListener Problem
    By plm-pusik in forum New To Java
    Replies: 15
    Last Post: 11-10-2010, 03:38 PM
  4. KeyListener problem
    By siyi90 in forum AWT / Swing
    Replies: 7
    Last Post: 02-08-2010, 10:16 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
  •