javafx virtual keyboard

public class EffectTest extends Application {
@Override
    public void start(final Stage stage) {
        final Keyboard keyboard = new Keyboard(
                new Key(KeyCode.W),
                                               new Key(KeyCode.S),
                new Key(KeyCode.A),
                                               new Key(KeyCode.D)
                                               );

        final Scene scene = new Scene(new Group(keyboard.createNode()));
        stage.setScene(scene);
        stage.setTitle("Keyboard Example");
        stage.show();
    }

    private static final class Key {
        private final KeyCode keyCode;
        private final BooleanProperty pressedProperty;

        public Key(final KeyCode keyCode) {
            this.keyCode = keyCode;
            this.pressedProperty = new SimpleBooleanProperty(this, "pressed");
        }

        public KeyCode getKeyCode() {
            return keyCode;
        }

        public boolean isPressed() {
            return pressedProperty.get();
        }

        public void setPressed(final boolean value) {
            pressedProperty.set(value);
        }

        public Node createNode() {
            final StackPane keyNode = new StackPane();
            keyNode.setFocusTraversable(true);
            installEventHandler(keyNode);

            final Rectangle keyBackground = new Rectangle(50, 50);
            keyBackground.fillProperty().bind(
                    Bindings.when(pressedProperty)
                            .then(Color.RED)
                            .otherwise(Bindings.when(keyNode.focusedProperty())
                                               .then(Color.LIGHTGRAY)
                                               .otherwise(Color.WHITE)));
            keyBackground.setStroke(Color.BLACK);
            keyBackground.setStrokeWidth(2);
            keyBackground.setArcWidth(12);
            keyBackground.setArcHeight(12);

            final Text keyLabel = new Text(keyCode.getName());
            keyLabel.setFont(Font.font("Arial", FontWeight.BOLD, 20));

            keyNode.getChildren().addAll(keyBackground, keyLabel);

            return keyNode;
        }

        private void installEventHandler(final Node keyNode) {
            // handler for enter key press / release events, other keys are
            // handled by the parent (keyboard) node handler
            final EventHandler<KeyEvent> keyEventHandler =
                    new EventHandler<KeyEvent>() {
                        public void handle(final KeyEvent keyEvent) {
                            if (keyEvent.getCode() == KeyCode.ENTER) {
                                setPressed(keyEvent.getEventType()
                                               == KeyEvent.KEY_PRESSED);

                                keyEvent.consume();
                            }
                        }
                    };

            keyNode.setOnKeyPressed(keyEventHandler);
            keyNode.setOnKeyReleased(keyEventHandler);
        }
    }

    private static final class Keyboard {
        private final Key[] keys;

        public Keyboard(final Key... keys) {
            this.keys = keys.clone();
        }

        public Node createNode() {
            final HBox keyboardNode = new HBox(6);
            keyboardNode.setPadding(new Insets(6));

            final List<Node> keyboardNodeChildren = keyboardNode.getChildren();
            for (final Key key: keys) {
                keyboardNodeChildren.add(key.createNode());
            }

            installEventHandler(keyboardNode);
            return keyboardNode;
        }

