Virtual / online key board is software program, which allows users to enter characters. This virtual / online key board can be operated by various input devices like Mouse, touch pad, external touch tablet etc. Virtual / Online key boards provides different input mechanisms to disability persons also.
Here I am going to share a java code, which was developed and used to create a Hindi virtual / online key board for application level.
You can download an application level Hindi Virtual / Online Key Board DOWNLOAD HERE.
The GUI of this Hindi online / virtual keyboard looks like as follows
Here you can Analyze the java code to create Hindi Online / Virtual Key board.
// File Name: HindiKeyBoard.java
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.*;
import javax.swing.*;
public class HindiKeyBoard {
public static void main(String args[])
{
Hindi_KeyBoard_Main J = new Hindi_KeyBoard_Main();
J.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
J.set_TextArea_prop();
J.set_menu();
J.Hindi_KeyBoard_Main_();
J.setSize(500, 600);
J.setVisible(true);
}
}
class Hindi_App_Level_Key_Board extends JDialog{
private static final long serialVersionUID = 1L;
protected JTextArea textArea;
HindiStateBuffer stateBuffer=new HindiStateBuffer();
String hindi[]={"अ","आ","इ","ई","उ","ऊ","ऋ","ए","ऐ","ओ","औ","अं","अः","क्","ख्","ग्","घ्","ङ्","च्","छ्","ज्","झ्","ञ्","ट्","ठ्","ड्","ढ्","ण्","त्","थ्","द्","ध्","न्","प्","फ्","ब्","भ्","म्","य्","र्","ल्","व्","श्","ष्","स्","ह्"};
String hindiMatraSet[]={"","ा","ि","ी","ु","ू","ृ","े","ै","ो","ौ","ं","ः"};
public Hindi_App_Level_Key_Board(Hindi_KeyBoard_Main aui){
super(aui,"हिन्दी",false);
JButton button[]=new JButton[63];
textArea=aui.textArea;
for(int i=0; i<63 br="" i=""> button[i]= new JButton("");
}
Container contPane = getContentPane();
JPanel panel =new JPanel();
JPanel flowPanel= new JPanel();
flowPanel.setLayout(new GridLayout(9,7));
for(int j=0; j<7 br="" j=""> button[j].setText(hindi[j]);
flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();;
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
for(int lo=0; lo<7 br="" lo=""> if(btn.getText().equals(hindi[lo])){
stateBuffer.precomposedString=stateBuffer.precomposedString+hindiMatraSet[lo];
}
}
}else{
stateBuffer.precomposedString=btn.getText();
}
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.reset();
}
});
}
int k=7;
for(int j=7; j<14 br="" j=""> button[j].setText(hindi[k++]);
flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();;
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
for(int lo=7; lo<14 br="" lo=""> if(btn.getText().equals(hindi[lo])){
stateBuffer.precomposedString=stateBuffer.precomposedString+hindiMatraSet[lo];
}
}
if(btn.getText().equals("्")){
stateBuffer.precomposedString=stateBuffer.precomposedString+"\u094d";
}
}else{
stateBuffer.precomposedString=btn.getText();
}
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.reset();
}
});
}
button[12].setText("्");
button[13].setText(hindiMatraSet[11]);
k=13;
for(int set=15; set<=19; set++){
button[set].setText(hindi[k++]);
}
for(int j=14; j<21 br="" j=""> flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false && !stateBuffer.precomposedString.equals("")){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.precomposedString=btn.getText();
}else{
stateBuffer.precomposedString=btn.getText();
}
stateBuffer.commit=false;
}
});
}
k=18;
for(int set=22; set<=26; set++){
button[set].setText(hindi[k++]);
}
for(int j=21; j<28 br="" j=""> flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false && !stateBuffer.precomposedString.equals("")){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.precomposedString=btn.getText();
}else{
stateBuffer.precomposedString=btn.getText();
}
stateBuffer.commit=false;
}
});
}
k=23;
for(int set=29; set<=33; set++){
button[set].setText(hindi[k++]);
}
for(int j=28; j<35 br="" j=""> flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false && !stateBuffer.precomposedString.equals("")){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.precomposedString=btn.getText();
}else{
stateBuffer.precomposedString=btn.getText();
}
stateBuffer.commit=false;
}
});
}
k=28;
for(int set=36; set<=40; set++){
button[set].setText(hindi[k++]);
}
for(int j=35; j<42 br="" j=""> flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false && !stateBuffer.precomposedString.equals("")){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.precomposedString=btn.getText();
}else{
stateBuffer.precomposedString=btn.getText();
}
stateBuffer.commit=false;
}
});
}
k=33;
for(int set=43; set<=47; set++){
button[set].setText(hindi[k++]);
}
for(int j=42; j<49 br="" j=""> flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false && !stateBuffer.precomposedString.equals("")){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.precomposedString=btn.getText();
}else{
stateBuffer.precomposedString=btn.getText();
}
stateBuffer.commit=false;
}
});
}
k=38;
for(int j=49; j<56 br="" j=""> button[j].setText(hindi[k++]);
flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false && !stateBuffer.precomposedString.equals("")){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.precomposedString=btn.getText();
}else{
stateBuffer.precomposedString=btn.getText();
}
stateBuffer.commit=false;
}
});
}
button[56].setText(hindi[45]);
for(int j=56; j<57 br="" j=""> flowPanel.add(button[j]);
button[j].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
Object btnObj= e.getSource();
JButton btn=(JButton) btnObj;
if(stateBuffer.commit==false && !stateBuffer.precomposedString.equals("")){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.precomposedString=btn.getText();
}else{
stateBuffer.precomposedString=btn.getText();
}
stateBuffer.commit=false;
}
});
}
for(int j=57; j<63 br="" j=""> flowPanel.add(button[j]);
}
button[57].setText("space");
button[57].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
int caretposition= textArea.getCaretPosition();
String space="\u0020";
if(stateBuffer.commit==false){
stateBuffer.precomposedString=stateBuffer.precomposedString.substring(0, stateBuffer.precomposedString.length()-1);
textArea.select(caretposition, caretposition+stateBuffer.precomposedString.length());
textArea.replaceSelection(stateBuffer.precomposedString);
}
stateBuffer.precomposedString=space;
textArea.replaceSelection(stateBuffer.precomposedString);
stateBuffer.reset();
}
});
button[58].setText("clear");
button[58].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
if(stateBuffer.precomposedString.equals("\u0020")){
stateBuffer.commit=false;
}else{
stateBuffer.reset();
}
}
});
flowPanel.setVisible(true);
panel.add(flowPanel);
panel.setVisible(true);
contPane.setLayout(new BoxLayout(contPane,BoxLayout.Y_AXIS));
contPane.add(panel);
contPane.setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
pack();
this.setVisible(true);
}
}
class HindiStateBuffer{
public boolean commit =true;
public String precomposedString;
HindiStateBuffer(){
commit=true;
precomposedString="";
}
public void reset(){
commit=true;
precomposedString="";
}
}
class Hindi_KeyBoard_Main extends JFrame{
private static final long serialVersionUID = 1L;
protected JTextArea textArea = new JTextArea();
protected JScrollPane textAreaScrollPane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
protected int textAreaRows = 10;
protected JFileChooser fileChooser=new JFileChooser();
Hindi_KeyBoard_Main thisreference =this;
Hindi_App_Level_Key_Board halkb;
public void Hindi_KeyBoard_Main_()
{
halkb = new Hindi_App_Level_Key_Board(thisreference);
}
public void set_TextArea_prop()
{
Container contPane = getContentPane();
contPane.setLayout(new BorderLayout());
textArea.setRows(textAreaRows);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createLineBorder(Color.black));
textArea.setSize(500, 600);
Font font = new Font("TimesRoman",Font.BOLD,15);
textArea.setFont(font);
contPane.add(textAreaScrollPane, BorderLayout.WEST);
}
public void set_menu()
{
JMenuBar menu = new JMenuBar();
JMenu menuTool = new JMenu("File");
menuTool.setMnemonic('F');
JMenuItem menuToolOpenText = new JMenuItem("Open File...");
menuToolOpenText.setMnemonic('O');
JMenuItem menuToolSaveToText = new JMenuItem("Save as Text...");
menuToolSaveToText.setMnemonic('S');
JMenuItem menuToolExit = new JMenuItem("Exit...");
menuToolSaveToText.setMnemonic('E');
menuTool.add(menuToolOpenText);
menuTool.add(menuToolSaveToText);
menuTool.add(menuToolExit);
menu.add(menuTool);
setJMenuBar(menu);
menuToolSaveToText.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae){
int returnVal = fileChooser.showSaveDialog(thisreference);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
String taString=textArea.getText();
FileOutputStream fosr=new FileOutputStream(file);
fosr.write(taString.getBytes());
fosr.close();
System.out.println(taString+"");
} catch (IOException ioe) {
System.out.println("Some error occurred while creating " + file + " : " + ioe);
}
System.out.println("Saving: " + file.getName());
} else {
System.out.println("Save command cancelled by user.");
}
textArea.setCaretPosition(textArea.getDocument().getLength());
}
});
menuToolOpenText.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae){
int returnVal = fileChooser.showOpenDialog(thisreference);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
Reader r = new BufferedReader(new FileReader(file));
int ch;
String nav="";
while ((ch = r.read()) != -1){
char chr=(char)ch;
nav=nav+chr;
}
textArea.setText(nav);
r.close();
} catch (IOException ioe) {
System.out.println("Some error occurred while creating " + file + " : " + ioe);
}
}
textArea.setCaretPosition(textArea.getDocument().getLength());
}
});
menuToolExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae){
dispose();
System.exit(0);
}
});
}
} 63>57>56>49>42>35>28>21>14>14>7>7>63>
i m getting errors ...what to do
ReplyDeleteDownload from here http://sourceforge.net/projects/hindkeyboard/
DeleteDo not copy the code from blog post :-)
This comment has been removed by the author.
ReplyDelete