2

so, i'm i writting a program in java, and i need to similate the 'ת' key pressing, i changing the keyboard input language to hebrew and when i trying to simulate the 'ת' key pressing the written character is ',' (sometimes), why? and how i fixing it to write the 'ת' character?

The code snippet:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Test extends JFrame{
    private static Robot robot;



    public static void main(String[] args) {
        try {
            robot = new Robot();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }

        JFrame win = new JFrame();
        win.setSize(200,100);
        JPanel panel = new JPanel();
        JButton button = new JButton("simulate");
        final JTextField textField = new JTextField();
        textField.setPreferredSize(new Dimension(100, 30));
        panel.add(textField);
        panel.add(button);
        win.add(panel);
        win.setVisible(true);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.requestFocus();
                robot.keyPress(0X2C);
                robot.keyRelease(0X2C);
            }
        });


    }
}

I have tried also to simulate the key code: KeyEvent.COMMA (also, dosen't work like i need..)

My operation system: Windows 10.

Please help.

Thank you.

A. Ab
  • 35
  • 1
  • 5
  • If you look into an [ASCII Table](http://www.asciichars.com/_site_media/ascii/ascii-chars-landscape.jpg) you will see that ',' has the value 0x2C, therefore this will be printed. – The_Programmer Dec 31 '17 at 14:56
  • O.k, so how do i simulating the 'ת' key pressing so that the 'ת' character will be inserted? – A. Ab Dec 31 '17 at 15:08

1 Answers1

0

I found this question and answer and there seems to be some solution in the top answer...

How to make the Java.awt.Robot type unicode characters? (Is it possible?)

Please try this (code available from https://github.com/johanwitters/stackoverflow.swing-hebrewkey)

package com.johanw.stackoverflow.hebrewkey;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class Test extends JFrame{
    private static Robot robot;

    public static void main(String[] args) {
        try {
            robot = new Robot();
        } catch (AWTException e1) {
            e1.printStackTrace();
        }

        JFrame win = new JFrame();
        win.setSize(200,100);
        JPanel panel = new JPanel();
        JButton button = new JButton("simulate");
        final JTextField textField = new JTextField();
        textField.setPreferredSize(new Dimension(100, 30));
        panel.add(textField);
        panel.add(button);
        win.add(panel);
        win.setVisible(true);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.requestFocus();
//                pressUnicode(robot,0X5D4);
                pressUnicode(robot,0X2C);
/*
                robot.keyRelease(0X2C);
                robot.keyRelease(0X2C);
*/
            }
        });


    }

    public static void pressUnicode(Robot r, int key_code)
    {
        r.keyPress(KeyEvent.VK_ALT);

        for(int i = 3; i >= 0; --i)
        {
            // extracts a single decade of the key-code and adds
            // an offset to get the required VK_NUMPAD key-code
            int numpad_kc = key_code / (int) (Math.pow(10, i)) % 10 + KeyEvent.VK_NUMPAD0;

            r.keyPress(numpad_kc);
            r.keyRelease(numpad_kc);
        }

        r.keyRelease(KeyEvent.VK_ALT);
    }
}
Johan Witters
  • 1,529
  • 11
  • 23
  • It's work great, inside and outside the window of the java program (that what i need - outside), actually, i can to type In this way a lot of characters. By the way, the key code of the 'ת' character (according to the program you have linked to in your reply) is 250 (decimal). thank you very much! – A. Ab Dec 31 '17 at 18:09