Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //With a few tests, it seems to work
- //Event handling may be messed up though
- class GrAccentsTextField extends TextField{
- private final static HashMap<String, String> accented = new HashMap<String, String>();
- private LinkedList<String> queue = new LinkedList<String>();
- private boolean prevCharIsAccent = false;
- static{
- accented.put("α", "ά");
- accented.put("ε", "έ");
- accented.put("η", "ή");
- accented.put("ι", "ί");
- accented.put("ο", "ό");
- accented.put("υ", "ύ");
- accented.put("ω", "ώ");
- accented.put("Α", "Ά");
- accented.put("Ε", "Έ");
- accented.put("Η", "Ή");
- accented.put("Ι", "Ί");
- accented.put("Ο", "Ό");
- accented.put("Υ", "Ύ");
- accented.put("Ω", "Ώ");
- }
- public GrAccentsTextField(){
- this.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
- @Override
- public void handle(KeyEvent event) {
- final String typedChar = event.getText();
- if (prevCharIsAccent && accented.containsKey(typedChar)) {
- queue.add(typedChar);
- } else {
- queue.clear();
- }
- prevCharIsAccent = !prevCharIsAccent && event.getCode() == KeyCode.UNDEFINED;
- if(prevCharIsAccent){
- queue.add(typedChar);
- }
- }
- });
- this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
- @Override
- public void handle(KeyEvent event) {
- if(!queue.isEmpty() && queue.peek().equals(event.getCharacter())){
- final String typedChar = queue.remove();
- if(accented.containsKey(typedChar)){
- Event.fireEvent(event.getTarget(), new KeyEvent(
- event.getSource(),
- event.getTarget(),
- event.getEventType(),
- accented.get(typedChar),
- accented.get(typedChar),
- event.getCode(),
- event.isShiftDown(),
- event.isControlDown(),
- event.isAltDown(),
- event.isMetaDown())
- );
- }
- event.consume();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement