Solved

String or char to KeyEvent ??

Posted on 2006-11-08
5
1,206 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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

630 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