Задать вопрос
Labunsky
@Labunsky
Я есть на хабре

Почему не работают '[', ']' и некоторые другие клавиши при симуляции нажатий на Windows XP awt.Robot'ом, и как с этим бороться?

Есть локализованная версия Windows XP. Робот отказывается нажимать клавиши [ ] ; ' , /, бросая вместо этого эксепшены.
Физически клавиши в наличии, нажимаются. Раскладка - en_US. На более поздних версиях винды и линукса все работает корректно.

Код для тестирования:
Robot robot = new Robot();
for (int i = 0; i < 256; i++) {
        try {
            robot.keyPress(i);
            robot.delay(100);
            robot.keyRelease(i);
        } catch (IllegalArgumentException e) {
            System.out.println("Failed to press " + KeyEvent.getKeyText(i));
        }
}

Вывод на XP

Failed to press Unknown keyCode: 0x0
Failed to press Unknown keyCode: 0x1
Failed to press Unknown keyCode: 0x2
Failed to press Unknown keyCode: 0x4
Failed to press Unknown keyCode: 0x5
Failed to press Unknown keyCode: 0x6
Failed to press Unknown keyCode: 0x7
Failed to press Unknown keyCode: 0xb
Failed to press Unknown keyCode: 0xd
Failed to press Unknown keyCode: 0xe
Failed to press Unknown keyCode: 0xf
Failed to press Kana
Failed to press Unknown keyCode: 0x16
Failed to press Unknown keyCode: 0x17
Failed to press Final
Failed to press Kanji
Failed to press Unknown keyCode: 0x1a
Failed to press Accept
Failed to press Mode Change
Failed to press Unknown keyCode: 0x29
Failed to press Unknown keyCode: 0x2a
Failed to press Unknown keyCode: 0x2b
Failed to press Comma
Failed to press Slash
Failed to press Unknown keyCode: 0x3a
Failed to press Semicolon
Failed to press Unknown keyCode: 0x3c
Failed to press Unknown keyCode: 0x3e
Failed to press Unknown keyCode: 0x3f
Failed to press Unknown keyCode: 0x40
Failed to press Open Bracket
Failed to press Close Bracket
Failed to press Unknown keyCode: 0x5e
Failed to press Unknown keyCode: 0x5f
Failed to press Unknown keyCode: 0x7c
Failed to press Unknown keyCode: 0x7d
Failed to press Unknown keyCode: 0x7e
Failed to press Dead Grave
Failed to press Dead Acute
Failed to press Dead Circumflex
Failed to press Dead Tilde
Failed to press Dead Macron
Failed to press Dead Breve
Failed to press Dead Above Dot
Failed to press Dead Diaeresis
Failed to press Dead Above Ring
Failed to press Dead Double Acute
Failed to press Dead Caron
Failed to press Dead Cedilla
Failed to press Dead Ogonek
Failed to press Dead Iota
Failed to press Dead Voiced Sound
Failed to press Dead Semivoiced Sound
Failed to press Unknown keyCode: 0x92
Failed to press Unknown keyCode: 0x93
Failed to press Unknown keyCode: 0x94
Failed to press Unknown keyCode: 0x95
Failed to press Ampersand
Failed to press Asterisk
Failed to press Double Quote
Failed to press Less
Failed to press Meta
Failed to press Unknown keyCode: 0x9e
Failed to press Unknown keyCode: 0x9f
Failed to press Greater
Failed to press Left Brace
Failed to press Right Brace
Failed to press Unknown keyCode: 0xa3
Failed to press Unknown keyCode: 0xa4
Failed to press Unknown keyCode: 0xa5
Failed to press Unknown keyCode: 0xa6
Failed to press Unknown keyCode: 0xa7
Failed to press Unknown keyCode: 0xa8
Failed to press Unknown keyCode: 0xa9
Failed to press Unknown keyCode: 0xaa
Failed to press Unknown keyCode: 0xab
Failed to press Unknown keyCode: 0xac
Failed to press Unknown keyCode: 0xad
Failed to press Unknown keyCode: 0xae
Failed to press Unknown keyCode: 0xaf
Failed to press Unknown keyCode: 0xb0
Failed to press Unknown keyCode: 0xb1
Failed to press Unknown keyCode: 0xb2
Failed to press Unknown keyCode: 0xb3
Failed to press Unknown keyCode: 0xb4
Failed to press Unknown keyCode: 0xb5
Failed to press Unknown keyCode: 0xb6
Failed to press Unknown keyCode: 0xb7
Failed to press Unknown keyCode: 0xb8
Failed to press Unknown keyCode: 0xb9
Failed to press Unknown keyCode: 0xba
Failed to press Unknown keyCode: 0xbb
Failed to press Unknown keyCode: 0xbc
Failed to press Unknown keyCode: 0xbd
Failed to press Unknown keyCode: 0xbe
Failed to press Unknown keyCode: 0xbf
Failed to press Back Quote
Failed to press Unknown keyCode: 0xc1
Failed to press Unknown keyCode: 0xc2
Failed to press Unknown keyCode: 0xc3
Failed to press Unknown keyCode: 0xc4
Failed to press Unknown keyCode: 0xc5
Failed to press Unknown keyCode: 0xc6
Failed to press Unknown keyCode: 0xc7
Failed to press Unknown keyCode: 0xc8
Failed to press Unknown keyCode: 0xc9
Failed to press Unknown keyCode: 0xca
Failed to press Unknown keyCode: 0xcb
Failed to press Unknown keyCode: 0xcc
Failed to press Unknown keyCode: 0xcd
Failed to press Unknown keyCode: 0xce
Failed to press Unknown keyCode: 0xcf
Failed to press Unknown keyCode: 0xd0
Failed to press Unknown keyCode: 0xd1
Failed to press Unknown keyCode: 0xd2
Failed to press Unknown keyCode: 0xd3
Failed to press Unknown keyCode: 0xd4
Failed to press Unknown keyCode: 0xd5
Failed to press Unknown keyCode: 0xd6
Failed to press Unknown keyCode: 0xd7
Failed to press Unknown keyCode: 0xd8
Failed to press Unknown keyCode: 0xd9
Failed to press Unknown keyCode: 0xda
Failed to press Unknown keyCode: 0xdb
Failed to press Unknown keyCode: 0xdc
Failed to press Unknown keyCode: 0xdd
Failed to press Quote
Failed to press Unknown keyCode: 0xdf
Failed to press Up
Failed to press Down
Failed to press Left
Failed to press Right
Failed to press Unknown keyCode: 0xe4
Failed to press Unknown keyCode: 0xe5
Failed to press Unknown keyCode: 0xe6
Failed to press Unknown keyCode: 0xe7
Failed to press Unknown keyCode: 0xe8
Failed to press Unknown keyCode: 0xe9
Failed to press Unknown keyCode: 0xea
Failed to press Unknown keyCode: 0xeb
Failed to press Unknown keyCode: 0xec
Failed to press Unknown keyCode: 0xed
Failed to press Unknown keyCode: 0xee
Failed to press Unknown keyCode: 0xef
Failed to press Unknown keyCode: 0xf6
Failed to press Unknown keyCode: 0xf7
Failed to press Unknown keyCode: 0xf8
Failed to press Unknown keyCode: 0xf9
Failed to press Unknown keyCode: 0xfa
Failed to press Unknown keyCode: 0xfb
Failed to press Unknown keyCode: 0xfc
Failed to press Unknown keyCode: 0xfd
Failed to press Unknown keyCode: 0xfe
Failed to press Unknown keyCode: 0xff
  • Вопрос задан
  • 123 просмотра
