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(, );
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();
keyBackground.setArcWidth();
keyBackground.setArcHeight(); final Text keyLabel = new Text(keyCode.getName());
keyLabel.setFont(Font.font("Arial", FontWeight.BOLD, )); 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();
keyboardNode.setPadding(new Insets()); 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;
}
} }
//
javafx virtual keyboard的更多相关文章
- 触摸屏键盘插件Virtual Keyboard 该怎么用 Virtual Keyboard 入门指南
最近公司有个项目,这个项目的显示器是触摸屏, 所以在一些需要简单输入的input需要加一个触摸屏的软键盘, 我在github上找了很多开源项目,最后选择了Virtual Keyboard, 以下是我自 ...
- 响应链和UIKit框架
Event Delivery: The Responder Chain When you design your app, it’s likely that you want to respond t ...
- 树莓派3B 安装微雪LCD5inch显示器(包含软键盘)
树莓派单独使用时,往往需要触摸屏和软键盘用以方便操作,微雪LCD显示器就能较好的实现这个功能, 正好实验室又买入了一个3B的板子和一个5inch的显示器,便对着官方的安装手册,亲自安装了一次. 一:材 ...
- Delphi QC 记录
各网友提交的 QC: 官方网址 说明 备注 https://quality.embarcadero.com/browse/RSP-12985 iOS device cannot use indy id ...
- android常见问题(一)
一:文本的颜色选择器: 在res目录下面创建color文件夹,在color文件夹下面创建font_style_colors.xml文件<?xml version="1.0" ...
- 词频统计_输入到文件_update
/* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...
- 如何在iPhone与iPad上开启firebug
原文: MARTIN KOOL games - web - dad - sarien.net - q42 - livejs - handcraft How to use Firebug on your ...
- PowerShell定时记录操作系统行为
作为系统管理员,有些时候是需要记录系统中的其他用户的一些操作行为的,例如:当系统管理员怀疑系统存在漏洞,且已经有被植入后门或者创建隐藏账户时,就需要对曾经登陆的用户进行监控,保存其打开或者操作过的文件 ...
- Android EditText 文本框实现搜索和清空效果
前言 本文实现的效果:文本框输入为空时显示输入的图标:不为空时显示清空的图标,此时点击清空图标能清空文本框内输入文字. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnbl ...
随机推荐
- 搭建Lvs负载均衡群集
一.Lvs详解 lvs内核模型 1.模型分析 用户访问的数据可以进入调度器 匹配调度器分配的虚拟IP|IP+端口(路由走向) 根据调度器的算法确定匹配的服务器 2.调度条件:基于IP.基于端口.基于内 ...
- Mac系统下安装pip
Mac下 pip的安装 编译 Python3.7 终端输入: curl https://bootstrap.pypa.io/get-pip.py | python3 1 安装完成,检查版本信息 pip ...
- Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)
菜单的概念,现在已经很普及了.Windows系统.Mac.桌面版Linux.Java Swing等,都有可视化菜单.一.Android平台3种菜单 选项菜单(OptionMenu).上下文菜单(Co ...
- C语言操作数截断
//测试截断 #include <stdio.h> int main() { int a = 0x80000001; unsigned int b = 0x80000001; printf ...
- 在Mac OS X上安装使用lazarus 1.6.4
一直觉得delphi的OO做得比C++还完善, 但如今日落西真是让人感到唏嘘, 这并不意味着delphi比C++差, 它的创始人被微软挖墙脚后创造了C#系列开发工具, 设计理念大部分与Delphi相 ...
- linux关于用户密码家目录总结
创建用户及其家目录useradd -d /home/tomcat -m tomcat接着修改密码passwd tomcat usermod -s /sbin/nologin + 用户名 禁止登录ssh ...
- mybatis :实现mybatis分页
上一篇文章里已经讲到了mybatis与spring MVC的集成,并且做了一个列表展示,显示出所有article 列表,但没有用到分页,在实际的项目中,分页是肯定需要的.而且是物理分页,不是内存分页. ...
- c#(asp.net) 如何计算两个日期之间相隔天数
1.DateTime t1 = Convert.ToDateTime("2006-1-6"); DateTime t2 = Convert.ToDateTime("200 ...
- vue 指令的用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HP 1022N 网络打印机安装步骤
HP 1022N 网络打印机安装步骤