How do you determine if a character requires the shift key to be pressed for input?

I am writing code to enter strings using the Robot class. Everything is basically fine (well, I have to use the large switch statement to get the character codes), except that some keys don't have key codes, because they are actually a Shift+ key combination of another key. For uppercase letters, it is easy to check using Character.isUpperCase(c), but for all characters, such as !@#$%^&*()_+various punctuation, they are not considered "upper case", although this requires a shift for pressing to generate a keystroke, I tried searching the site, but just found forum post without satisfactory answers. Is there a way to determine if a character needs to be changed, and if so, which character is the “unbiased” version?

EDIT: Here is the code I have.

public void GenerateKeyTyped(char c) {
    if (Character.isUpperCase(c)) {
        r.keyPress(KeyEvent.VK_SHIFT);
    }
    r.keyPress(GetKeyCode(c));
    r.keyRelease(GetKeyCode(c));
    if (Character.isUpperCase(c)) {
        r.keyRelease(KeyEvent.VK_SHIFT);
    }
}
+3
4

, ( ), :

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main3 {

    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("!", "1");
            map.put("@", "2");
            map.put("#", "3");

            char[] charz = "abcdefgABCDEFG123!#".toCharArray();

            for(int i = 0; i < charz.length; i++)
            {
                System.out.print(charz[i] + " : ");
                boolean shiftRequired = false;
                String key = String.valueOf(charz[i]);
                String value = map.get(key);

                if (value != null)
                {
                    shiftRequired = true;
                    key = value;
                }
                else if (Character.isUpperCase(key.charAt(0)))
                {
                    shiftRequired = true;
                }
                else
                {
                    key = key.toUpperCase();
                }

                KeyStroke ks = KeyStroke.getKeyStroke("pressed " + key.toUpperCase());

                int keyCode = ks.getKeyCode();
                System.out.println(keyCode);

                if (shiftRequired)
                    robot.keyPress(java.awt.event.KeyEvent.VK_SHIFT);

                robot.keyPress( keyCode );
                robot.keyRelease( keyCode );

                if (shiftRequired)
                    robot.keyRelease(java.awt.event.KeyEvent.VK_SHIFT);
            }


        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
+1

, , , . , , ( , , Dvorak, , , ), , , .

+1

AWT? , - :

AWTKeyStroke ks = AWTKeyStroke.getAWTKeyStroke('!');
int modifers = ks.getModifiers();

if ((modifiers & KeyEvent.VK_SHIFT) == KeyEvent.VK_SHIFT) {
    // Shift pressed
}
+1

If you look at the ASCII table , these character keys are in the range. 0x21 - 0x2B
Probably you can look at the ASCII table to recognize all your other characters.

0
source

Source: https://habr.com/ru/post/1712162/


All Articles