Cargando la página...

ayuda con keypressed de teclado numerico

Por titopez el 18 de noviembre de 2009 en los siguientes foros: Java

ayuda con keypressed de teclado numerico

Avatar de titopez
Estoy haciendo mi aplicacion de una calculadora en java, y no se como utilizarla desde el teclado númerico.Cuando utilizo el sgte código:
"tfPantalla.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int kc=e.getKeyCode();
String tecla=e.getKeyText(kc);
tfPantalla.setText(tecla);
}
});
"
me sale siempre:"NumPad-6", y yo quiero que me salga solo el numero, por favor si alguien me puede ayudar

Solución (respuesta) elegida por el iniciador del debate

Gastón - Publicado el 19 de noviembre de 2009


Avatar de Gastón
Hola Roberto.

Intenta con:

tfPantalla.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
tfPantalla.setText(e.getKeyChar());
}
});


getKeyChar

public char getKeyChar()

Returns the character associated with the key in this event. For example, the key-typed event for shift + "a" returns the value for "A".

Returns:
the Unicode character defined for this key event. If no valid Unicode character exists for this key event, keyChar is CHAR_UNDEFINED.


Saludos.

Gracias por responder

Respuestas

1
19
noviembre
2009
Avatar de Gastón

Gastón 0 puntos

Hola Roberto.

Intenta con:

tfPantalla.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
tfPantalla.setText(e.getKeyChar());
}
});


getKeyChar

public char getKeyChar()

Returns the character associated with the key in this event. For example, the key-typed event for shift + "a" returns the value for "A".

Returns:
the Unicode character defined for this key event. If no valid Unicode character exists for this key event, keyChar is CHAR_UNDEFINED.


Saludos.
2
20
noviembre
2009
Avatar de titopez

titopez (iniciador del tema) 0 puntos

Si, gracias me servió bastante.
Lo que hice fue esto:

int kc=e.getKeyCode();
String tecla=e.getKeyText(kc);
if(kc==e.VK_NUMPAD0){
...
}

lo compare con cada una de las constantes que representan a los numeros del teclado
numérico.
Gracias.