JavaFX Virtual keyboard

Source: Internet
Author: User

 Public classEffecttest extends Application {@Override Public voidStart (final stage stage) {Final Keyboard Keyboard=NewKeyboard (NewKey (KEYCODE.W),NewKey (KEYCODE.S),NewKey (keycode.a),NewKey (KEYCODE.D)); Final scene Scene=NewScene (NewGroup (Keyboard.createnode ()));        Stage.setscene (Scene); Stage.settitle ("Keyboard Example");    Stage.show (); }     Private StaticFinalclassKey {Privatefinal KeyCode KeyCode; Privatefinal Booleanproperty Pressedproperty;  PublicKey (Final keycode keycode) { This. keycode =KeyCode;  This. Pressedproperty =NewSimplebooleanproperty ( This,"pressed"); }         Publickeycode getKeyCode () {returnKeyCode; }         PublicBoolean ispressed () {returnPressedproperty.Get(); }         Public voidsetpressed (Final boolean value) {Pressedproperty.Set(value); }         PublicNode CreateNode () {final Stackpane Keynode=NewStackpane (); Keynode.setfocustraversable (true);            Installeventhandler (Keynode); Final Rectangle Keybackground=NewRectangle ( -, -); Keybackground.fillproperty (). Bind (Bindings.when (Pressedproperty). Then (Colo                                               r.red). Otherwise (Bindings.when (Keynode.focusedproperty ())            . then (Color.lightgray). otherwise (color.white));            Keybackground.setstroke (Color.Black); Keybackground.setstrokewidth (2); Keybackground.setarcwidth ( A); Keybackground.setarcheight ( A); Final Text Keylabel=NewText (Keycode.getname ()); Keylabel.setfont (Font.font ("Arial", Fontweight.bold, -));            Keynode.getchildren (). AddAll (Keybackground, Keylabel); returnKeynode; }        Private voidInstalleventhandler (Final Node keynode) {//handler for Enter key Press/release events, other keys is//handled by the parent (keyboard) node handlerFinal eventhandler<keyevent> Keyeventhandler =NewEventhandler<keyevent>() {                         Public voidhandle (Final KeyEvent keyevent) {if(Keyevent.getcode () = =keycode.enter) {setpressed (Keyevent.geteventtype () ==keyevent.key_pressed);                            Keyevent.consume ();            }                        }                    };            Keynode.setonkeypressed (Keyeventhandler);        Keynode.setonkeyreleased (Keyeventhandler); }    }    Private StaticFinalclassKeyboard {Privatefinal key[] keys;  PublicKeyboard (final Key ... keys) { This. Keys =Keys.clone (); }         PublicNode CreateNode () {final HBox Keyboardnode=NewHBox (6); Keyboardnode.setpadding (NewInsets (6)); Final List<Node> Keyboardnodechildren =Keyboardnode.getchildren ();  for(Final Key Key:keys) {Keyboardnodechildren.add (Key.createnode ());            } installeventhandler (Keyboardnode); returnKeyboardnode; }        Private voidInstalleventhandler (Final Parent keyboardnode) {//handler for key pressed/released events not handled by//Key NodesFinal eventhandler<keyevent> Keyeventhandler =NewEventhandler<keyevent>() {                         Public voidhandle (Final KeyEvent keyevent) {Final key key=LookupKey (Keyevent.getcode ()); if(Key! =NULL) {key.setpressed (Keyevent.geteventtype () ==keyevent.key_pressed);                            Keyevent.consume ();            }                        }                    };            Keyboardnode.setonkeypressed (Keyeventhandler);            Keyboardnode.setonkeyreleased (Keyeventhandler); Keyboardnode.addeventhandler (keyevent.key_pressed,NewEventhandler<keyevent>() {                                              Public voidhandle (Final KeyEvent keyevent) {                                                         Handlefocustraversal (Keyboardnode,                                             KeyEvent);        }                                         }); }        PrivateKey LookupKey (final keycode keycode) { for(Final Key key:keys) {if(Key.getkeycode () = =keycode) {                    returnkey; }            }            return NULL; }        Private Static voidhandlefocustraversal (Final Parent traversalgroup, final keyevent Keye            Vent) {final Node nextfocusednode; Switch(Keyevent.getcode ()) { CaseLeft:nextfocusednode=Getpreviousnode (Traversalgroup, (Node) keyevent                    . Gettarget ());                    Keyevent.consume ();  Break;  CaseRight:nextfocusednode=Getnextnode (Traversalgroup, (Node) Keyevent.gettarg                    ET ());                    Keyevent.consume ();  Break; default:                    return; }            if(Nextfocusednode! =NULL) {nextfocusednode.requestfocus (); }        }        Private StaticNode Getnextnode (final parent parent, final node node) {Final Iter Ator<Node> Childiterator =parent.getchildrenunmodifiable (). iterator ();  while(Childiterator.hasnext ()) {if(Childiterator.next () = =node) {                    returnChilditerator.hasnext ()?Childiterator.next ():NULL; }            }            return NULL; }        Private StaticNode Getpreviousnode (final parent parent, final node node) {fi NAL Iterator<Node> Childiterator =parent.getchildrenunmodifiable (). iterator (); Node Lastnode=NULL;  while(Childiterator.hasnext ()) {final Node CurrentNode=Childiterator.next (); if(CurrentNode = =node) {                    returnLastnode; } lastnode=CurrentNode; }            return NULL; }    }   }//  

JavaFX Virtual keyboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.