0

So, to give a bit of context, I finally got tired of having to research for the ALT codes of certain symbols and sub/superscript numbers, and never getting it right, I suppose this mapping error have something to do with de keyboard version I'm using, but to be honest, I don't care, and to solve this problem I created a program in Java that will print all the alt codes going from Alt0000 to Alt9999 on a separate notepad window via keyPress but, I'm having problems on printing all of them, somehow it erases the text multiple times during the run, and after a while it starts printing nonsensical things.

As requested in a comment bellow, here's a print of the nonsensical things:

Nonsensical Things

And for adding more details, the program runs fine for the first 100-ish codes, them it erases some of the codes and run fine until de 6000-ish codes, which is where it starts printing only the pre texts that indicate the codes and don't breaking any lines, a further more down the line it also erases the progress, and keeps with de "nonsensical things" as I am calling it.

If someone could give me some tips on how to do/fix this I would very much appreciate.

Since I'm not an expert programmer I'm also accepting tips on how to improve my code.

Thanks. :)

This is my code:

package randomProjects;

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

public class SimulateKeyPress {

    public static void main(String[] args) throws InterruptedException {
        
        int[] keyEvents = {KeyEvent.VK_NUMPAD0,
                KeyEvent.VK_NUMPAD1,
                KeyEvent.VK_NUMPAD2,
                KeyEvent.VK_NUMPAD3,
                KeyEvent.VK_NUMPAD4,
                KeyEvent.VK_NUMPAD5,
                KeyEvent.VK_NUMPAD6,
                KeyEvent.VK_NUMPAD7,
                KeyEvent.VK_NUMPAD8,
                KeyEvent.VK_NUMPAD9};
        
        try {
            Robot robot = new Robot();
            
            // click on notepad window positioned at the rigth of the IDE
            robot.mouseMove(1000,400);
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            
            // run trhought the thousand units
            for(int um = 0; um <= 9; um++) {
                // run trhought the hundreds
                for(int c = 0; c <= 9; c++) {
                    // run trhought the dozens
                    for(int d = 0; d <= 9; d++) {
                        // run trhought the units
                        for(int u = 0; u <= 9; u++) {
                            // write the pressed keys
                            // alt+#### 
                            robot.keyPress(KeyEvent.VK_A);
                            robot.keyRelease(KeyEvent.VK_A);
                            robot.keyPress(KeyEvent.VK_L);
                            robot.keyRelease(KeyEvent.VK_L);
                            robot.keyPress(KeyEvent.VK_T);
                            robot.keyRelease(KeyEvent.VK_T);
                            robot.keyPress(KeyEvent.VK_ADD);
                            robot.keyRelease(KeyEvent.VK_ADD);
                            robot.keyPress(keyEvents[um]);
                            robot.keyRelease(keyEvents[um]);
                            robot.keyPress(keyEvents[c]);
                            robot.keyRelease(keyEvents[c]);
                            robot.keyPress(keyEvents[d]);
                            robot.keyRelease(keyEvents[d]);
                            robot.keyPress(keyEvents[u]);
                            robot.keyRelease(keyEvents[d]);
                            robot.keyPress(KeyEvent.VK_TAB);
                            
                            // write the respective ALT CODE result
                            robot.keyPress(KeyEvent.VK_ALT);
                            robot.keyPress(keyEvents[um]);
                            robot.keyPress(keyEvents[c]);
                            robot.keyPress(keyEvents[d]);
                            robot.keyPress(keyEvents[u]);
                            robot.keyRelease(KeyEvent.VK_ALT);
                            robot.keyRelease(keyEvents[um]);
                            robot.keyRelease(keyEvents[c]);
                            robot.keyRelease(keyEvents[d]);
                            robot.keyRelease(keyEvents[u]);
                            
                            // Break line
                            robot.keyPress(KeyEvent.VK_ENTER);
                            robot.keyRelease(KeyEvent.VK_ENTER);
                            
                            //Thread.sleep(100);
                        }
                    }
                }
            }
    
        } catch (AWTException e) {
                e.printStackTrace();
        }
    }

}

I've also found this related queries, but had no luck in finding an answer to my problem:

ALT-Codes in Java

How to detect Windows alt-code input without relying on keypress?

How to simulate keyboard presses in java?

liuzp
  • 1
  • 1
  • Pressing ALT+1 is not the same as pressing ALT+0001. On my Windows 10 machine, ALT+0001 prints nothing but ALT+1 prints a smiley face. Also, rather than open a separate Notepad window maybe you should write a Java [GUI](https://docs.oracle.com/javase/8/javase-clienttechnologies.htm) application using _Swing_ or JavaFX that has a text area and print the ALT codes in the text area. And what do you mean by _after a while it starts printing nonsensical things_? Can you provide an example of a _nonsensical_ thing? – Abra Jan 07 '22 at 05:37
  • @Abra I know that ALT+0001 is different from ALT+1, and since all the ALT codes that I've seen are of the form ALT+#### I didn't bother in programming the numbers of the form #, ##, and ###. About the notepad, it was the easy way out for something that I'll use one time only, so I also didn't bother in programming a swing GUI. I've edited the post to include a print of the nonsensical things. – liuzp Jan 07 '22 at 06:57

0 Answers0