Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1214
  • Last Modified:

String or char to KeyEvent ??

Is there already a method that will convert a String or a char to a keystroke.

For example If I have the word "Hamburg" I could call a method String.toKeyEvent("Hamburg"), that would convert this to keystrokes.

At the moment I have written a large switch statement which containes all of the alphabet, but I was wondering whether there was a better method.

Thanks

0
mr_mooray
Asked:
mr_mooray
  • 2
1 Solution
 
_iskywalker_Commented:
You could use a hash method!
in perl for example you could open an array with:
mykeys{"HamBURG"}='k';
so
print mykeys{"HamBURG"}."\n";
will print
k
you can use also a function for it but i think java have it, which language you are using?
another way  to do this is making  2 dimension arrays:
mykeys[][]=
{{"k","HAMBURG"}};
so you must search through the second dimension for optaining the first.
A hash is surely in all common langugaes implementated search for it, it is the easist way.
0
 
mr_moorayAuthor Commented:
Im using java, sorry not to mention.

At the moment I have a large switch statement:

public void typeLetter(char c) {
            try {
                  Robot robot = new Robot();
                  robot.delay(10);
                  
                  switch (c) {
                  case 'a':
                        robot.keyPress(KeyEvent.VK_A);
                        robot.keyRelease(KeyEvent.VK_A);
                        break;
                  case 'b':
                        robot.keyPress(KeyEvent.VK_B);
                        robot.keyRelease(KeyEvent.VK_B);
                        break;
                  case 'c':
                        robot.keyPress(KeyEvent.VK_C);
                        robot.keyRelease(KeyEvent.VK_C);
                        break;
                  case 'd':
                        robot.keyPress(KeyEvent.VK_D);
                        robot.keyRelease(KeyEvent.VK_D);
                        break;
                  case 'e':
                        robot.keyPress(KeyEvent.VK_E);
                        robot.keyRelease(KeyEvent.VK_E);
                        break;
                  case 'f':
                        robot.keyPress(KeyEvent.VK_F);
                        robot.keyRelease(KeyEvent.VK_F);
                        break;

...

Im not sure what you mean by hashing or how I could this.
0
 
_iskywalker_Commented:
here some tutorial, the point is they will be faster than every time making a switch (i hope they are faster, since a switch is already pretty primitive ).

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter10/hashtable.html
http://www.roseindia.net/javatutorials/javahashmap.shtml

search in google hashmap tutorials, there is a lot.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now