I'm writing a script for a code editor and I want dynamic commands.
So, if the user types "class" it will change the colour of "class".
How do I do this?
// This is the main focus part of the code.
textarea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
word += evt.getKeyChar();
if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
word = "";
line = "";
lineInMemory = line;
}
if(evt.getKeyCode() == KeyEvent.VK_SPACE) {
word = word.replaceAll("null","");
line += word;
word = "";
String text = textarea.getText();
String[] words = line.split(" ");
if(word.toLowerCase().equals("class")) {
// What the heck do I put here?!
}
}
}
});
I already have key listeners that read the keys, put them into words, and then the words are put into sentences. I would like it so that they type the keyword and it automatically changes the colour of the keyword while they are still typing, a bit like what Sublime Text does.