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的更多相关文章

  1. 触摸屏键盘插件Virtual Keyboard 该怎么用 Virtual Keyboard 入门指南

    最近公司有个项目,这个项目的显示器是触摸屏, 所以在一些需要简单输入的input需要加一个触摸屏的软键盘, 我在github上找了很多开源项目,最后选择了Virtual Keyboard, 以下是我自 ...

  2. 响应链和UIKit框架

    Event Delivery: The Responder Chain When you design your app, it’s likely that you want to respond t ...

  3. 树莓派3B 安装微雪LCD5inch显示器(包含软键盘)

    树莓派单独使用时,往往需要触摸屏和软键盘用以方便操作,微雪LCD显示器就能较好的实现这个功能, 正好实验室又买入了一个3B的板子和一个5inch的显示器,便对着官方的安装手册,亲自安装了一次. 一:材 ...

  4. Delphi QC 记录

    各网友提交的 QC: 官方网址 说明 备注 https://quality.embarcadero.com/browse/RSP-12985 iOS device cannot use indy id ...

  5. android常见问题(一)

    一:文本的颜色选择器: 在res目录下面创建color文件夹,在color文件夹下面创建font_style_colors.xml文件<?xml version="1.0" ...

  6. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  7. 如何在iPhone与iPad上开启firebug

    原文: MARTIN KOOL games - web - dad - sarien.net - q42 - livejs - handcraft How to use Firebug on your ...

  8. PowerShell定时记录操作系统行为

    作为系统管理员,有些时候是需要记录系统中的其他用户的一些操作行为的,例如:当系统管理员怀疑系统存在漏洞,且已经有被植入后门或者创建隐藏账户时,就需要对曾经登陆的用户进行监控,保存其打开或者操作过的文件 ...

  9. Android EditText 文本框实现搜索和清空效果

    前言 本文实现的效果:文本框输入为空时显示输入的图标:不为空时显示清空的图标,此时点击清空图标能清空文本框内输入文字. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnbl ...

随机推荐

  1. UI Framework-1: Aura Focus and Activation

    Focus and Activation Focus and Activation are closely related.   Definitions Focused window - this i ...

  2. 隐藏div,文本框角圆滑,消除外边框

    #div_1 /*将div设置完成,并且隐藏,当需要的时候对其属性值进行修改*/ { height: 36px; width: 1099px; background-color: #F0DFDF; m ...

  3. BZOJ 3626 LCA(离线+树链剖分+差分)

    显然,暴力求解的复杂度是无法承受的. 考虑这样的一种暴力,我们把 z 到根上的点全部打标记,对于 l 到 r 之间的点,向上搜索到第一个有标记的点求出它的深度统计答案.观察到,深度其实就是上面有几个已 ...

  4. yes---重复输出指定的字符串

    yes命令在命令行中输出指定的字符串,直到yes进程被杀死.不带任何参数输入yes命令默认的字符串就是y. 语法 yes(参数) 参数 字符串:指定要重复打印的字符串. 实例 [root@localh ...

  5. 【Fiddler】使用fiddler抓取指定浏览器的包

    参考资料:http://blog.csdn.net/sufubo/article/details/49331705 使用fiddler抓取不到浏览器的包时常用的解决办法: 1.必须先打开Fiddler ...

  6. 紫书 习题 10-14 UVa 10886(暴力+数据范围)

    开始的时候一看这题感觉很难,觉得肯定有什么很快的办法 不能暴力做(受了上一题10-13的影响) 然后一看那个函数感觉无从下手. 然后看了博客发现,原来这道题就是直接暴力-- 因为n的范围为10的7次方 ...

  7. 浅析[分块]qwq

    首先说明这篇博客写得奇差无比 让我们理清一下为什么要打分块,在大部分情况下,线段树啊,splay,treap,主席树什么的都要比分块的效率高得多,但是在出问题的时候如果你和这些数据结构只是混的脸熟的话 ...

  8. hbase源码系列(十二)Get、Scan在服务端是如何处理

    hbase源码系列(十二)Get.Scan在服务端是如何处理?   继上一篇讲了Put和Delete之后,这一篇我们讲Get和Scan, 因为我发现这两个操作几乎是一样的过程,就像之前的Put和Del ...

  9. ArcGIS api for javascript——地图配置-定制平移动画

    描述 本例展示了当用户点击平移按钮时如何定制地图的动画.panDuration和panRate是Dojo动画属性,可以分别确定动画的duration和帧刷新的rate.这些属性的单位都是毫秒,panD ...

  10. 关于Github Pages

    迁移Github Pages 我稍微有一点强迫症,实在是忍受不了整洁的界面有一些推广的广告.正所谓博客平台不重要,重要的是要有干货,CSDN首页满屏的广告也就忍受了,但是自己的文章的页面有广告看着实在 ...