This discussion is archived
5 Replies Latest reply: Apr 9, 2008 1:45 PM by 807601 RSS

How Can I get character key code?

807601 Newbie
Currently Being Moderated
Hello,

I`m trying to get an int array containing the key codes of a string, but it returns an array of zeros.
My code:
package pattern.robot;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Arrays;

import javax.swing.KeyStroke;

public class MyRobot extends Robot {

    public MyRobot() throws AWTException {
        super();
    }

    public static int[] parse(String s1) {
        int[] result = new int[s1.length()];
        for(int i = 0; i < s1.length(); i++) {
            KeyStroke keyStroke = KeyStroke.getKeyStroke(s1.charAt(i));
            result[i] = keyStroke.getKeyCode();
        }
System.out.println(Arrays.toString(result));
        return result;
    }

    public static void main(String[] args) {
        try {
            MyRobot.parse("MyRobot key code test");
            //MyRobot bot = new MyRobot();
            //bot.startApp();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
It prints:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Am I doing something wrong? How to get the key codes?

Thanks for any help/directions.

Att.

UB.
  • 1. Re: How Can I get character key code?
    796365 Newbie
    Currently Being Moderated
    Why are you using Robot? It appears that all you are doing is getting the characters of a string. Try this
    import java.util.Arrays;
    
    public class Tester
    {
        public static void main(String[] args)
        {
            String st = "this is a string";
            int[] chrs = new int[st.length()];
            for (int n = 0; n < st.length(); n++)
            {
                chrs[n] = st.charAt(n);
            }
            System.out.println(Arrays.toString(chrs));
        }
    }
  • 2. Re: How Can I get character key code?
    807601 Newbie
    Currently Being Moderated
    No, no way.

    I didn't post the code for the robot actions, I just posted the code fraction where I am testing how to get the keyCodes of a string. charCode will not work, I need the values of the VK_* constants.
    I didn't posted all the code because it could polute the post with too many thinks that where not causing any problem.
  • 3. Re: How Can I get character key code?
    794342 Newbie
    Currently Being Moderated
    if you read the apidocs for getKeyStroke
    "Returns a shared instance of a KeyStroke that represents a KEY_TYPED event for the specified character."
    and then go to keyTyped, you'll find
    keyTyped + getKeyCode ALWAYS returns 0

    all you need to do is convert the string to upper case, and you'll get your values
  • 4. Re: How Can I get character key code?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    I need the values of the VK_* constants
    Available here

    Like Michael already said, for alpha characters the key code is equal to the char value of the uppercase letter (its ASCII code). For numeric characters, the key code is equal to the ASCII code. For special characters, refer to the link provided.

    db
  • 5. Re: How Can I get character key code?
    807601 Newbie
    Currently Being Moderated
    ChuckBing wrote:
    Why are you using Robot?
    Or why are you subclassing Robot? What Robot methods are you overriding?