|
19 | 19 | import com.sun.jna.NativeLong; |
20 | 20 | import com.sun.jna.platform.unix.X11; |
21 | 21 | import com.sun.jna.ptr.IntByReference; |
22 | | -import de.openindex.support.core.AppUtils; |
23 | 22 | import org.apache.commons.lang3.StringUtils; |
24 | 23 | import org.slf4j.Logger; |
25 | 24 | import org.slf4j.LoggerFactory; |
@@ -138,72 +137,59 @@ private static void sendTextViaX11(String text) throws LinuxException { |
138 | 137 | X11.XFlush(display); |
139 | 138 |
|
140 | 139 | for (int i = 0; i < text.length(); i++) { |
141 | | - char c = text.charAt(i); |
142 | | - //LOGGER.debug("printing " + c); |
143 | | - |
144 | | - |
145 | | - //int value = StandardCharsets.UTF_8.encode(String.valueOf(c)).getInt(); |
146 | | - //LOGGER.debug("> value " + value); |
147 | | - String unicode = String.format("U%04x", (int) c); |
148 | | - //LOGGER.debug("> code " + unicode); |
149 | | - |
150 | | - //X11.KeySym sym = X11.XStringToKeysym(String.valueOf(c)); |
151 | | - X11.KeySym sym = X11.XStringToKeysym(unicode); |
152 | | - |
153 | | - //int code = (sym != null) ? X11.XKeysymToKeycode(display, sym) : 0x00D1; |
154 | | - int code = X11.XKeysymToKeycode(display, sym); |
155 | | - |
156 | | - X11.KeySym[] resetKeySym = null; |
157 | | - if (!AppUtils.isAsciiCharacter(c)) { |
158 | | - //LOGGER.debug("> use scratch"); |
159 | | - //LOGGER.debug("> " + X11.XKeysymToString(sym)); |
160 | | - |
161 | | - resetKeySym = new X11.KeySym[keysymsPerKeycode.getValue()]; |
162 | | - for (int j = 0; j < resetKeySym.length; j++) { |
163 | | - resetKeySym[j] = X11.XKeycodeToKeysym(display, (byte) scratch_keycode, j); |
164 | | - } |
165 | | - |
166 | | - X11.XChangeKeyboardMapping( |
167 | | - display, |
168 | | - scratch_keycode, |
169 | | - 2, |
170 | | - new X11.KeySym[]{sym, sym}, |
171 | | - 1); |
172 | | - code = scratch_keycode; |
173 | | - } |
174 | | - |
175 | | - //LOGGER.debug("> " + code); |
176 | | - |
177 | | - if (code > 0) { |
178 | | - //XTEST.XTestFakeKeyEvent(display, code, false, new NativeLong(0)); |
179 | | - //X11.XFlush(display); |
| 140 | + final char character = text.charAt(i); |
| 141 | + //LOGGER.debug("printing {}", character); |
180 | 142 |
|
181 | | - XTEST.XTestFakeKeyEvent(display, code, true, new NativeLong(0)); |
182 | | - //X11.XFlush(display); |
183 | | - X11.XSync(display, false); |
| 143 | + // get unicode formatted string |
| 144 | + final String unicode = String.format("U%04x", (int) character); |
| 145 | + //LOGGER.debug("> code {}", unicode); |
184 | 146 |
|
185 | | - XTEST.XTestFakeKeyEvent(display, code, false, new NativeLong(0)); |
186 | | - //X11.XFlush(display); |
187 | | - X11.XSync(display, false); |
| 147 | + // get keysym for the unicode string |
| 148 | + final X11.KeySym sym = X11.XStringToKeysym(unicode); |
188 | 149 |
|
189 | | - //noinspection CatchMayIgnoreException |
190 | | - try { |
191 | | - Thread.sleep(100); |
192 | | - } catch (Exception ex) { |
193 | | - } |
| 150 | + // remember the old value before the keysym is overwritten |
| 151 | + final X11.KeySym[] resetKeySym = new X11.KeySym[keysymsPerKeycode.getValue()]; |
| 152 | + for (int j = 0; j < resetKeySym.length; j++) { |
| 153 | + resetKeySym[j] = X11.XKeycodeToKeysym(display, (byte) scratch_keycode, j); |
194 | 154 | } |
195 | 155 |
|
196 | | - if (resetKeySym != null) { |
197 | | - X11.XChangeKeyboardMapping( |
198 | | - display, |
199 | | - scratch_keycode, |
200 | | - resetKeySym.length, |
201 | | - resetKeySym, |
202 | | - 1); |
203 | | - } |
| 156 | + // register keysym for the unicode string |
| 157 | + X11.XChangeKeyboardMapping( |
| 158 | + display, |
| 159 | + scratch_keycode, |
| 160 | + 2, |
| 161 | + new X11.KeySym[]{sym, sym}, |
| 162 | + 1); |
| 163 | + |
| 164 | + //XTEST.XTestFakeKeyEvent(display, code, false, new NativeLong(0)); |
| 165 | + //X11.XFlush(display); |
| 166 | + |
| 167 | + // post event for key press |
| 168 | + XTEST.XTestFakeKeyEvent(display, scratch_keycode, true, new NativeLong(0)); |
| 169 | + //X11.XFlush(display); |
| 170 | + X11.XSync(display, false); |
| 171 | + |
| 172 | + // post event for key release |
| 173 | + XTEST.XTestFakeKeyEvent(display, scratch_keycode, false, new NativeLong(0)); |
| 174 | + //X11.XFlush(display); |
| 175 | + X11.XSync(display, false); |
| 176 | + |
| 177 | + //noinspection CatchMayIgnoreException |
| 178 | + //try { |
| 179 | + // Thread.sleep(50); |
| 180 | + //} catch (Exception ex) { |
| 181 | + //} |
| 182 | + |
| 183 | + // reset the keysym to the old value before it was overwritten |
| 184 | + X11.XChangeKeyboardMapping( |
| 185 | + display, |
| 186 | + scratch_keycode, |
| 187 | + resetKeySym.length, |
| 188 | + resetKeySym, |
| 189 | + 1); |
204 | 190 | } |
205 | 191 |
|
206 | | - //X11.XFlush(display); |
| 192 | + X11.XFlush(display); |
207 | 193 | //X11.XSync(display, false); |
208 | 194 | X11.XCloseDisplay(display); |
209 | 195 | } |
|
0 commit comments