Einige Symbole können mit java.awt.Robot auf einer spanischen Tastatur nicht geschrieben werdenJava

Java-Forum
Anonymous
 Einige Symbole können mit java.awt.Robot auf einer spanischen Tastatur nicht geschrieben werden

Post by Anonymous »

Bei Verwendung der Klasse java.awt.Robot in Java mit einer spanischen Tastatur wie im Bild gezeigt konnte ich den Tastendruck der folgenden Symbole nicht simulieren:

Code: Select all

{
}
[
]
~
@
#
^
\
|
Hier ist der Code, den ich verwende:

Code: Select all

package test;

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

public class Principal5 {

public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec("notepad.exe");

sleep(2000);

writeString("aAbBcC012345();{} -=_+\t\n[]:\",~!@#$%^&*\\|\'/?¿ñ¡");
}

private static void writeString(String str) throws Exception {
Robot robot = new Robot();

// Press keys using robot
char car;
for (int i = 0; i < str.length(); i++) {
car = str.charAt(i);
try {
// Check if the current character is a capital letter
if (Character.isUpperCase(car)) {
// Press shift key
robot.keyPress(KeyEvent.VK_SHIFT);
// Press the current character
robot.keyPress(Character.toUpperCase(car));
// Release shift key
robot.keyRelease(KeyEvent.VK_SHIFT);
} else if (Character.isLetter(car)) {
// else display the character as it is
robot.keyPress(Character.toUpperCase(car));
} else {
symbolPress(car);
}
} catch (Exception e1) {
System.out.println("Exception for character " + car + " " + e1.getMessage());
}

// wait for 200ms
sleep(200);
}

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}

private static void symbolPress(char car) throws Exception {
switch (car) {
case ';':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_COMMA);
break;

case '(':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_8);
break;
case ')':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_9);
break;

/*
case '{':
doType(KeyEvent.VK_ALT_GRAPH, KeyEvent.VK_BACK_QUOTE);
break;
case '}':
doType(KeyEvent.VK_ALT_GRAPH, KeyEvent.VK_DEAD_CEDILLA);
break;
*/

case ' ':
doType(KeyEvent.VK_SPACE);
break;
case '-':
doType(KeyEvent.VK_MINUS);
break;
case '=':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_0);
break;
case '_':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_MINUS);
break;
case '+':
doType(KeyEvent.VK_PLUS);
break;
case '\t':
doType(KeyEvent.VK_TAB);
break;
case '\n':
doType(KeyEvent.VK_ENTER);
break;

/*
case '[':
doType(KeyEvent.VK_ALT_GRAPH, KeyEvent.VK_OPEN_BRACKET);
break;
case ']':
doType(KeyEvent.VK_ALT_GRAPH, KeyEvent.VK_PLUS);
break;
*/

case ':':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_PERIOD);
break;
case '"':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_2);
break;
case ',':
doType(KeyEvent.VK_COMMA);
break;
case '':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_LESS);
break;

case '0':
doType(KeyEvent.VK_0);
break;
case '1':
doType(KeyEvent.VK_1);
break;
case '2':
doType(KeyEvent.VK_2);
break;
case '3':
doType(KeyEvent.VK_3);
break;
case '4':
doType(KeyEvent.VK_4);
break;
case '5':
doType(KeyEvent.VK_5);
break;
case '6':
doType(KeyEvent.VK_6);
break;
case '7':
doType(KeyEvent.VK_7);
break;
case '8':
doType(KeyEvent.VK_8);
break;
case '9':
doType(KeyEvent.VK_9);
break;

/*
case '~':
doType(KeyEvent.VK_ALT_GRAPH, KeyEvent.VK_4, KeyEvent.VK_ENTER);
break;
case '@':
doType(KeyEvent.VK_ALT_GRAPH, KeyEvent.VK_2);
break;
case '#':
doType(KeyEvent.VK_ALT_GRAPH, KeyEvent.VK_3);
break;
case '^':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET);
break;
case '\\':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH);
break;
case '|':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH);
break;
*/

case '!':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_1);
break;
case '¡':
doType(KeyEvent.VK_INVERTED_EXCLAMATION_MARK);
break;

case '$':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_4);
break;
case '%':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5);
break;
case '&':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_6);
break;
case '*':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_PLUS);
break;
case '\'':
doType(KeyEvent.VK_QUOTE);
break;
case '/':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_7);
break;
case '?':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_QUOTE);
break;
case '¿':
doType(KeyEvent.VK_SHIFT, KeyEvent.VK_INVERTED_EXCLAMATION_MARK);
break;

/*
case '`':
doType(KeyEvent.VK_BACK_QUOTE);
break;
*/

default:
System.out.println("Not cathegorized character " + car);
}

}

private static void doType(int... keyCodes) throws Exception {
doType(keyCodes, 0, keyCodes.length);
}

private static void doType(int[] keyCodes, int offset, int length) throws Exception {
if (length > 0) {
Robot robot = new Robot();
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}

// Our custom sleep method
public static void sleep(long ms) {
try {
Thread.sleep(ms);
} catch (Exception ignored) {
// Se ignora
}
}

}
Image

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post