Two small questions: keypressed event and regexp

C

Claus K.

Hello everyone,

just a small thing which has been bugging me for some time now.

I have created a small GUI which serves as a Mudclient (works alright),
but there is a problem with this:

#code snippet:
public void keyPressed(KeyEvent arg0)
{
....
else if (arg0.getKeyCode() == KeyEvent.VK_NUMPAD1)
{
appletwindow.out.println("look");
appletwindow.output.append("look" + "\n");
appletwindow.addHistoryIndex("look");
appletwindow.input.setText("look");
}
....

out is an Stream sending data to the server,
output is a textarea containing what is sent to and from the server,
history is like a unix shell command history, i.e. with arrow-up and
arrow-down keys, you can navigate between your last commands which will
appear in input, which is a textfield.

Now, what is happening if you press numpad-1 is that the command "look"
is sent over the stream, the command appears in the output textarea,
also in the history, but for the input-textfield, there is the number
"1" printed with the string "look" printed afterwards, of which I only
want the string "look" to appear, not the actual number which has been
pressed.

How can I manage to do that?

My second question is concerning regexps:

I have this coming over the stream:



and I have to filter it out, but all my expressions dont seem to catch
it.
I have tried to go for (for example)

Pattern color_pattern2 = Pattern.compile("\\[[0-9]+.[0-9]{2}[m]");

or a variation of

Pattern color_pattern =
Pattern.compile("\u001B"+"\\u005B"+"[0-9]+?"+"[m]");

which filters out most of those weird color codes (which they are), but
they fail to catch what I have described above. I think that is due to
the comma in there, but however I change the above, I just dont catch
it.

Easiest Solution would be to expand color_pattern I think, but Java
regexp works so differently from what I know from sed or perl, that I
am very frustrated by now.

Could someone please shed some light on those two - as I perceive them
- minor issues?

I would be very grateful,

Claus
 
H

hiwa

For the text field, you could set it uneditable and/or unfocusable.

For regex, your should formalize the characteristics of the target
text.
If it's just "[1;37m" and only numbers are variable, then regex would
be:
"\\[\\d;\\d\\dm".
 
C

Claus K.

Setting the Textfield temporarily unfocusable or uneditable didnt work
either, the key was to use keyReleased instead of keyPressed. Did not
think of trying that before just now (a friend suggested that). Now,
the numbers appear but are replaced upon keyRelease with whatever I
want to have there. This is not ideal, but at least workable, though I
presume there is a way to pipe those numbers to /dev/null which I just
have not found mentioned anywhere yet.

This proved to be a rather intersting question, actually.

As for the regexp, the one you suggested works, but somehow, you cannot
have two regexp matchers active at the same time (or at least for the
same line), which I just did not know (nor remember being stated
somewhere), so I had to combine the two to:

Pattern color_pattern =
Pattern.compile("\u001B"+"\\u005B"+"[0-9]??"+".??"+"[0-9]+?"+"[m]");

I finally learned the difference between ?? and ? and ?+ now as well.

Thank you for helping me, I really appreciate that.

Claus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top