Getkeycodeforchar printing wrong JAVA

Asked

Viewed 41 times

0

getExtendedKeyCodeForChar is printed wrong:

Using this test class:

package test;

import iRobot.iRobot_Functions;


public class Test {
    public static void main(String[] args) {
        String admUsr = "HOMP\\adm03!@";
        iRobot_Functions irf = new iRobot_Functions();
        irf.Wait(3000);
        irf.Send(admUsr);
        System.out.println(admUsr);
    }
}

Using these functions:

package iRobot;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class iRobot_Functions {
  Robot r;
  public void Wait(int i)
  {
      Robot r;
      try {
          r = new Robot();
          r.delay(i);
      } catch (AWTException ex) {
          Logger.getLogger(iRobot_Functions.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  // pressionar teclas
  public void Press(int keyCode) {
      try {
        r = new Robot();
        r.keyPress(keyCode);
        r.delay(200);
        r.keyRelease(keyCode);
        r.delay(200);
      } catch (AWTException ex) {
        Logger.getLogger(iRobot_Functions.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
    public void Send(String s) {
        if (null == s) return;
        Robot r = null;
        char[] chars = s.toCharArray();
        try {
            for (char c : chars) {
                int code = c;
                int keyCode = KeyEvent.getExtendedKeyCodeForChar(code);
                r = new Robot();
                r.delay(40);
                r.keyPress(keyCode);
                r.keyRelease(keyCode);
            } 
        } catch (AWTException ex) {
            System.err.println(ex.getMessage());
        }
    }
}

No system.out.println is printing certain HOMP adm03!@

But in Send it is printnado wrong homp adm0312

Ignoring shifts and using homp instead of HOMP 1 instead of ! 2 instead of @

https://barrabase.com.br/wp-content/uploads/2017/09/teclado-atalhos-dicas.jpg

Someone knows how to fix it?

  • Please present a [mcve] so that it is possible to test and execute the code.

  • I edited with all the complete code, I hope you or someone can help, I don’t know what to do

  • Solution for those with the same problem: https://stackoverflow.com/questions/1101625/how-do-you-determine-if-a-character-requires-the-shift-key-to-be-down-to-be-type

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.