Created
February 17, 2017 13:54
-
-
Save bugabinga/c9698469507c709d15825c932eceae4f to your computer and use it in GitHub Desktop.
Demonstration of an On-Screen-Keyboard with JavaFx. http://stackoverflow.com/questions/42286960/prevent-javafx-scene-from-gaining-focus-onscreen-keyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.isp.stackoverflow; | |
import static java.util.stream.IntStream.concat; | |
import static java.util.stream.IntStream.rangeClosed; | |
import static javafx.geometry.Orientation.HORIZONTAL; | |
import java.awt.AWTException; | |
import java.awt.Dimension; | |
import java.awt.Robot; | |
import javax.swing.JFrame; | |
import javax.swing.SwingUtilities; | |
import javafx.application.Platform; | |
import javafx.embed.swing.JFXPanel; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.layout.FlowPane; | |
/** | |
* @author okr | |
* @date 17.02.2017 | |
* | |
*/ | |
public class OskFx | |
{ | |
/** | |
* Number of rows of keyboard keys. | |
*/ | |
private static final int PREF_ROW_COUNT = 3; | |
/** | |
* Some height for the keyboard. Needs refac for hidpi. | |
*/ | |
private static final int OSK_HEIGHT = 300; | |
/** | |
* Spacing between keyboard keys. | |
*/ | |
private static final double PADDING = 16.0; | |
private static Robot MARVIN; | |
private static void initAndShowGUI() | |
{ | |
try | |
{ | |
MARVIN = new Robot(); | |
} | |
catch ( final AWTException __ ) | |
{ | |
// TODO(okr): Explain the why! | |
} | |
// This method is invoked on Swing thread | |
final JFrame frame = new JFrame( "FX" ); | |
frame.setUndecorated( true ); | |
frame.setFocusableWindowState( false ); | |
frame.setFocusable( false ); | |
frame.enableInputMethods( false ); | |
frame.setAlwaysOnTop( true ); | |
final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); | |
frame.setSize( (int) screenSize.getWidth(), OSK_HEIGHT ); | |
frame.setLocation( 0, (int) (screenSize.getHeight() - OSK_HEIGHT) ); | |
final JFXPanel fxPanel = new JFXPanel(); | |
frame.add( fxPanel ); | |
frame.setVisible( true ); | |
Platform.runLater( () -> fxPanel.setScene( initFxScene() ) ); | |
} | |
private static Scene initFxScene() | |
{ | |
final FlowPane root = new FlowPane( HORIZONTAL, PADDING / 2, PADDING / 2 ); | |
root.setPadding( new Insets( PADDING ) ); | |
final Scene scene = new Scene( root ); | |
//The constant values for characters from 0 to 9 and A to Z in KeyEvent are incidentally the ASCII values. | |
concat( rangeClosed( '0', '9' ), rangeClosed( 'A', 'Z' ) ) | |
.forEach( character -> | |
{ | |
//This cast is safe, as long we iterate over only ASCII values... | |
final Button keyboardButton = new Button( Character.toString( (char) character ) ); | |
keyboardButton.setOnMousePressed( __ -> MARVIN.keyPress( character ) ); | |
keyboardButton.setPrefWidth( OSK_HEIGHT / PREF_ROW_COUNT - PADDING ); | |
keyboardButton.setPrefHeight( OSK_HEIGHT / PREF_ROW_COUNT - PADDING ); | |
root.getChildren().add( keyboardButton ); | |
} ); | |
return scene; | |
} | |
/** | |
* @param args ignored. | |
*/ | |
public static void main( final String[] args ) | |
{ | |
SwingUtilities.invokeLater( () -> initAndShowGUI() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment