dcsimg
Generating KeyEvents for a given locale
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dimitri_Dering
Posted On:   Sunday, February 3, 2002 11:50 PM

Hello, i have the following problem: I must generate a keyboard-input to a native-application. This input must be correct for different locales installed on the client's computer (it's a global network). My code uses java.awt.Robot-methods (keyPress and keyRelease) which have following signatures: public void keyPress(int keycode) public void keyRelease(int keycode) Both methods take a keycode as parameter. My questio is, how to generate a keycode? I have tried the following code sample (copyied from http://forum.java.sun.com/thread.jsp?forum=256&thread=46611):    More>>

Hello,



i have the following problem:
I must generate a keyboard-input to a native-application.
This input must be correct for different locales installed
on the client's computer (it's a global network).



My code uses java.awt.Robot-methods (keyPress
and keyRelease) which have following signatures:


public void keyPress(int keycode)

public void keyRelease(int keycode)


Both methods take a keycode as parameter.
My questio is, how to generate a keycode?



I have tried the following code sample (copyied from http://forum.java.sun.com/thread.jsp?forum=256&thread=46611):




			
//------------------------//
// file RobotActions.java //
//------------------------//

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

public class RobotActions
{
private Robot robot;

public RobotActions() throws AWTException
{
robot = new Robot();
}

//Access to all robot methods by returning robot object
public Robot returnRobot()
{
return robot;
}


//Keyboard Methods

//Type out string
public synchronized void keyType(String text)
{
//Converts string to character array
char[] textArray = new char[text.length()];
textArray =text.toCharArray();

for(int i=0; i

if(Character.isUpperCase(textArray[i]))
{
//Types out character if uppercase letter
shift((int)textArray[i]);

}
else if(Character.isLowerCase(textArray[i]))
{
//Types out character if lowercase letter
robot.keyPress((int)Character.toUpperCase(textArray[i]));
}
else
{
//Switch for handling other characters
switch(textArray[i])
{
case '~': {
shift(KeyEvent.VK_BACK_QUOTE);
};
break;
case '`': {
robot.keyPress(KeyEvent.VK_BACK_QUOTE);
};
break;
case '!': {
shift(KeyEvent.VK_1);
};
break;
case '@': {
shift(KeyEvent.VK_2);
};
break;
case '#': {
shift(KeyEvent.VK_3);
};
break;
case '$': {
shift(KeyEvent.VK_4);
};
break;
case '%': {
shift(KeyEvent.VK_5);
};
break;
case '^': {
shift(KeyEvent.VK_6);
};
break;
case '&': {
shift(KeyEvent.VK_7);
};
break;
case '*': {
shift(KeyEvent.VK_8);
};
break;
case '(': {
shift(KeyEvent.VK_9);
};
break;
case ')': {
shift(KeyEvent.VK_0);
};
break;
case '_': {
shift(KeyEvent.VK_MINUS);
};
break;
case '+': {
shift(KeyEvent.VK_EQUALS);
};
break;
case '{': {
shift(KeyEvent.VK_OPEN_BRACKET);
};
break;
case '}': {
shift(KeyEvent.VK_CLOSE_BRACKET);
};
break;
case '|': {
shift(KeyEvent.VK_BACK_SLASH);
};
break;
case ':': {
shift(KeyEvent.VK_SEMICOLON);
};
break;
case ' <': {
shift(KeyEvent.VK_COMMA);
};
break;
case '>': {
shift(KeyEvent.VK_PERIOD);
};
break;
case '?': {
shift(KeyEvent.VK_SLASH);
};
break;
case '\'': {
//robot.keyPress(KeyEvent.VK_QUOTE);
type(javax.swing.KeyStroke.getKeyStroke("QUOTE").getKeyCode());
};
break;
case '"': {
//shift(KeyEvent.VK_QUOTE);
type(javax.swing.KeyStroke.getKeyStroke("QUOTEDBL").getKeyCode());
};
break;
default:
robot.keyPress((int)textArray[i]);
break;
}//end of switch

}//end of if
}//end of for
}//end of keyType-method

//Types string followed by enter
public synchronized void commandType(String text)
{
keyType(text);
robot.keyPress('
');
}


private void type(int keyCode)
{
robot.keyPress(keyCode);
robot.keyRelease(keyCode);
}

//Modifier key press
private void shift(int keyCode)
{
robot.keyPress(16);
robot.keyPress(keyCode);
robot.keyRelease(keyCode);
robot.keyRelease(16);
}
}


//------------------------//
// file TextToVirtual.java //
//------------------------//

import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class TextToVirtual
{

public static void main(String [] args) throws IOException,
AWTException,
IllegalArgumentException
{
// Create robotActions and Robot object
RobotActions bill = new RobotActions();
Robot robbie = bill.returnRobot();

//System.exit(0);

// opens notepad
Process proc = Runtime.getRuntime().exec("notepad.exe");

//Set robot's delays
robbie.delay(1000);
robbie.setAutoDelay(30);


//String examples
bill.commandType("Hello there!.");
bill.commandType(" This is a demo of typing out strings");
bill.commandType("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVSWXYZ");
bill.commandType("`~1!2@3#4$5%6^7&8*9(0)-_=+");
bill.commandType(" [{]}|:;, <.>/?");
bill.commandType("\"); // using escape for odd character
bill.commandType("\'"); // using escape for odd character
bill.commandType("""); // using escape for odd character

//Return control to system
System.exit(0);
}
}



Okey this sample works properly if (and only if!!!) US-keyboard layout and
US-locale are set as default on the client's system. If would try to run this sample on a system with an other default locale (such as german or french) the characters put in (in particular specials, e.g. ,./[]' ) are not correct.



I see two approaches as solution of this problem:

1) a function that generates keyCode for a given char and locale.

2) a function that switches the keyboard mapping for a given application in the system. But this approach assumes a possibility to retreive the actual keyboard mapping for a given application.



I have not found anything (API-functions) like this functions described above:-((
(There is no function in JDK1.3 and JDK1.4, that would generate a keycode for a given locale and character or switch the keyboard map. The Input Framework is only capable for use of java->java and native->java inputs, but not vice versa).



Any suggestions are very welcome.



Thanks in advance,


Dimitri    <<Less
About | Sitemap | Contact