LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-21-2005, 01:49 PM   #1
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Rep: Reputation: 15
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?

Last edited by nyk; 01-21-2005 at 09:24 PM.
 
Old 01-22-2005, 09:49 AM   #2
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaa
Distribution: Slack, Gentoo, Custom
Posts: 162

Rep: Reputation: 30
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
 
Old 01-22-2005, 07:49 PM   #3
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
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
 
Old 01-23-2005, 04:24 AM   #4
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaa
Distribution: Slack, Gentoo, Custom
Posts: 162

Rep: Reputation: 30
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
 
Old 01-23-2005, 05:30 AM   #5
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
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
 
Old 01-24-2005, 07:15 PM   #6
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
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?
 
Old 01-25-2005, 01:39 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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.

Last edited by jlliagre; 01-25-2005 at 03:14 PM.
 
Old 01-25-2005, 01:03 PM   #8
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
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?

Last edited by nyk; 01-25-2005 at 01:04 PM.
 
Old 01-25-2005, 03:19 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Does it works with the standalone test program on FC / gentoo ?
Are you using the same JVMs on the three platforms ?
 
Old 01-26-2005, 04:05 AM   #10
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
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...
 
Old 01-26-2005, 05:22 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
I was referring to the standalone program send in this thread by darkroom.
Moreover, you can run an applet in standalone mode with appletviewer.
 
Old 01-26-2005, 06:18 AM   #12
darkRoom
Member
 
Registered: Mar 2004
Location: Valencia, espaa
Distribution: Slack, Gentoo, Custom
Posts: 162

Rep: Reputation: 30
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 ?
 
Old 02-08-2005, 09:07 AM   #13
nyk
Member
 
Registered: Jan 2004
Location: Berne, Switzerland
Distribution: FC4, Gentoo
Posts: 112

Original Poster
Rep: Reputation: 15
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....
 
Old 02-08-2005, 12:04 PM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,490

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 02:13 AM
need some help with a java applet gulo General 1 07-14-2005 02:52 PM
Java Applet ekl_n Programming 2 02-26-2005 11:48 AM
Java applet error: "Applet Failed" nro Programming 1 08-28-2004 05:52 PM
Java Applet paint() Jose Muiz Programming 0 10-26-2003 06:45 PM


All times are GMT -5. The time now is 01:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration