Fixed buggy keyboard behavior

This commit is contained in:
Andrea Cavalli 2018-10-16 20:03:12 +02:00
parent f7a7f6d200
commit 6e5ed8ef94
2 changed files with 61 additions and 59 deletions

View File

@ -767,7 +767,7 @@ public class Keyboard {
{ Key.SHIFT, Key.SHIFT, Key.SHIFT }, /* 0,0 */ { Key.SHIFT, Key.SHIFT, Key.SHIFT }, /* 0,0 */
{ Key.ALPHA, Key.ALPHA, Key.ALPHA }, /* 0,1 */ { Key.ALPHA, Key.ALPHA, Key.ALPHA }, /* 0,1 */
{ Key.NONE, Key.NONE, Key.NONE }, /* 0,2 */ { Key.NONE, Key.NONE, Key.NONE }, /* 0,2 */
{ Key.NONE, Key.NONE, Key.NONE }, /* 0,3 */ { Key.UP, Key.NONE, Key.NONE }, /* 0,3 */
{ Key.NONE, Key.NONE, Key.NONE }, /* 0,4 */ { Key.NONE, Key.NONE, Key.NONE }, /* 0,4 */
{ Key.SETTINGS, Key.NONE, Key.NONE }, /* 0,5 */ { Key.SETTINGS, Key.NONE, Key.NONE }, /* 0,5 */
{ Key.BRIGHTNESS_CYCLE, Key.BRIGHTNESS_CYCLE_REVERSE, Key.ZOOM_MODE }, /* 0,6 */ { Key.BRIGHTNESS_CYCLE, Key.BRIGHTNESS_CYCLE_REVERSE, Key.ZOOM_MODE }, /* 0,6 */

View File

@ -167,71 +167,73 @@ public class HtmlEngine implements GraphicEngine {
}); });
final NodeList<? extends HTMLElement> buttons = HtmlEngine.document.getBody().getElementsByTagName("button"); final NodeList<? extends HTMLElement> buttons = HtmlEngine.document.getBody().getElementsByTagName("button");
for (int i = 0; i < buttons.getLength(); i++) for (int i = 0; i < buttons.getLength(); i++)
if (buttons.item(i).hasAttribute("keycode")) if (buttons.item(i).hasAttribute("keycode")) {
buttons.item(i).addEventListener("click", (final Event evt) -> { buttons.item(i).addEventListener("touchstart", (final Event evt) -> {
evt.preventDefault(); buttonEvent(evt, false);
final EventTarget target = evt.getCurrentTarget();
final HTMLButtonElement button = target.cast();
new Thread(() -> {
try {
if (button.hasAttribute("keycode") && button.getAttribute("keycode").contains(",")) {
final String code = button.getAttribute("keycode");
final String[] coordinates = code.split(",", 2);
final boolean removeshift = Keyboard.shift && Integer.parseInt(coordinates[0]) != 0 && Integer.parseInt(coordinates[1]) != 0;
final boolean removealpha = Keyboard.alpha && Integer.parseInt(coordinates[0]) != 0 && Integer.parseInt(coordinates[1]) != 1;
Keyboard.keyRaw(Integer.parseInt(coordinates[0]), Integer.parseInt(coordinates[1]), false);
if (removeshift)
Keyboard.keyRaw(0, 0, false);
if (removealpha)
Keyboard.keyRaw(0, 1, false);
Thread.sleep(100);
Keyboard.keyRaw(Integer.parseInt(coordinates[0]), Integer.parseInt(coordinates[1]), true);
if (removeshift)
Keyboard.keyRaw(0, 0, true);
if (removealpha)
Keyboard.keyRaw(0, 1, true);
} else if (Keyboard.alpha && !Keyboard.shift) {
if (button.hasAttribute("keycodea")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodea")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodea")), true);
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), true);
}
} else if (!Keyboard.alpha && Keyboard.shift) {
if (button.hasAttribute("keycodes")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodes")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodes")), true);
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), true);
}
} else if (Keyboard.alpha && Keyboard.shift) {
if (button.hasAttribute("keycodesa")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodesa")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodesa")), true);
} else if (button.hasAttribute("keycodes")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodes")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodes")), true);
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), true);
}
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), false);
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), true);
}
} catch (final Exception ex) {
ex.printStackTrace();
}
}).start();
}); });
buttons.item(i).addEventListener("touchend", (final Event evt) -> {
buttonEvent(evt, true);
});
buttons.item(i).addEventListener("mousedown", (final Event evt) -> {
buttonEvent(evt, false);
});
buttons.item(i).addEventListener("mouseup", (final Event evt) -> {
buttonEvent(evt, true);
});
}
renderer = new HtmlRenderer(this, g); renderer = new HtmlRenderer(this, g);
initialized = true; initialized = true;
if (onInitialized != null) if (onInitialized != null)
onInitialized.run(); onInitialized.run();
} }
private void buttonEvent(Event evt, boolean released) {
evt.preventDefault();
final EventTarget target = evt.getCurrentTarget();
final HTMLButtonElement button = target.cast();
new Thread(() -> {
try {
if (button.hasAttribute("keycode") && button.getAttribute("keycode").contains(",")) {
final String code = button.getAttribute("keycode");
final String[] coordinates = code.split(",", 2);
final boolean removeshift = Keyboard.shift && Integer.parseInt(coordinates[0]) != 0 && Integer.parseInt(coordinates[1]) != 0;
final boolean removealpha = Keyboard.alpha && Integer.parseInt(coordinates[0]) != 0 && Integer.parseInt(coordinates[1]) != 1;
Keyboard.keyRaw(Integer.parseInt(coordinates[0]), Integer.parseInt(coordinates[1]), released);
if (released) {
if (removeshift)
Keyboard.keyRaw(0, 0, false);
if (removealpha)
Keyboard.keyRaw(0, 1, false);
}
} else if (Keyboard.alpha && !Keyboard.shift) {
if (button.hasAttribute("keycodea")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodea")), released);
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), released);
}
} else if (!Keyboard.alpha && Keyboard.shift) {
if (button.hasAttribute("keycodes")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodes")), released);
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), released);
}
} else if (Keyboard.alpha && Keyboard.shift) {
if (button.hasAttribute("keycodesa")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodesa")), released);
} else if (button.hasAttribute("keycodes")) {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycodes")), released);
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), released);
}
} else {
Keyboard.debugKey(Integer.parseInt(button.getAttribute("keycode")), released);
}
} catch (final Exception ex) {
ex.printStackTrace();
}
}).start();
}
@JSBody(params = {}, script = "return CSS.supports(\"zoom:2\")") @JSBody(params = {}, script = "return CSS.supports(\"zoom:2\")")
private static native boolean suppportsZoom(); private static native boolean suppportsZoom();