1

I'm in the process of developing a text editor that has a "macro system," where the user can reassign values to the keys on their keyboard -- so that when they press the letter a, it might print the letter "z" instead. (Really, it'll be used for math symbols, not other letters).

Can anyone get me started on the Java code to reassign a value to a key within a JTextPane?

If you need more details, let me know.

Thank you!


So far, this is what I have:

public void keyPressed(KeyEvent evt) {
//Called when a key is pressed.

//Intercept the key before the default value is printed.
//1. Suppress the original character. Do this in the KeyEvent object
//by setting the doit property to false in your listener if the key
//matches a macro.

    jTextPane1.addKeyListener(new KeyAdapter() {
public void keyPressed(keyEvent event) {
if (event.getKeyCode() == KeyEvent.VK_A) {
//Perform an action when A is pressed and there is a macro.
if(macroA == true)
{
    keyPressed.doit() = false;
}
}
}
else if (event.getKeyCode() == KeyEvent.VK_B) {
//Perform an action when B is pressed if there is a macro.
if(macroB == true)
{
    keyPressed.doit() = false;
}
}
}
});

I'm working on how to implement it with "creating" the macro, checking to see if a macro exists.

If you have any more advice, I would appreciate it.

Thank you.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Math Student
  • 41
  • 2
  • 6
  • 1
    KeyListeners are used on old AWT applications. Swing has much better support for this and you should not be using KeyListeners – camickr Jan 19 '11 at 04:02

2 Answers2

3

I haven't done any Swing development in a while, but I think you're looking for a KeyListener. There's an example here: http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Your back end would need to map the key inputs with your macros and intercept the key input and insert the macro instead.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
  • You will also need to suppress the original character. You do this in the KeyEvent object by setting the doit property to false in your listener if the key matches a macro. – Konstantin Komissarchik Jan 18 '11 at 23:32
  • Can anyone give me a code example of mapping key inputs? That seems to be where I'm stuck, so far. – Math Student Jan 18 '11 at 23:35
  • The tutorial linked to above has example code that you can try out via JNLP. It shows key presses, including modifiers (CTRL, etc). Is there anything more specific you're looking for? – Sam Martin Jan 18 '11 at 23:42
  • I'll check it out, and I'll post back. I might be wondering how to make the result (the reassignment) from one GUI class (a pop-up window) appear in another (the main window of the text editor where the actual JFrame is. – Math Student Jan 18 '11 at 23:43
  • @Konstantin, I agree with the suppression. I worked my answer poorly. – Babak Naffas Jan 18 '11 at 23:51
2

If you want to change the character displayed in the text pane then you have two options (that I can think of)

a) rewrite the code that displays the text in the text pane b) insert a different character into the Document so that character will get painted

Option two is the easier approach.

For simple one to one key mappings you can just use a DocumentFilter.

For more complex key mappings, like using Ctrl+1, to enter a special character you would then use KeyBindings.

The Swing tutorial has section on both of these approaches. See "Text Component Features" and "Using Key Bindings".

camickr
  • 321,443
  • 19
  • 166
  • 288
  • +1. In most complicated cases I would also override Document's methods like insertString()/remove() rather than using DocumentFilter but usualy the camickr's suggestions are enough. – StanislavL Jan 19 '11 at 06:36
  • @StanislavL, Why create a custom Document?. Then the code will only work for the text component you target. It is my understanding that all updates to the Document go throught the DocumentFilter if one exists. The reason I use a DocumentFilter is so that the same class can be used on a JTextField, JTextArea or JTextField. Although it is not a requirement of the question, this makes the solution more reusable. – camickr Jan 19 '11 at 06:43