Solved

String or char to KeyEvent ??

Posted on 2006-11-08
5
1,201 Views
Last Modified: 2008-02-01
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
Comment
Question by:mr_mooray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
_iskywalker_ earned 250 total points
ID: 17896700
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
 

Author Comment

by:mr_mooray
ID: 17896744
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
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17896889
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question