Inputing accented characters into JEditorPane
How can I input accented characters? I'm using an american keyboard, default american locale, running windows XP. I've search around, the closest I found was how to do this in word '97, an acute accented e is done with:
Ctrl-' e
Is there some standard mode or behavior I can assign to the editor pane to allow this?
[338 byte] By [
erra] at [2007-11-27 3:17:39]

# 1
You might use a KeyListener:
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
public class KeyTest {
JTextPane pane;
KeyTest() {
pane = new JTextPane();
pane.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_A) {
try {
if (e.getModifiers() == KeyEvent.CTRL_MASK) {
int offset = pane.getCaretPosition();
pane.getDocument().insertString(offset, "?,
pane.getCharacterAttributes());
}
else if (e.getModifiers() == KeyEvent.ALT_MASK) {
int offset = pane.getCaretPosition();
pane.getDocument().insertString(offset, "?,
pane.getCharacterAttributes());
}
else if (e.getModifiers() == KeyEvent.ALT_MASK+KeyEvent.CTRL_MASK) {
int offset = pane.getCaretPosition();
pane.getDocument().insertString(offset, "?,
pane.getCharacterAttributes());
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
});
JFrame fr = new JFrame("KeyTest");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane sp = new JScrollPane(pane);
fr.getContentPane().add(sp);
fr.setSize(500, 500);
fr.setVisible(true);
}
public static void main(String[] args) {
new KeyTest();
}
}
# 3
err--
as far as just wanting to input stuff...you don't really need a framework. Most operating systems come with a "character map" utility to input characters not normally located on keyboards. At least recent ones like Windows XP/Vista, GNOME/KDE, and OS-X all have utilities to do that.
Inputting bizarre characters is more an operating system/keyboard issue than it is a program issue. Word provides that functionality because it was introduced before windows had an easy way of doing it. Really your program just needs to be able to deal with unicode characters that may be inserted by the OS into an editor properly (which is fine because many java functions are at least BMP unicode ready). If you want to add convenience buttons/shortcuts for special chars that are frequently used in your program, then you've got to do the extra work as opposed to the user unfortunately.
(I wrote a little science program utility that generally requires greek characters to be input...so I added a button panel with frequently used greek chars in biology and gave them mnemonics to accelerate input. That way people don't have to hunt for them in their charmaps themselves).