Author Topic: Problem when getting a char from the KeyMapper  (Read 3568 times)

Offline Melssj5

  • double
  • *****
  • Posts: 724
    • View Profile
Problem when getting a char from the KeyMapper
« on: September 29, 2008, 06:49:05 am »
Hi, I have almost finished the whole chat stuff on my game. I have 2 problems:

1.- WHen I press BackSpace once it seems to register twice or something becausse my event is fired two times. this is the code:

Code: [Select]

public void appendChatChar () {
   
    //System.out.println ("Haciendo el appendChatChar: "+mensajeChat);
   
        boolean estado=false;
       
    do {
            estadoTeclas=mapaTeclas.poll();
            if (estadoTeclas!=KeyState.NONE) {
            estado=estadoTeclas.getState ();
            if (estadoTeclas.getKeyCode()==KeyEvent.VK_ENTER) {
            //System.out.println ("enviando el mensaje");
            mensajeSalida=mensajeChat;
            chateando=!chateando;
            render.setEnChat(false);
            } else if (estadoTeclas.getKeyCode()==KeyEvent.VK_ESCAPE) {
            //System.out.println ("dejando de chatear");
            mensajeChat="";
            chateando=!chateando;
            render.setEnChat(false);
           
            } else if (estadoTeclas.getKeyCode()==KeyEvent.VK_BACK_SPACE || estadoTeclas.getKeyCode()==KeyEvent.VK_DELETE) {
            System.out.println ("BORRANDO UN CARACTER DEL CHAT");
            if (mensajeChat.length()>=1)mensajeChat=mensajeChat.substring(0, mensajeChat.length()-1);
            render.setMensajeChat(mensajeChat);
            } else {
            char C=estadoTeclas.getChar();
            int h=C;
            if (h!=0)mensajeChat=mensajeChat+C;
            render.setMensajeChat(mensajeChat);
           
            }
               
            }
        } while (estadoTeclas!=KeyState.NONE);
   
    }


The estadoTeclas.getKeyCode()==KeyEvent.VK_BACK_SPACE is always being fired two times, always. Anyway to fix it?



2.- When I get get the events for my game everything goes well, but when doing the chat stuff, for each key typed and appended to a String I am receiving this: For example I write HELLO and get H(blank)E(blank)L(blank)L(blank)O(blank)

char C=estadoTeclas.getChar();
int h=C;//used this to control it
if (h!=0)mensajeChat=mensajeChat+C;
render.setMensajeChat(mensajeChat);

I guess Its a bug on the getChar method.
Nada por ahora

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Problem when getting a char from the KeyMapper
« Reply #1 on: September 29, 2008, 08:21:10 am »
You have to checked for pressed/released. The way you are doing it, you are getting both events...one when the key is pressed and the other one when it is released.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Problem when getting a char from the KeyMapper
« Reply #2 on: September 29, 2008, 08:42:19 am »
Hehe, Egon beat me to it.  Yes, you need to check your "estado" varriable.  It will be true when the key goes down, then false when it comes back up.  When going down, that is probably the correct time to use the character.  Here is your example with the modifications:

Code: [Select]
public void appendChatChar()
{
    //System.out.println( "Haciendo el appendChatChar: " + mensajeChat );
    boolean estado = false;
    int codigo;

    do
    {
        estadoTeclas = mapaTeclas.poll();
        if( estadoTeclas != KeyState.NONE )
        {
            estado = estadoTeclas.getState();
            codigo = estadoTeclas.getKeyCode();
            if( estado )
            {
                if( codigo == KeyEvent.VK_ENTER )
                {
                    //System.out.println( "enviando el mensaje" );
                    mensajeSalida = mensajeChat;
                    chateando = !chateando;
                    render.setEnChat( false );
                }
                else if( codigo == KeyEvent.VK_ESCAPE )
                {
                    //System.out.println( "dejando de chatear" );
                    mensajeChat = "";
                    chateando =! chateando;
                    render.setEnChat( false );
                }
                else if( codigo == KeyEvent.VK_BACK_SPACE || codigo == KeyEvent.VK_DELETE )
                {
                    System.out.println( "BORRANDO UN CARACTER DEL CHAT" );

                    if( mensajeChat.length() >= 1 )
                        mensajeChat=mensajeChat.substring( 0, mensajeChat.length() - 1 );

                    render.setMensajeChat( mensajeChat );
                }
                else
                {
                    char C = estadoTeclas.getChar();
                    int h = C;

                    if( h != 0 )
                        mensajeChat = mensajeChat + C;

                    render.setMensajeChat( mensajeChat );
                }
            }
        }
    } while( estadoTeclas != KeyState.NONE );
}
« Last Edit: September 29, 2008, 09:08:40 am by paulscode »

Offline Melssj5

  • double
  • *****
  • Posts: 724
    • View Profile
Re: Problem when getting a char from the KeyMapper
« Reply #3 on: September 29, 2008, 03:43:12 pm »
mmmm ok Understood.
Nada por ahora