如何使java.awt.Robot中的类型UNI code字? (是否可以?) [英] How to make the Java.awt.Robot type unicode characters? (Is it possible?)

查看:163
本文介绍了如何使java.awt.Robot中的类型UNI code字? (是否可以?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用户提供的字符串可能包含单code字,我们希望机器人键入该字符串。

你如何将一个字符串转换键codeS机器人会用?

你怎么做,所以它也是Java版本独立?(1.3 - > 1.6)

我们已经工作了ASCII字符是

  //字符C = nextChar();
//字符c ='A'; //这个作品,所以是'A'
焦C ='A'; //这不,也不是'A'
机器人机器人=新的机器人();
关键的KeyStroke = KeyStroke.getKeyStroke(pressed+ Character.toUpperCase(C));
如果(NULL!=键){
  //应该只担心表壳标准字符
  如果(Character.isUpperCase(c))的
  {
    robot.key preSS(KeyEvent.VK_SHIFT);
  }  robot.key preSS(key.getKey code());
  robot.keyRelease(key.getKey code());  如果(Character.isUpperCase(c))的
  {
    robot.keyRelease(KeyEvent.VK_SHIFT);
  }
}


解决方案

根据javamonkey79的code我创建了下面的片段应该对所有的Uni code值工作...

 公共静态无效pressUni code(机器人R,诠释KEY_ code)
{
    r.key preSS(KeyEvent.VK_ALT);    的for(int i = 3; I> = 0; --i)
    {
        //提取键盘code单一十年,并增加了
        //偏移获得所需VK_NUMPAD键盘code
        INT numpad_kc = KEY_ code /(INT)(Math.pow(10,I))%10 + KeyEvent.VK_NUMPAD0;        r.key preSS(numpad_kc);
        r.keyRelease(numpad_kc);
    }    r.keyRelease(KeyEvent.VK_ALT);
}

这自动通过UNI code键盘code的每个十年去,它映射到相应的VK_NUMPAD当量和presses /释放相应的按键。

We have a user provided string that may contain unicode characters, and we want the robot to type that string.

How do you convert a string into keyCodes that the robot will use?
How do you do it so it is also java version independant (1.3 -> 1.6)?

What we have working for "ascii" chars is

//char c = nextChar();
//char c = 'a'; // this works, and so does 'A'
char c = 'á'; // this doesn't, and neither does 'Ă'
Robot robot = new Robot();
KeyStroke key = KeyStroke.getKeyStroke("pressed " + Character.toUpperCase(c) );
if( null != key ) {
  // should only have to worry about case with standard characters
  if (Character.isUpperCase(c))
  {
    robot.keyPress(KeyEvent.VK_SHIFT);
  }

  robot.keyPress(key.getKeyCode());
  robot.keyRelease(key.getKeyCode());

  if (Character.isUpperCase(c))
  {
    robot.keyRelease(KeyEvent.VK_SHIFT);
  }
}

解决方案

Based on javamonkey79's code I've created the following snippet which should work for all Unicode values...

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

This automatically goes through each decade of the unicode key-code, maps it to the corresponding VK_NUMPAD equivalent and presses/releases the keys accordingly.

这篇关于如何使java.awt.Robot中的类型UNI code字? (是否可以?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