java - How to use JavaFX KeyListeners? -


i have editable javafx combobox. user must able

  1. type alphabets ('a' 'z'), space , round braces ('(', ')') enter string
  2. press tab exit
  3. press enter exit

how filter out every other key, modifiers etc?

i have read , used event handlers key_pressed, key_released unable figure out straight-forward way achieve above. using mac os yosemite, java 8, latest version of javafx and
public static final eventtype<keyevent> key_typed not work @ all. below code attempt. variable typedtext stores desired values.

combobox.addeventhandler(keyevent.key_released, new eventhandler<keyevent>() {         private final string[] alloweditems = new string[]{"a","b","c","d","e","f",         "g","h","i","j","k","l","m","n","o","p","q","r",         "s","t","u","v","w","x","y","z"," ","(",")"};         private final list data = arrays.aslist(alloweditems);         private string tempinput;         public boolean containscaseinsensitive(string s, list<string> l){             (string string : l) {                 if (string.equalsignorecase(s)){                   return true;                 }             }             return false;         }         public void handle(keyevent event) {             boolean b;             b = event.isshiftdown();             if (b) {                if (event.gettext().equals("(")) {                     tempinput = "(";                 } else if (event.gettext().equals(")")){                     tempinput = ")";                 }             } else {                 tempinput = event.getcode().tostring().tolowercase();             }             system.out.println("tempinput:"+tempinput);             if (containscaseinsensitive(tempinput, data)) {             typedtext = tempinput;             system.out.println("typedtext:"+typedtext);             }          }     }); } 

you can editor, textfield in case, , add textformatter restricts input.

tab works out of box, "enter" keypress different matter, request focus in example. you'd navigate next item in focus traversal list, there's no future-proof api yet in javafx.

import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.combobox; import javafx.scene.control.textfield; import javafx.scene.control.textformatter; import javafx.scene.input.keycode; import javafx.scene.input.keyevent; import javafx.scene.layout.hbox; import javafx.stage.stage;  public class comboboxsample extends application {      @override     public void start(stage stage) {          combobox<string> combobox = new combobox<>();         combobox.seteditable(true);         combobox.getitems().addall("a", "b", "c", "d", "e");         combobox.setvalue("a");           // restrict input         textfield textfield = combobox.geteditor();         textformatter<string> formatter = new textformatter<string>(change -> {             change.settext(change.gettext().replaceall("[^a-z ()]", ""));             return change;          });         textfield.settextformatter(formatter);          // dummy textfield jump on enter press         textfield dummytextfield = new textfield();          combobox.addeventfilter(keyevent.key_pressed, e -> {             if( e.getcode() == keycode.enter) {                 dummytextfield.requestfocus();                 e.consume();             }         });          hbox root = new hbox();          root.getchildren().addall(combobox, dummytextfield);          scene scene = new scene(root, 450, 250);         stage.setscene(scene);         stage.show();      }      public static void main(string[] args) {         launch(args);     }  } 

Comments