LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java applet doesn't real all keys (https://www.linuxquestions.org/questions/programming-9/java-applet-doesnt-real-all-keys-280580/)

nyk 01-21-2005 01:49 PM

java applet doesn't read all keys
 
I have a java applet that should read the keys the users presses in a website. I watch it in firefox on linux. It successfully ready all normal key and shows theirs ASCII vaule. But it can't dsiplay the special key like the "öäüöèéà" characters used in Europe and elsewhere (ISO-8859-1).
Here is the relevant code:
Code:

    public boolean keyDown(Event event, int j)
    {
            char keyPressed;
                keyPressed = (char)j;

Like this it assigns all keys except the special "umlaute" chars to the keyPressed variable.
But it seems that the special keys (öäüèé) don't activate this handler, because I watch the normal key's ASCII values, but nothing happens with the special key. So I guess they don't activate the keyDown... Is there an other method?

How can I make it work for the special keys?

darkRoom 01-22-2005 09:49 AM

Hi
Maybe i've misunderstood what you are saying, are you saying the accent keys do not trigger the keyDown event or that the accent keys trigger the method but don't generate an ascii value ?

I use a UK keybard so i can't test the code, but could you tell me what happens when you press an accent key ?

Code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class keys extends JFrame implements KeyListener {
   
JTextArea text = new JTextArea();
JScrollPane scroll = new JScrollPane(text);

    public keys() {
        super("Keys");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().add(scroll);
        setSize(400, 400);
        text.addKeyListener(this);
        setVisible(true);
    }
   
    public static void main(String args[]){ keys myKeys = new keys(); }
   
    private void write(String toWrite){
        text.append(toWrite + "\n");             
        text.setCaretPosition(text.getDocument().getLength());
          }
   
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
   
    public void keyTyped(KeyEvent e) {
    int id = e.getID();
    char c = e.getKeyChar();
    int keyCode = e.getKeyCode();
    String str = KeyEvent.getKeyText(keyCode);
    write("  Key detected.  ID = " + id + ": Char " + c + ": KeyCode " + keyCode + " " + str);
    }
   
}

Also take a look at this class:
http://java.sun.com/j2se/1.3/docs/ap...tml#getKeyCode()

laters

nyk 01-22-2005 07:49 PM

Thanks for your help!

The problem is that the accent key don't trigger the KeyDown event. I have no idea why and how to repair this. Is there a better version of KeyDown, that also reacts to accent keys?

I compiled your code with javac, but can't run it with java, it says:
Code:

nyk@homenyk nyk $ java keys.class
Exception in thread "main" java.lang.NoClassDefFoundError: keys/class


darkRoom 01-23-2005 04:24 AM

Hi
When you run the code, you don't need the .class part

Code:

java keys
I took a look at the key_event class and it seems the only support for european characters/accents is the Spanish inverted exclamation mark and accents used with dead keys. So if you are looking to listen for special characters ie 'ñ' you'll have to write those characters yourself (unless someone else has already done that). Anyway check this code out which shows how to detect use of dead keys

http://java.sun.com/docs/books/tutor...ylistener.html

regards

nyk 01-23-2005 05:30 AM

Thanx, I'll read this and try to make a key listener...

Now the codes for the accent chacter ö don't look good:
Code:

�  Key detected.  ID = 400: Char �: KeyCode 0 Unknown keyCode: 0x0

nyk 01-24-2005 07:15 PM

I don't think my knowledge of java is good enough to write a key listener that accepts the accent keys!

From what I red on the web I got the impression that this might even be an impossible problem... Could it be that Sun just didn't implement support for European accent keys in their jre port to linux? Because if it's possible to create such a keylistener for the accent keys I find it strange that I can't find any traces of code that does such thing. I only found key-listeners posted in discussions about special keys (not neccessarily European accents) and all of them don't recognite the accent keys, they react to it but just report a symbol like this [].
Does anyone know if this is possible to do and how or if it's not?

jlliagre 01-25-2005 01:39 AM

Quote:

From what I red on the web I got the impression that this might even be an impossible problem... Could it be that Sun just didn't implement support for European accent keys in their jre port to linux?
No kidding !!
Java supports Unicode, and with it very exotic asian character set's, so there's no problem with good old european ones.

Your problem is related to the environment you are running in, not Java.

Just set and export the LC_ALL variable to the locale you want to support, say ch_DE or ch_FR and your program will display correctly the accented and other special characters.

By default, your locale is "C", meaning only 7 bit ascii characters are recognized.

nyk 01-25-2005 01:03 PM

This would be very nice, thanks!

But I tried these two ways and both of them failed!
Code:

export LC_ALL=ch_DE
firefox &

or
Code:

LC_ALL=ch_DE firefox &
also tried it with LANG=ch_DE and with LC_ALL=ch_DE.UTF8 and the same for LANG.
But none of that works on gentoo or fedora code 3, you can't enter a accented key.

The strange thing I figured out today is, that it works on my SuSe9 PC at work! How can this be? I think saw something in env containing UTF, but now when I login by ssh I don't see anything that could be usefull anymore...
Do you have any idea what changes in env or configuration could result in this difference between the behaviour of SuSe and FC3/gentoo?

jlliagre 01-25-2005 03:19 PM

Does it works with the standalone test program on FC / gentoo ?
Are you using the same JVMs on the three platforms ?

nyk 01-26-2005 04:05 AM

Can't test it in standalone, it's an applet.

On the system that works, I use
/usr/java/j2re1.4.2_03/plugin/i386/ns610-gcc32/libjavaplugin_oji.so
as JVM.
And on the system where it doesn't work this one:
/opt/jre1.5.0_01/plugin/i386/ns7/libjavaplugin_oji.so

But it's both the Sun version, which should work...

jlliagre 01-26-2005 05:22 AM

I was referring to the standalone program send in this thread by darkroom.
Moreover, you can run an applet in standalone mode with appletviewer.

darkRoom 01-26-2005 06:18 AM

Ok so im confused. A keyListener can't be written for the accent keys, but when said keys are used for normal input the correct symbol is written to stout ?

And why not change the locale in the actual program ?

nyk 02-08-2005 09:07 AM

I don't know about a keylistener. It's not necessary, as the program works on SuSe, I would just like to know what could be the reason why it doesn't work in FC3 or gentoo....

jlliagre 02-08-2005 12:04 PM

Please test darkroom's standalone program as I already asked, or your applet with appletviewer, so we can have some clues about the issue.


All times are GMT -5. The time now is 08:16 PM.