Java Robot class press Turkish letter (Ö, ö, Ş, ş, Ü, ü, Ğ, ğ, İ, ı, Ç, ç, Ə, ə)? - java

I have problem with press a special letter (Turkish etc.) via java robot class. I hava a method to press keys which works as alt+keycode. I cant convert some special letters to current keycode. So how can I solve it. Thanx
For Example:
KeyStroke ks = KeyStroke.getKeyStroke('ö', 0);
System.out.println(ks.getKeyCode());
Output : 246
// So alt+0246='ö'
//but if I convert 'ş' to keycode
//Output is 351 . So alt+351= '_' and alt+0351= '_'
//What is the Correct combination for 'ş'. same for 'Ş', 'ş','Ğ', 'ğ', 'İ', 'ı', 'Ə', 'ə'
KeyPress:
public void altNumpad(int... numpadCodes) {
if (numpadCodes.length == 0) {
return;
}
robot.keyPress(VK_ALT);
for (int NUMPAD_KEY : numpadCodes) {
robot.keyPress(NUMPAD_KEY);
robot.keyRelease(NUMPAD_KEY);
}
robot.keyRelease(VK_ALT);
}

The character numbers are defiunied in the Unicode standard. The are also used in HTML, therefore you can use this table.
Anyway if you see the character in the source code depends on the fact that the editor interprets the file correctly (UTF-8 is preferred).
Second the used editor must have a font installed that contains these characters. Hence if you type alt+0351 and get and '_' this may just be a replacement character indicating that the font misses this character.
And in the end you should tell the Java compiler that the source code is UTF-8 - just to make sure (javac -encoding utf8).

I am not sure why you did
KeyStroke ks = KeyStroke.getKeyStroke('ö', 0);
Because java docs say,
public static KeyStroke getKeyStroke(Character keyChar,
int modifiers)
//Use 0 to specify no modifiers.
you need to pass a modifier other than 0 to the overload.
You should try to pass a modification like,
java.awt.event.InputEvent.ALT_DOWN_MASK
So probably should try,
KeyStroke ks = KeyStroke.getKeyStroke('ö', java.awt.event.InputEvent.ALT_DOWN_MASK);
Java doc as reference: http://docs.oracle.com/javase/7/docs/api/javax/swing/KeyStroke.html#getKeyStroke(char)
If you cannot properly get a output from that then you should consider the fact the character is UTF-8
This might help you in that regard, Java, Using Scanner to input characters as UTF-8, can't print text