        private void installEventHandler(final Parent keyboardNode) {
            // handler for key pressed / released events not handled by
            // key nodes
            final EventHandler<KeyEvent> keyEventHandler =
                    new EventHandler<KeyEvent>() {
                        public void handle(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,
                                         new EventHandler<KeyEvent>() {
                                             public void handle(
                                                     final KeyEvent keyEvent) {
                                                 handleFocusTraversal(
                                                         keyboardNode,
                                                         keyEvent);
                                             }
                                         });
        }

        private Key lookupKey(final KeyCode keyCode) {
            for (final Key key: keys) {
                if (key.getKeyCode() == keyCode) {
                    return key;
                }
            }
            return null;
        }

        private static void handleFocusTraversal(final Parent traversalGroup,
                                                 final KeyEvent keyEvent) {
            final Node nextFocusedNode;
            switch (keyEvent.getCode()) {
                case LEFT:
                    nextFocusedNode =
                            getPreviousNode(traversalGroup,
                                            (Node) keyEvent.getTarget());
                    keyEvent.consume();
                    break;

                case RIGHT:
                    nextFocusedNode =
                            getNextNode(traversalGroup,
                                        (Node) keyEvent.getTarget());
                    keyEvent.consume();
                    break;

                default:
                    return;
            }

            if (nextFocusedNode != null) {
                nextFocusedNode.requestFocus();
            }
        }

        private static Node getNextNode(final Parent parent,
                                        final Node node) {
            final Iterator<Node> childIterator =
                    parent.getChildrenUnmodifiable().iterator();

            while (childIterator.hasNext()) {
                if (childIterator.next() == node) {
                    return childIterator.hasNext() ? childIterator.next()
                                                   : null;
                }
            }

            return null;
        }

        private static Node getPreviousNode(final Parent parent,
                                            final Node node) {
            final Iterator<Node> childIterator =
                    parent.getChildrenUnmodifiable().iterator();
            Node lastNode = null;

            while (childIterator.hasNext()) {
                final Node currentNode = childIterator.next();
                if (currentNode == node) {
                    return lastNode;
                }

                lastNode = currentNode;
            }

            return null;
        }
    }

}
//  
时间: 2024-10-11 16:40:00

javafx virtual keyboard的相关文章

AutoIt Virtual Keyboard

#NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=1295599487_Gnome-Input-Keyboard-64.ico #AutoIt3Wrapper_OutFile=Virtual_Keyboard.exe #AutoIt3Wrapper_Compression=4 #AutoIt3Wrapper_Res_Description=Virtual Key

HoloLens开发手记 - 使用Windows设备控制台 Using Windows Device Portal

Windows设备控制台允许你通过Wi-Fi或USB来远程控制你的HoloLens设备.设备控制台是HoloLens上的一个Web Server,你可以通过PC的浏览器来连接到它.设备控制台包含了很多帮助你管理.调试和优化HoloLens设备的工具. 设置HoloLens以使用Windows设备控制台 Setting up HoloLens to use Windows Device Portal 打开HoloLens,并穿戴上 使用绽开手势打开开始菜单 选中设置应用,在你放置它以后会自动启动

MAC安裝《Genymotion Android模擬器》大玩Android APP (神魔之塔)

链接地址:http://www.minwt.com/mac/10083.html/comment-page-2 MAC» 智慧型裝罝» Android | 2014/02/12 Android是一個開放的平台,因此先前也分享了幾個Android的模擬器,但當初梅干使用Android模擬器,最主要的功能就是用來測試網頁,看網頁在Android手機上是否能正常運作,雖然說這些Android模擬器,也可透過Google Player安裝Android APP,在電腦中就可玩Android APP,但由

pyqt在控件上创建图片

# text=self.UserName.text()        # if  text.isEmpty():        #     imgpath=r'Image/lineclose.png'        #     self.ButtonShowKeyboard = QToolButton(self)        #     self.ButtonShowKeyboard.setCursor(Qt.PointingHandCursor)        #     self.Butt

debian内核代码执行流程(一)

本文根据debian开机信息来查看内核源代码. 系统使用<debian下配置dynamic printk以及重新编译内核>中内核源码来查看执行流程. 使用dmesg命令,得到下面的开机信息: [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Linux version 3.2.57 ([email protected]) (gcc versio

Qt5.3企业版和开源版功能区别

一.综述 Qt企业版(Qt Enterprise)主要包括: 更完整的Qt库 Qt Creator Enterprise IDE 商业授权 Digia公司的技术支持 Qt云服务 二.企业版的功能特性 参看官方文档. 2.1 Charts Charts是QT提供的图表模块.他提供了非常简便的APIs来绘制令人惊叹的Line, Spline,Area,Scatter,Pie,Donut,Bar,Polar和Box-and-Whiskers表. 支持Qt5/Qt4.8/Qt4.7. C++和Qt Qu

全面拥抱移动测试,Mobile JSON Wire Protocol Specification文档翻译

Selenium3已经宣布不支持移动化测试.对于老牌测试工具selenium来说这是以退为进,因为移动自动化测试工具的标准还在selenium团队手上. 本文轻度翻译了这个标准,看得懂的人不用翻译也能看懂,看不懂的人翻的天花乱坠也是一头雾水. 注意,这个规格是给工具的开发者定义的条条框框,对于使用者来说,只要知道哪些是必须工具必须支持的,且支持的细节是什么就可以了,其他可以不去深究. 这个标题就不翻译了 Mobile JSON Wire Protocol Specification 源地址 DR

Android自定义键盘之汉字键盘

实现软键盘主要用到了系统的两个类:Keyboard和KeyboardView. Keyboard类源码的介绍是: Listener for virtual keyboard events.即用于监听虚拟键盘. KeyboardView类源码的介绍是: A view that renders a virtual {@link Keyboard}. It handles rendering of keys and detecting key presses and touch movements.即

强烈推荐:Android史上最强大的自定义任务软件Tasker

强烈推荐:Android史上最强大的自定义任务软件Taskerhttp://bbs.mumayi.com/thread-28387-1-1.html(出处: 木蚂蚁手机乐园) Android上的Tasker绝对称得上是Android系统的神器之一,与Auto Memory Manager不同,Tasker不是加速型的软件,而是系统增强型的软件,由于有众多系统状态可控制,故使得Tasker一跃成为Android系统中最闪亮的明星.但Tasker也无疑是最难使用的软件,由于可以控制的地方太多,反而让