Подписаться 2 Сложный 6 комментариев
Решения вопроса 1
Labunsky
@Labunsky Автор вопроса
Я есть на хабре
В итоге использовал затычку для клавиш с помощью простой обертки над JNA:
class WinXP {
        private static boolean isXP;
        private static Map<Integer, Integer> faultyXPCodes = new HashMap<>();

        static {
            String os = System.getProperty("os.name").toLowerCase();
            isXP = os.startsWith("windows") && os.contains("xp");

            faultyXPCodes.put(91 , 0x1A); // [
            faultyXPCodes.put(93 , 0x1B); // ]
            faultyXPCodes.put(92 , 0x2B); // \
            faultyXPCodes.put(59 , 0x27); // ;
            faultyXPCodes.put(222, 0x28); // '
            faultyXPCodes.put(44 , 0x33); // ,
            faultyXPCodes.put(46 , 0x34); // .
            faultyXPCodes.put(47 , 0x35); // /
        }

        private static boolean isFaulty(int code) {
            return isXP && faultyXPCodes.containsKey(code);
        }

        private static WinUser.INPUT PrepareInput(int flag, int code) {
            final WinUser.INPUT input = new WinUser.INPUT();

            input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
            input.input.setType("ki");

            input.input.ki.wScan = new WinDef.WORD(0x0);
            input.input.ki.time = new WinDef.DWORD(0x0);
            input.input.ki.dwFlags = new WinDef.DWORD(flag | 0x8);
            input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR(0x0);

            input.input.ki.wScan = new WinDef.WORD(faultyXPCodes.get(code));
            return input;
        }

        private static void SendInput(WinUser.INPUT input) {
            User32.INSTANCE.SendInput(
                    new WinDef.DWORD(1),
                    (WinUser.INPUT[]) input.toArray(1),
                    input.size()
            );
        }

        private static void Press(int code) {
            SendInput(PrepareInput(0x0, code));
        }

        private static void Release(int code) {
            SendInput(PrepareInput(0x2, code));
        }
    }
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы