Results 1 to 9 of 9
- 01-24-2012, 08:31 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
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
- 01-24-2012, 08:40 PM #2
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!
- 01-24-2012, 08:42 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
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
- 01-24-2012, 08:45 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:if (code >= KeyEvent.VK_A && code <= KeyEvent.VK_Z) // a letter was typed ...
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-24-2012, 08:51 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
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
- 01-24-2012, 08:54 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
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
- 01-24-2012, 09:14 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: keyListener problem is there like a VK_KeySet or something
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-24-2012, 09:18 PM #8
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
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
- 01-25-2012, 05:25 AM #9
Re: keyListener problem is there like a VK_KeySet or something
That's a job for a DocumentFilter, not a KeyListener.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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
A problem with KeyListener
By Reskaillev in forum New To JavaReplies: 4Last Post: 07-22-2011, 02:29 AM -
Problem with Keylistener, some help pls
By syon in forum AWT / SwingReplies: 1Last Post: 01-21-2011, 01:31 AM -
AWT KeyListener Problem
By plm-pusik in forum New To JavaReplies: 15Last Post: 11-10-2010, 03:38 PM -
KeyListener problem
By siyi90 in forum AWT / SwingReplies: 7Last Post: 02-08-2010, 10:16 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks