Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Created February 17, 2017 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugabinga/c9698469507c709d15825c932eceae4f to your computer and use it in GitHub Desktop.
Save bugabinga/c9698469507c709d15825c932eceae4f to your computer and use it in GitHub Desktop.
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