0
  • I am trying to make a on-screen keyboard for Windows using Java.
  • The result I am looking for is somewhat similar to the tablet PC input panel (Touch Keyboard Mode) on Windows 7 except that mine will be Bengali.
  • But I cannot figure out how to send data from the program (when the user presses the on-screen buttons) such that it can serve as input for another program (say, a text editor).
  • This link seems useful but I cannot understand it.

How can I achieve this?
My current code is:

import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.border.Border;
import javax.swing.*;

public class Bengali extends JFrame {
    private JMenuBar menuBar;
    private JButton[][] buttons;
    private int i,j;
    private String hex;
    //Constructor 
    public Bengali(){

        this.setTitle("GUI_project");
        this.setSize(1600,140);
        //menu generate method
        generateMenu();
        this.setJMenuBar(menuBar);

        //pane with null layout
        JPanel contentPane = new JPanel(null);
        contentPane.setPreferredSize(new Dimension(1600,140));
        contentPane.setBackground(new Color(192,192,192));

        buttons=new JButton[4][32];
        for(i=0;i<4;i++)
        {
            for(j=0;j<32;j++)
            {
                buttons[i][j]=new JButton();
                buttons[i][j].setBounds(0+50*j,0+35*i,50,35);
                buttons[i][j].setBackground(new Color(214,217,223));
                buttons[i][j].setForeground(new Color(0,0,0));
                buttons[i][j].setEnabled(true);
                buttons[i][j].setFont(new Font("sansserif",0,12));
                hex = "u"+Integer.toHexString(i+2432);
                buttons[i][j].setText(hex);
                buttons[i][j].setVisible(true);

                //Set methods for mouse events
                //Call defined methods
                buttons[i][j].addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent evt) {
                            but(evt,i,j);
                        }
                    });
                //adding components to contentPane panel
                contentPane.add(buttons[i][j]);
            }
            //adding panel to JFrame and seting of window position and close operation
            this.add(contentPane);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLocationRelativeTo(null);
            this.pack();
            this.setVisible(true);
        }
    }
    //Method mouseClicked for buttons[i][j]
    private void but (MouseEvent evt,int i,int j) 
    {
        //TODO
    }

    //method for generate menu
    public void generateMenu()
    {
        menuBar = new JMenuBar();

        JMenu file = new JMenu("File");
        JMenu tools = new JMenu("Tools");
        JMenu help = new JMenu("Help");

        JMenuItem open = new JMenuItem("Open   ");
        JMenuItem save = new JMenuItem("Save   ");
        JMenuItem exit = new JMenuItem("Exit   ");
        JMenuItem preferences = new JMenuItem("Preferences   ");
        JMenuItem about = new JMenuItem("About   ");

        file.add(open);
        file.add(save);
        file.addSeparator();
        file.add(exit);
        tools.add(preferences);
        help.add(about);

        menuBar.add(file);
        menuBar.add(tools);
        menuBar.add(help);
    }

    public static void build(){
        System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new Bengali();
                }
            });
    }

}

The GUI is somewhat ready. Made using SimpleGUI extension for BlueJ.

Kitswas
  • 1,134
  • 1
  • 13
  • 30

2 Answers2

0

This can very easily be done with the Robot class. You have to simulate key presses (and not "write to any input stream" as this cannot work).

This post's accepted answer describes this very well: Press a key with Java

Twometer
  • 1,561
  • 2
  • 16
  • 24
  • You haven't quite understood my problem. This [link](https://stackoverflow.com/questions/782178/how-do-i-convert-a-string-to-an-inputstream-in-java?rq=1) might help to clarify. – Kitswas Feb 14 '18 at 16:47
  • @SwastikPal You said you wanted to create a on-screen keyboard, and i told you how to create one. You're right, I don't understand the problem. – Twometer Feb 14 '18 at 16:58
0

You can use PipedInputStream and PipedOutputStream classes. Whenever an event has occurred you have to write to the output stream and you should have a listener for input stream. You may have run these in two different threads. example snippet:

    PipedInputStream pis = new PipedInputStream();
    PipedOutputStream pos = new PipedOutputStream(pis);

You should start the listener thread first in case if you don't want to lose any data.

Ran
  • 333
  • 1
  • 4
  • 16