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 ...
随机推荐
- RadioButton的drawableTop图片文字不居中
在安卓应用的开发中,一般普通应用用到最多的就是底部放一个RadioGroup实现切换的布局,今天在实现的时候,却出现了底部RadiButton的drawableTop图片及文字无法居中的情况,经过对比 ...
- MVC中几种常用的ActionResult
一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...
- token登录验证机制
一张图解释 token登录验证机制
- Varnish 问题点 与 技术方案 Varnish 优劣分析
A10 有没有能做热点统计 1 Varnish 分布式 HA (目前没有HA) 2 Varnish 热点监控 (建议热点需要外部插件统计,API的话目前并木有发现,但是他自带一个伪热 ...
- ArcGIS api for javascript——放大时切换图层
描述 本例展示了如何在地图里指出显示的缓存或切片的细节等级(LODs).当打开示例地图,可以看到一些来自ArcGIS Online ESRI_Imagery_World_2D图层的影像.这个应用程序配 ...
- poj2031-Building a Space Station(最小生成树,kruskal,prime)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5874 Accepte ...
- BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD
题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...
- POJ2369 Permutations【置换群】
题目链接: http://poj.org/problem?id=2369 题目大意: 给定一个序列.问最少须要多少次置换才干变为 1.2.-.N 的有序序列.比方说给 定5个数的序列 4 1 5 2 ...
- Android程序猿自己动手制作.9.png图片
1:怎样制作9.png图片素材: 打开SDK工具文件夹下: draw9patch.zip 解压执行draw9patch.bat.有的直接搜索会有:draw9patch.bat. 双击执行后,例如以下 ...
- sc命令以及InstallUtil安装service
1.安装 https://stackoverflow.com/questions/8164859/install-a-windows-service-using-a-windows-command-p ...