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.
Related
I'm using Java, Selenium, and Chrome for test automation. Our developers recently upgraded our UI from AngularJS to Angular2 (not sure if that matters). But since then, sendKeys is inputting incomplete characters in to the text field. Here's an example:
public void enterCustomerDetails()
{
txtFirstName.sendKeys("Joh201605130947AM");
txtSurname.sendKeys("Doe201605130947AM");
txtEmail.sendKeys("johndoe#gmail.com");
}
I also tried using executeScript. It didn't work. It can enter complete characters but the form thinks the field is null.
public void forceSendKeys(WebElement element, String text)
{
if (element != null)
((JavascriptExecutor) this.webDriver).executeScript("arguments[0].value=arguments[1]", element, text);
}
public void enterCustomerDetails()
{
forceSendKeys(txtFirstName, "Joh201605130947AM");
forceSendKeys(txtSurname, "Doe201605130947AM");
forceSendKeys(txtEmail, "johndoe#gmail.com");
}
I also tried using .click() before .sendKeys and adding in sleep time. They didn't work too.
I got an idea to enter the characters 1 by 1 from this post: How to enter characters one by one in to a text field in selenium webdriver?
It worked but that means I have to rewrite all my codes from sendKeys to the new function:
public void sendChar(WebElement element, String value)
{
element.clear();
for (int i = 0; i < value.length(); i++){
char c = value.charAt(i);
String s = new StringBuilder().append(c).toString();
element.sendKeys(s);
}
}
public void enterCustomerDetails()
{
sendChar(txtFirstName, "Joh201605130947AM");
sendChar(txtSurname, "Doe201605130947AM");
sendChar(txtEmail, "johndoe#gmail.com");
}
If you guys know a better way, please help! :)
I assume this is caused by this Angular2 issue https://github.com/angular/angular/issues/5808
Angular can't process input events when they arrive too fast.
As a workaround you would need to send single characters with a small delay between each.
I stumbled upon this error when doing integration tests with NightwatchJS (which uses selenium).
So I'm writing this for people coming here in the future.
I wrote this extension command for nightwatch:
exports.command = function (selector, value, using) {
var self = this;
self.elements(using || 'css selector', selector, function (elems) {
elems.value.forEach(function (element) {
for (var c of value.split('')) {
self.elementIdValue(element.ELEMENT, c);
}
});
});
return this;
};
Which can be used in this way:
var username = 'integration#test.com';
browser.setValueSlow('input[ngcontrol=username]', username); //Works with ng2!
This issue was also discussed on NightwatchJS's github here
By using Actions class, this issue solved for me
Tried many ways as mentioned above. Also tried to setvalue by js executescript
Finally, found this code and it worked well for grid component built on angular
actions.sendKeys(webElement,
modifiedValue).perform();
I was getting this error too in Java, Selenium. You might also be getting this error too while writing your codes - "sendKeys (CharSequence) from the type Webelement refers to the missing type charSequence"
I tried varying the wait time and even typing extra characters before the main characters, they did not work.
The simple trick I used was to change the Java Compiler version from JRE 9 to JRE 10.
This is due to a bug in Angular apps. Workaround is to put a sleep function.
public void setText(By element, String text) {
sleep(100); // Angular version < 2 apps require this sleep due to a bug
driver.findElement(element).clear();
driver.findElement(element).sendKeys(text);
}
i had the same problem, if you see it carefully selenium is changing the characters, some numbers perform a backspace or other symbols, i read it happens when using selenium with vncserver, i changed to firefox.... and it worked.
if that's not your problem, maybe sending the data in parts:
input1="Joh201605130947AM"
txtFirstName.sendKeys(input1[0:7])
txtFirstName.sendKeys(input1[8:end])
Using
Chromium 78.0.3904.70,
Vaadin Flow Framework 14.1.3,
Selenium 3.141.59
and OpenJDK 11.0.5
the behavior also occurs and is even worse:
I see that the character is typed in and suddenly after that it disappears.
A workaround is to be persistent and just try it again. And again. Until the character is finally typed in.
// Type in every single character
for (int i = 0; i < textToType.length(); i++) {
boolean typingCharacterWasSuccessful = false;
// If typing was not successful before, just type again
while (!typingCharacterWasSuccessful) {
// Type in the character
char singleCharacterToType = textToType.charAt(i);
htmlTextfeld.sendKeys(Character.toString(singleCharacterToType));
// Wait a little. Maybe alternatively/additionally wait.until(...)
Thread.sleep(200);
// Check typed in string.
String inputValueAfterTyping = htmlTextfeld.getAttribute("value");
if (inputValueAfterTyping.length() > i + 1) {
// Alternatively: delete everything and start all over
throw new Exception("Input value too long. Maybe character typed in twice!?");
}
// Typing was successful if the value in the input field is as expected (up to now)
typingCharacterWasSuccessful
= inputValueAfterTyping.equals(textToType.substring(0, i + 1));
}
}
I had a similar issue for big texts in Java.
I overcome the issue using the copy and paste of the text in the keyboard-related methods, as in the following method:
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
public static void clickAndSetMultilineText(WebElement element, String text) {
/** click on element **/
element.click();
/** clear older content of the text using keyboard functionality **/
element.sendKeys(Keys.CONTROL + "a"); // select all text
element.sendKeys(Keys.DELETE); // delete old text
StringSelection stringSelection= new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null); // copy text to the keyboard
element.sendKeys(Keys.CONTROL+"v"); // paste text to the field
}
Hope this is helpful.
try this code.. other way to set values using javascript
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");
driver.findElement(By.xpath("//input[#name='body']")).clear();
driver.findElement(By.xpath("//input[#name='body']")).sendKeys("Ripon: body text");
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. :)
I have problems with reading user input from java lanterna library terminal. Upon key strike I would like the system to print a certain character on the terminal. I use this code:
public class Snake {
public static void main(String[] args) {
Terminal terminal = TerminalFacade.createTerminal(System.in, System.out, Charset.forName("UTF8"));
terminal.enterPrivateMode();
Key key =terminal.readInput();
if (key.getKind() == Key.Kind.Tab)
{
terminal.moveCursor(100, 100);
terminal.putCharacter('D');
}
}
}
Unfortunately, I only have terminal opened - I cannot do any input. Anybody has an idea why this happens?
Based on the given code, it seems that you are only running through your if statement once before the main method is finished executing.
Try implementing a while loop to continuously search for input like so:
public static void main(String[] args) {
Terminal terminal = TerminalFacade.createTerminal(System.in, System.out, Charset.forName("UTF8"));
terminal.enterPrivateMode();
// I would recommend changing "true" to a boolean variable that you can flip with a key press.
// For example, the "esc" key to exit the while loop and close the program
Key key;
while(true){
// Read input
key = terminal.readInput();
// Check the input for the "tab" key
if (key.getKind() == Key.Kind.Tab){
terminal.moveCursor(100, 100);
terminal.putCharacter('D');
}
}
terminal.exitPrivateMode();
}
Additionally, check out the Lanterna development guide.
I have an issue with windowlicker on OS X ( everything works ok on Windows ).
The issue is that when I try to simulate user input to any text field the data is not inserted properly ( some letters are cut out ).
For instance:
JTextFieldDriver txField = new JTextFieldDriver(this,
JTextField.class,
named(fieldName));
txField.focusWithMouse();
txField.typeText(input);
Preceding code will result in that I will observe windowlicker inserting input to a text field named fieldName and the input will be incomplete ( Peter will be Peer or Fred will be Fre and so on ). Everything works properly on windows.
I am not sure if all that has anything to do with a warning. I get similar on windows. The warning is:
"WARNING: could not load keyboard layout Mac-, using fallback layout with reduced capabilities (JAR entry com/objogate/wl/keyboard/Mac- not found in /Users/odo/.m2/repository/com/googlecode/windowlicker/windowlicker-core/r268/windowlicker-core-r268.jar)"
Windowlicker seems not to be very popular tool. Nevertheless I managed to figure out the root cause. The warning stating that keyboard layout can't be set is displayed because I am not using english locale. It looks like windowlicker supports only Mac-GB keyboard layout.
The warning will go away if an appropriate system property is set.
For instance:
System.setProperty("com.objogate.wl.keyboard", "Mac-GB");
However this will not solve the main problem. After couple trials I figured out that only 'a' and 'd' characters are trimmed. This is because windowlicker inserts them as if user would hold the 'a' or 'd' key for a bit. Holding those keys results in a helper menu invocation which allows to select special characters. In order to fix that I played with the JTextComponentDriver and found a workaround. The solution is not to use driver's typeText to insert text. JTextComponentDriver's component() method can be used to retrieve the actual guy component and then having an instance setText() can be invoked to set the text.
Below I present my helper class which is using described solution:
public class TextTyper {
private final String inputText;
privte TextTyper(String inputText) {
this.inputText = inputText;
}
public static TextTyper typeText( final String inputText ){
return new TextTyper( inputText );
}
public void into( JTextComponentDriver<?> driver ) throws Exception{
driver.focusWithMouse();
driver.clearText();
Component cmp = driver.component().component();
if(cmp instanceof JPasswordField ){
JPasswordField pwField = (JPasswordField) cmp;
pwField.setText(this.inputText);
}
else if( cmp instanceof JTextField){
JTextField txField = (JTextField) cmp;
txField.setText(this.inputText);
}
else
throw new Exception("Component is not an instance of JTextField or JPasswordField");
}
}
I am new to java and android development and I am working on an application that takes input from the user through an EditText. However, sometimes the input data has some characters with an encoding that is different from what the app expects.
I have created a hashmap with all the possible mapping (about 460 elements) and whenever the user types in (or paste) something I use the afterTextChanged method to go through the text and swap the ones that matches the incorrect encoding with the correct ones that displays correctly.
My code works fine for small sized entry, however the longer it is the slower it gets because afterTextChanged will be called recursively after every swap I do. And when pasting a text that has about 30+ characters with incorrect encoding the application will crash.
here is my code:
public void afterTextChanged(Editable editable) {
substituteText(editable);
}
public CharSequence substituteText(Editable txt) {
if (txt.length() == 0) {
return txt;
}
for (int i = 0; i < txt.length(); i++) {
char ch = txt.charAt(i);
CharSequence ret = myHashMap.get(ch);
if (ret != null) {
txt.replace(i, i + 1, ret);
}
}
return txt;
}
I thought about cloning "editable" to some, change it and then copy it back to the edittext editable variable so it will only see it as one change but I failed to do it since I couldn't find an easy way to clone it.
Is there a more efficient way to do this?
If you use onTextChanged instead of afterTextChanged, you'll get passed the new character(s) the user typed and where they are, and you'd only need to check those.