1 2 Previous Next 22 Replies Latest reply: May 8, 2013 12:28 PM by 1001738 RSS

    creating and firing keyevent from action event on textfield

    1001738
      My virtual keyboard have buttons to represent keys in keyboard. On button pressed action event is generated. To type some text in text field using virtual key board , changed all action events of each button pressed to key events and doing fireevent on textfield. But no text is seen in textfield. Not understanding what's happening with firevent. When printing keyevent code getting right code.

      public void handle(ActionEvent event) {
      textField.requestFocus();

      KeyEvent keyEvent = KeyEvent.impl_keyEvent(textField, KeyCode.valueOf(name).impl_getChar(), name, KeyCode.valueOf(name).impl_getCode(), false, false, false, false, new EventType<KeyEvent>());

      KeyCode code = keyEvent.getCode();
      System.out.println("code : " + code);

      textField.fireEvent(keyEvent);
      keyEvent.consume();

      }
        • 1. Re: creating and firing keyevent from action event on textfield
          KonradZuse
          KeyEvent keyEvent = KeyEvent.impl_keyEvent(textField, KeyCode.valueOf(name).impl_getChar(), name, KeyCode.valueOf(name).impl_getCode(), false, false, false, false, new EventType<KeyEvent>());

          What exactly is this...?
          • 2. Re: creating and firing keyevent from action event on textfield
            shakir.gusaroff
            import javafx.application.Application;
            import javafx.event.ActionEvent;
            import javafx.event.EventHandler;
            import javafx.scene.Scene;
            import javafx.scene.control.Button;
            import javafx.scene.control.ButtonBuilder;
            import javafx.scene.control.TextField;
            import javafx.scene.layout.HBox;
            import javafx.scene.layout.StackPane;
            import javafx.scene.layout.VBox;
            import javafx.stage.Stage;
            
            public class VirtualKeyB extends Application {
            
                @Override
                public void start(Stage primaryStage) {
                    final TextField tf = new TextField();
                    EventHandler<ActionEvent> eh = new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent event) {
                            tf.requestFocus();
                            tf.appendText(((Button) event.getSource()).getText());
                        }
                    };
                    Button btna = ButtonBuilder.create().text("a").onAction(eh).build();
                    Button btns = ButtonBuilder.create().text("s").onAction(eh).build();
                    Button btnd = ButtonBuilder.create().text("d").onAction(eh).build();
                    Button btnf = ButtonBuilder.create().text("f").onAction(eh).build();
                    Button btng = ButtonBuilder.create().text("g").onAction(eh).build();
                    Button btnh = ButtonBuilder.create().text("h").onAction(eh).build();
                    Button btnj = ButtonBuilder.create().text("j").onAction(eh).build();
            
            
                    HBox hb = new HBox(3);
                    hb.getChildren().addAll(btna, btns, btnd, btnf, btng, btnh, btnj);
            
                    VBox vb = new VBox(10);
                    vb.getChildren().addAll(tf, hb);
                    StackPane root = new StackPane();
                    root.getChildren().add(vb);
                    Scene scene = new Scene(root, 300, 250);
                    primaryStage.setScene(scene);
                    primaryStage.show();
                }
            
                public static void main(String[] args) {
                    launch(args);
                }
            }
            • 3. Re: creating and firing keyevent from action event on textfield
              1001738
              For letters and number keys we can append text but how to replicate the keyboard functionality for alt, ctrl and other modifier keys? There are also other keyboard functionality like ctrl + c, ctrl + v etc?
              • 4. Re: creating and firing keyevent from action event on textfield
                1001738
                KeyEvent keyEvent = KeyEvent.impl_keyEvent(textField, KeyCode.valueOf(name).impl_getChar(), name, KeyCode.valueOf(name).impl_getCode(), false, false, false, false, new EventType<KeyEvent>());

                Syntax :
                KeyEvent.impl_keyEvent(EventTarget target, java.lang.String character, java.lang.String text, int code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown, EventType<? extends KeyEvent> eventType)

                Ex :
                KeyEvent keyEvent = KetEvent.impl_keyEvent(textfield, "A", "A", 49, false, false, false, false, KeyEvent.KEY_PRESSED);
                • 5. Re: creating and firing keyevent from action event on textfield
                  jsmith
                  Use of impl_ methods is inadvisable:
                  http://fxexperience.com/2011/10/dont-use-impl/

                  Instead use public API for event manipulation added in Java8:
                  https://javafx-jira.kenai.com/browse/RT-9383 "Event constructors"
                  • 6. Re: creating and firing keyevent from action event on textfield
                    1001738
                    Trying to type on text field using shift and keycodes for @, $, ^, & through virtual key board but printing some undefined characters. ASCII values defined for these characters in Java FX are quite different from the ASCII table.
                    If any one has solution could you please post it.

                    Thanks
                    • 7. Re: creating and firing keyevent from action event on textfield
                      James_D
                      This works for me, using JavaFX 8
                      import javafx.application.Application;
                      import javafx.beans.binding.Bindings;
                      import javafx.beans.property.BooleanProperty;
                      import javafx.event.ActionEvent;
                      import javafx.event.EventHandler;
                      import javafx.scene.Node;
                      import javafx.scene.Scene;
                      import javafx.scene.control.Button;
                      import javafx.scene.control.TextField;
                      import javafx.scene.control.ToggleButton;
                      import javafx.scene.input.KeyCode;
                      import javafx.scene.input.KeyEvent;
                      import javafx.scene.layout.HBox;
                      import javafx.scene.layout.VBox;
                      import javafx.stage.Stage;
                      
                      public class VirtualKeyboardTest extends Application {
                      
                        @Override
                        public void start(Stage primaryStage) {
                          final ToggleButton shift = new ToggleButton("Shift");
                          final TextField textField = new TextField();
                          final HBox row1 = new HBox(5);
                          row1.getChildren().addAll(
                              createButton("1", "!", KeyCode.DIGIT1, shift.selectedProperty(),
                                  textField),
                              createButton("2", "@", KeyCode.DIGIT2, shift.selectedProperty(),
                                  textField),
                              createButton("3", "#", KeyCode.DIGIT3, shift.selectedProperty(),
                                  textField),
                              createButton("4", "$", KeyCode.DIGIT4, shift.selectedProperty(),
                                  textField),
                              createButton("5", "%", KeyCode.DIGIT5, shift.selectedProperty(),
                                  textField),
                              createButton("6", "^", KeyCode.DIGIT6, shift.selectedProperty(),
                                  textField),
                              createButton("7", "&", KeyCode.DIGIT7, shift.selectedProperty(),
                                  textField),
                              createButton("8", "*", KeyCode.DIGIT8, shift.selectedProperty(),
                                  textField),
                              createButton("9", "(", KeyCode.DIGIT9, shift.selectedProperty(),
                                  textField),
                              createButton("0", ")", KeyCode.DIGIT0, shift.selectedProperty(),
                                  textField));
                          final HBox row2 = new HBox(5);
                          row2.getChildren().addAll(
                              createButton("q", "Q", KeyCode.Q, shift.selectedProperty(), textField),
                              createButton("w", "W", KeyCode.W, shift.selectedProperty(), textField),
                              createButton("e", "E", KeyCode.E, shift.selectedProperty(), textField),
                              createButton("r", "R", KeyCode.R, shift.selectedProperty(), textField),
                              createButton("t", "T", KeyCode.T, shift.selectedProperty(), textField),
                              createButton("y", "Y", KeyCode.Y, shift.selectedProperty(), textField),
                              createButton("u", "U", KeyCode.U, shift.selectedProperty(), textField),
                              createButton("i", "I", KeyCode.I, shift.selectedProperty(), textField),
                              createButton("o", "O", KeyCode.O, shift.selectedProperty(), textField),
                              createButton("p", "P", KeyCode.P, shift.selectedProperty(), textField));
                          final HBox row3 = new HBox(5);
                          row3.getChildren().addAll(
                              createButton("a", "A", KeyCode.A, shift.selectedProperty(), textField),
                              createButton("s", "S", KeyCode.S, shift.selectedProperty(), textField),
                              createButton("d", "D", KeyCode.D, shift.selectedProperty(), textField),
                              createButton("f", "F", KeyCode.F, shift.selectedProperty(), textField),
                              createButton("g", "G", KeyCode.G, shift.selectedProperty(), textField),
                              createButton("h", "H", KeyCode.H, shift.selectedProperty(), textField),
                              createButton("j", "J", KeyCode.J, shift.selectedProperty(), textField),
                              createButton("k", "K", KeyCode.K, shift.selectedProperty(), textField),
                              createButton("l", "L", KeyCode.L, shift.selectedProperty(), textField));
                          final HBox row4 = new HBox(5);
                          row4.getChildren().addAll(shift,
                              createButton("z", "Z", KeyCode.Z, shift.selectedProperty(), textField),
                              createButton("x", "X", KeyCode.X, shift.selectedProperty(), textField),
                              createButton("c", "C", KeyCode.C, shift.selectedProperty(), textField),
                              createButton("v", "V", KeyCode.V, shift.selectedProperty(), textField),
                              createButton("b", "B", KeyCode.B, shift.selectedProperty(), textField),
                              createButton("n", "N", KeyCode.N, shift.selectedProperty(), textField),
                              createButton("m", "M", KeyCode.M, shift.selectedProperty(), textField));
                          VBox root = new VBox(5);
                          root.getChildren().addAll(textField, row1, row2, row3, row4);
                          Scene scene = new Scene(root);
                          primaryStage.setScene(scene);
                          primaryStage.show();
                        }
                      
                        private Button createButton(final String unshifted, final String shifted,
                            final KeyCode code, final BooleanProperty shift, final Node target) {
                          final Button button = new Button();
                          button.textProperty().bind(
                              Bindings.when(shift).then(shifted).otherwise(unshifted));
                          button.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                              KeyEvent keyEvent;
                              final String value;
                              if (shift.get()) {
                                value = shifted;
                              } else {
                                value = unshifted;
                              }
                              keyEvent = new KeyEvent(button, target, KeyEvent.KEY_TYPED, value,
                                  value, code, shift.get(), false, false, false);
                              target.fireEvent(keyEvent);
                            }
                          });
                          return button;
                        }
                      
                        public static void main(String[] args) {
                          launch(args);
                        }
                      }
                      • 8. Re: creating and firing keyevent from action event on textfield
                        James_D
                        [url https://github.com/james-d/Virtual-keyboard/tree/master/src]Here is a more complete solution.
                        • 9. Re: creating and firing keyevent from action event on textfield
                          1001738
                          Thank you it helped me a lot. But I have problem with space bar key event. Not seeing any white space between characters.
                          • 10. Re: creating and firing keyevent from action event on textfield
                            James_D
                            You're referring to the bigger example on github, right? It works fine on my system (JDK 8.0 b87, on Mac OSX, though the OS shouldn't make any difference).

                            I just pushed an update to that, but I'm pretty certain it worked before.
                            • 11. Re: creating and firing keyevent from action event on textfield
                              1001738
                              I am using jdk7 and this is how I am generating keyevent for space bar

                              Syntax : KeyEvent.impl_keyEvent(EventTarget target, java.lang.String character, java.lang.String text, int code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown, EventType<? extends KeyEvent> eventType)

                              KeyEvent keyEvent = KeyEvent.impl_keyEvent(null, KeyCode.SPACE.impl_getChar(), KeyCode.SPACE.impl_getChar(), KeyCode.SPACE.impl_getCode(), false, false, false, false, KeyEvent.KEY_PRESSED);
                              target.fireEvent(keyEvent);

                              When I debug I see keycode as 32 for the code I have written but I don't understand why I am not seeing space in between characters on text area(target).

                              Thanks,
                              • 12. Re: creating and firing keyevent from action event on textfield
                                James_D
                                I found that characters would only appear if I fired a KEY_TYPED event, not a KEY_PRESSED. In the solution I put on github I fire all three: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED (the latter only if the key combination represented an actual character). Try firing a KEY_TYPED event for your space bar, and see if that fixes it.
                                • 13. Re: creating and firing keyevent from action event on textfield
                                  1001738
                                  Yes key_typed worked. Thanks
                                  • 14. Re: creating and firing keyevent from action event on textfield
                                    1001738
                                    Is there any difference in event handling for text field and text area as I am using the same piece of code for backspace, enter, tab etc and passing target as a parameter.

                                    with the following code backspace is working fine on text area but not on text field. when debugged i don't see any difference.

                                    KeyEvent keyEvent = KeyEvent.impl_keyEvent(input, KeyCode.BACK_SPACE.impl_getChar(), KeyCode.BACK_SPACE.toString(), KeyCode.BACK_SPACE.impl_getCode(), false, false, false, false, KeyEvent.KEY_PRESSED);
                                    1 2 Previous Next