I know this is a late answer but here it is how I handle this problem for Turkish QWERTY keyboard
static void writeRobotWrite(Robot robot, String keys) throws InterruptedException {
....
try {
robot.keyPress(keyCode);
robot.delay(20);
robot.keyRelease(keyCode);
robot.delay(20);
}catch (IllegalArgumentException e)
{
pressUnicode(c, robot);
}
}
}
Basically when I got undefined keyCode for Robot I call pressUnicode function which is:
static void pressUnicode(char c, Robot robot)
{
String cantRecognize = ""+c;
StringSelection selection = new StringSelection(cantRecognize);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
Simply I'm just copying and pasting the character. This is working for all undefined characters. :)

Related

How to flter ANSI/Terminal Control sequences from InputStream?

I'm building a simple terminal emulator using the pty4j library. My program has a print() method that renders the text to a canvas using the GraphicsContext.fillText() method from javafx. I connect the emulator to an instance of cmd and read ouptput from a buffered reader. Now sadly when it recieves text it also includes ANSI-escape characters (see image). However If i print the ouput to the IDE or system console it works fine.
I tried using the readLine() method from the BufferedReader and then applying a regex, but because not all input recieved from the terminal is terminated by a \n it blocks on the last line.
Thread terminalReaderThread = new Thread() {
public void run() {
try {
int c;
while (terminal.isRunning() && (c = terminal.getReader().read()) != -1) {
if(c != 0){
print(Character.toString((char)c));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
terminalReaderThread.start();
Is there an effective way to filter these escape codes from the inputStream?
The answers I received to my question (Safely ignoring unknown ANSI, ESC/P, ESC/POS sequences, know the length) should also answer yours.
If you read the standard (ECMA-48 https://www.ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf) you will see that the sequences always start from the ESCAPE character and always end with a FINAL BYTE which has a value from a defined range.
With this information it is enough to detect start and end of each sequence. (regex for example) (also the newline character (and other C0 codes) is not allowed inside the escape sequence so you will never have an escape sequence not entirely located inside one line)

Java Robot class press special letter?

I have problem with pressing a special letter (Chinese, cyrillic etc.) via java robot class. I hava a method to press keys which works as alt+keycode. I cant convert some special letters to corrent keycode. So how can I solve it. Thanx
For Example:
KeyStroke ks = KeyStroke.getKeyStroke('a', 0);
System.out.println(ks.getKeyCode());
Output : 97
//but if I convert 'ş' to keycode
//Output is 351 . So alt+351= '_' The Correct combination is alt+0254 for 'ş'
KeyPress:
public static void doType(int a, int keyCodes)
throws AWTException {
Robot robot = new Robot();
robot.keyPress(VK_ALT);
robot.keyPress(keyCodes);
robot.keyRelease(keyCodes);
robot.keyRelease(VK_ALT);
}
'a' evaluates to 97 in UTF-8.
KeyStroke.getKeyCode()
simply returns an integer representation of 'a'.

Generate AWT keyevent for special characters

I want to generate key events for Special characters like £, €, µ, ½, Ö, Ä etc. I am able to generate keyevents for key which are on my keyboard like 'A,B,c, %, *, ^' etc with following code:
public static void generateKeyEvent(final int c) {
new Thread() {
public void run() {
try {
Robot robot = new Robot();
robot.keyPress(c);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
robot.keyRelease(c);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
In case of normal characters, it is working fine but in case of characters which i mentioned above the code is throwing following exception:
java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.companyname.utils.Abc$1.run(Abc.java:286)
One thing which i noticed during my search for the solution to this problem, as these special characters are not mapped on my keyboard that is why it is throwing this exception.
any idea, how can i do this?
I got answer to this problem.. basically if you want to print symbol like those then you need to "alt" key for typing that.
for example: if you need to type 'é' in notepad you have to type alt+130.
So i did the same, i generated the key event for alt then for numpad 1 then numpad3 and finally numpad0.
How are you passing the keys?
Note that Robot.keyPress expects key code, not character. Take a look at the KeyEvent constants. There is a VK_EURO_SIGN, not sure about the others.
You should be able to get an arbitrary key code by implementing a KeyListener and checking KeyEvent.getKeyCode() when the particular key (combination of keys) is pressed.

Java robot class

I have a problem with typing in Robot Class. I want the robot to type something the
user has entered. The robot for some reason can't type some of the characters. Here is my type code:
public void type(String s,Robot robot) {
byte[] stringBytes = s.getBytes();
for (byte b : stringBytes) {
int code = b;
if (code > 96 && code < 123)
code = code - 32;
robot.keyPress(code);
robot.keyRelease(code);
}
}
how can i fix this problem?
If you want to "type back what the user entered", then surely you should be capturing a set of KeyEvent objects, and not a String. There is not a key for every String character, far from it! (for instance you need to press 'shift' to input a colon, so that's two key presses and not one)
Robot expects key codes defined in KeyEvent.

Java input method for Virtual Keyboard

I am facing a problem in implementing Input method for Virtual Keyboard. Currently I am using robot class for sending input to any application from virtual keyboard. But for that I need to create mapping of key-code and unicode, which is not consistent on different keyboard layout, can I directly pass the UNICODE to any application using Input method without worry about mapping between keycode and unicode.
Any useful link or sample code will be useful.
It is a simple Java program which is always on top of any application and work as onscreen keyboard. Using a mouse while you press any button (key) of the keyboard, the corresponding character will be typed in the application running below. This is working perfectly for English Alphabets. I am facing problem while I am doing for unicode.
find the code snippet below
public static void simulateKeyEvent(char key){
try{
AWTKeyStroke awtKS = AWTKeyStroke.getAWTKeyStroke(key);
int key_code = awtKS.getKeyCode();
System.out.println("key = "+key+" keyCode = "+key_code);
robot.keyPress(key_code);
robot.keyRelease(key_code);
}catch(Exception e){
e.printStackTrace();
}
}
How I sovled it:
//on startup: override the SystemEventQueue
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
final OwnEventQueue newEventQueue = new OwnEventQueue();
eventQueue.push(newEventQueue);
//because dispatchEvent is protected
public class OwnEventQueue {
private final static OwnEventQueue instance;
static{
instance = new OwnEventQueue();
}
#Override
public void dispatchEvent(AWTEvent event) {
super.dispatchEvent(event);
}
public static OwnEventQueue getInstance() {
return instance;
}
}
//then onpress of keyboard button
Character character = getCharacter();
int[] events = {KeyEvent.KEY_PRESSED, KeyEvent.KEY_RELEASED, KeyEvent.KEY_TYPED};
for (int i = 0; i < events.length; i++) {
KeyEvent pressKeyEvent = new KeyEvent(focusComponent, events[i], System.currentTimeMillis(), 0, 0, character.charValue());
OwnEventQueue.getInstance().dispatchEvent(pressKeyEvent);
}
robotKeystrokeSender.keyPress(KeyEvent.VK_RIGHT);
robotKeystrokeSender.delay(10);
robotKeystrokeSender.keyRelease(KeyEvent.VK_RIGHT);
Is your virtual keyboard used as a device by your OS ?
Or, in other words, have you tried considering it as a "real" keyboard ?
According to Java hardware abstraction, were your virtual keyboard to be considered as a driver, it should simply work like a real keyboard.
EDIT : according to comment, this is not a virtual device, but a Java application, as a consequence, probleme is different.
According to Javadoc, Robot can send key strokes given as int. To create those key strokes from characters, I would recommand you create them using getKeystroke(char) before to transform them into integer values using getKeycode(). This way, you would have integer values associatesd your unicode characters, whichever they are.
EDIT 2 : once again, a modification ;-)
it seems like getKeyStroke(String) "should" handle unicode characters.

Categories

Resources