i created small project test key events. when press keys, isn't behaving want. need key events calculator project. created calculator project , aside mouse clicks, want add feature numbers or operators can typed keyboard. can check , make more functional?
public class fxmldocumentcontroller implements initializable{ @fxml private label label; @fxml private button backspace; @fxml private button spacebar; @fxml private button enter; @fxml void typedbs(keyevent event) { if (event.getcode() == keycode.back_space) { label.settext(event.gettext() + " typed."); } } @fxml void typede(keyevent event) { if (event.getcode()==keycode.enter) { label.settext(event.gettext() + " typed"); } } @fxml void typedsb(keyevent event) { if (event.getcode()==keycode.space) { label.settext(event.gettext()+" typed"); } } @fxml void pressbackspace(keyevent event) { if (event.getcode() == keycode.back_space) { label.settext("you pressed space key!"); } } @fxml void clickbackspace(actionevent event) { label.settext("you clicked space key!"); } @fxml void clickenter(actionevent event) { label.settext("you clicked enter key!"); } @fxml void clickspacebar(actionevent event) { label.settext("you clicked spacebar key!"); } @fxml void pressenter(keyevent event) { if (event.getcode() == keycode.enter) { label.settext("you pressed enter key!"); } } @fxml void pressspacebar(keyevent event) { if (event.getcode() == keycode.space) { label.settext("you pressed spacebar key!"); } } @override public void initialize(url url, resourcebundle rb) { // todo }
}
if @ fxml file can see root node(the anchorpane) has onkeypressed , onkeyreleased.
<anchorpane id="anchorpane" onkeypressed="#handleonkeypressed" onkeyreleased="#handleonkeyreleased" prefheight="650.0" prefwidth="855.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="atmfx.fxmldocumentcontroller"> <children> <borderpane layoutx="263.0" layouty="94.0" prefheight="200.0" prefwidth="200.0" anchorpane.bottomanchor="0.0" anchorpane.leftanchor="0.0" anchorpane.rightanchor="0.0" anchorpane.topanchor="0.0"> <left> <anchorpane fx:id="apleftdisplay" prefheight="200.0" prefwidth="200.0" borderpane.alignment="center"> <children>
in controller method handles these 2 keyevents
@fxml private void handleonkeyreleased(keyevent event) { system.out.println(); button tempbutton = buttons.get(event.gettext()); system.out.println("released key text: " + event.gettext()); system.out.println("released key code: " + event.getcode()); if (tempbutton != null) { tempbutton.disarm(); tempbutton.setstyle(""); } else if (event.getcode().equals(keycode.enter)) { tempbutton = buttons.get("enter"); tempbutton.disarm(); tempbutton.setstyle(""); } else if (event.getcode().equals(keycode.back_space)) { tempbutton = buttons.get("backspace"); tempbutton.disarm(); tempbutton.setstyle(""); } else if (event.getcode().equals(keycode.space)) { tempbutton = buttons.get("space"); tempbutton.disarm(); tempbutton.setstyle(""); } } @fxml private void handleonkeypressed(keyevent event) { button tempbutton = buttons.get(event.gettext()); system.out.println("pressed key text: " + event.gettext()); system.out.println("pressed key code: " + event.getcode()); if (tempbutton != null) { tempbutton.arm(); tempbutton.setstyle("-fx-background-color: blue"); } else if (event.getcode().equals(keycode.enter)) { tempbutton = buttons.get("enter"); tempbutton.arm(); tempbutton.setstyle("-fx-background-color: blue"); } else if (event.getcode().equals(keycode.back_space)) { tempbutton = buttons.get("backspace"); tempbutton.arm(); tempbutton.setstyle("-fx-background-color: blue"); } else if (event.getcode().equals(keycode.space)) { tempbutton = buttons.get("space"); tempbutton.arm(); tempbutton.setstyle("-fx-background-color: blue"); } }
in code added of buttons hasmap. way in keyevent handlers able retrieve button depending on keyboard key pressed.
map<string, button> buttons = new hashmap<>(); thebuttonsparentnode.getchildren().stream().filter((tempnode) -> (tempnode instanceof button)).map(( tempnode) -> (button) tempnode).foreachordered((tempbutton) -> { buttons.put(tempbutton.gettext().tolowercase(), tempbutton);//adding button text , button hashmap });
Comments
Post a Comment