Two small questions: keypressed event and regexp
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:
[1;37m
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