代码之家  ›  专栏  ›  技术社区  ›  A. Ab

java-当我试图模拟×'键按下时,为什么不输入字母“×”(希伯来语)而输入字符?

  •  2
  • A. Ab  · 技术社区  · 7 年前

    所以,我在用java编写一个程序,我需要模拟按下“×”键,我将键盘输入语言改为希伯来语,当我试图模拟按下“×”键时,写的字符是“,”(有时),为什么?我如何修改它来写“×”字符?

    代码段:

    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);
                }
            });
    
    
        }
    }
    

    我还尝试模拟关键代码: KeyEvent.COMMA (而且,没有像我需要的那样工作……)

    我的操作系统:Windows 10。

    请帮忙。

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Johan Witters    7 年前

    我找到了这个问题和答案,在最上面的答案中似乎有一些解决方案。。。

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

    请尝试此(代码可从 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);
        }
    }
    
    推荐文章