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 ...
随机推荐
- Android chromium 2
Overview JNI (Java Native Interface) is the mechanism that enables Java code to call native function ...
- ES6学习笔记(十七)Class 的基本语法
1.简介 类的由来 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y = y; ...
- word/excel/ppt 2 PDF
PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...
- 紫书 习题 10-14 UVa 10886(暴力+数据范围)
开始的时候一看这题感觉很难,觉得肯定有什么很快的办法 不能暴力做(受了上一题10-13的影响) 然后一看那个函数感觉无从下手. 然后看了博客发现,原来这道题就是直接暴力-- 因为n的范围为10的7次方 ...
- 紫书 习题 10-10 UVa 1645(递推)
除了根节点以外,有n-1个节点,然后就看n-1的因数有那些,所有因数加起来(递推)就好了. #include<cstdio> #define REP(i, a, b) for(int i ...
- NYOJ 541 最强的战斗力
最强DE 战斗力 时间限制:1000 ms | 内存限制:65535 KB 难度: 描写叙述 春秋战国时期,赵国地大物博,资源很丰富.人民安居乐业.但很多国家对它虎视眈眈.准备联合起来对赵国发起一 ...
- android 2048游戏实现
android 的2048小游戏完整实现:GridLayout布局(android 4.0及以上). 曾经做过一个2048的算法题,学了几天android,认为能够实现个安卓版的.也就动手写了个. 包 ...
- cocos2d-x 3.0正式版 vs2013配置
cocos2d-x 3.0正式版与之前的版本号差异较大,曾经的教程非常多都不使用了. 1.从cocos2d-x官网http://www.cocos2d-x.org下载3.0版的压缩包,随便解压一个位置 ...
- 智课雅思短语---二、exert positive/ negative effects on…
智课雅思短语---二.exert positive/ negative effects on… 一.总结 一句话总结:对…产生有利/不利的影响 1.the advantages far outweig ...
- shrio int配置
之前章节我们已经接触过一些INI配置规则了,如果大家使用过如Spring之类的IoC/DI容器的话,Shiro提供的INI配置也是非常类似的,即可以理解为是一个IoC/DI容器,但是区别在于它从一个根 ...