Two small questions: keypressed event and regexp

Discussion in 'Java' started by Claus K., May 6, 2006.

  1. Claus K.

    Claus K. Guest

    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.output.append("look" + "\n");

    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

    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
    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 =

    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

    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 K., May 6, 2006
    1. Advertisements

  2. hiwa

    hiwa Guest

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

    For regex, your should formalize the characteristics of the target
    If it's just "[1;37m" and only numbers are variable, then regex would
    hiwa, May 7, 2006
    1. Advertisements

  3. Claus K.

    Claus K. Guest

    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 =

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

    Thank you for helping me, I really appreciate that.

    Claus K., May 7, 2006
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    keyPressed event problem

    vertigo, Apr 25, 2004, in forum: Java
  2. William Zumwalt

    how to get KeyPressed event inside JWindow

    William Zumwalt, Dec 19, 2004, in forum: Java
    Michael Dunn
    Dec 21, 2004
  3. boki
  4. Boki
    Darryl Pierce
    Apr 16, 2005
  5. Joao Silva
    7stud --
    Aug 21, 2009