Skip to content

Commit

Permalink
tweaks to input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
GiantLuigi4 committed Jun 8, 2023
1 parent d462cc2 commit 81ebbbf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
27 changes: 19 additions & 8 deletions src/main/java/net/montoyo/mcef/example/BrowserScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void init() {
browser = api.createBrowser((urlToLoad == null) ? MCEF.HOME_PAGE : urlToLoad, false);
browser.resize(minecraft.getWindow().getWidth(), minecraft.getWindow().getHeight() - scaleY(20));
urlToLoad = null;
browser.allowCursorChanges(true);
}

//Resize the browser if window size changed
Expand Down Expand Up @@ -145,15 +146,21 @@ public void render(PoseStack matrices, int mouseX, int mouseY, float delta) {

@Override
public void onClose() {
// Keyboard.enableRepeatEvents(false);
super.onClose();
}

// onClose is less reliable
@Override
public void removed() {
//Make sure to close the browser when you don't need it anymore.
if (!ExampleMod.INSTANCE.hasBackup() && browser != null) {
browser.allowCursorChanges(false);
browser.close();
}

// Keyboard.enableRepeatEvents(false);
super.onClose();
super.removed();
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
return this.keyChanged(keyCode, scanCode, modifiers, true) || super.keyPressed(keyCode, scanCode, modifiers);
Expand All @@ -167,7 +174,7 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
@Override
public boolean charTyped(char codePoint, int modifiers) {
if (browser != null && !url.isFocused()) {
browser.injectKeyTyped((int) codePoint, modifiers);
browser.injectKeyTyped(codePoint, modifiers);
return true;
} else {
return super.charTyped(codePoint, modifiers);
Expand Down Expand Up @@ -199,7 +206,8 @@ public boolean keyChanged(int keyCode, int scanCode, int modifiers, boolean pres
char key = keystr.charAt(keystr.length() - 1);

if(keystr.equals("Enter")) {
key = '\r';
keyCode = 10;
key = '\n';
}

if (browser != null && !focused) { //Inject events into browser
Expand All @@ -208,8 +216,11 @@ public boolean keyChanged(int keyCode, int scanCode, int modifiers, boolean pres
else
browser.injectKeyReleasedByKeyCode(keyCode, key, modifiers);

if(key == '\r')
browser.injectKeyTyped(key, 0);
if (pressed && key == '\n')
if (
// (modifiers & GLFW.GLFW_MOD_SHIFT) != 0
modifiers != 0
) browser.injectKeyTyped('\r', modifiers);
return true; // Something did happen
}

Expand Down
28 changes: 19 additions & 9 deletions src/main/java/org/cef/browser/CefBrowserOsr.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void injectMouseMove(int x, int y, int mods, boolean left) {

@Override
public void injectMouseButton(int x, int y, int mods, int btn, boolean pressed, int ccnt) {
if(btn != 3) {
if (btn != 3) {
MouseEvent ev = new MouseEvent(dc_, pressed ? MouseEvent.MOUSE_PRESSED : MouseEvent.MOUSE_RELEASED, 0, mods, x, y, ccnt, false, btn);
sendMouseEvent(ev);
} else {
Expand All @@ -180,10 +180,10 @@ public void injectMouseButton(int x, int y, int mods, int btn, boolean pressed,

public int remapModifiers(int mods) {
int vkMods = 0;
if ((mods & GLFW_MOD_CONTROL) != 0) vkMods |= KeyEvent.CTRL_DOWN_MASK;
if ((mods & GLFW_MOD_ALT) != 0) vkMods |= KeyEvent.ALT_DOWN_MASK;
if ((mods & GLFW_MOD_SHIFT) != 0) vkMods |= KeyEvent.SHIFT_DOWN_MASK;
if ((mods & GLFW_MOD_SUPER) != 0) vkMods |= KeyEvent.META_DOWN_MASK;
if ((mods & GLFW_MOD_CONTROL) != 0) vkMods |= KeyEvent.CTRL_DOWN_MASK | KeyEvent.CTRL_MASK;
if ((mods & GLFW_MOD_ALT) != 0) vkMods |= KeyEvent.ALT_DOWN_MASK | KeyEvent.ALT_MASK;
if ((mods & GLFW_MOD_SHIFT) != 0) vkMods |= KeyEvent.SHIFT_DOWN_MASK | KeyEvent.SHIFT_MASK;
if ((mods & GLFW_MOD_SUPER) != 0) vkMods |= KeyEvent.META_DOWN_MASK | KeyEvent.META_MASK;
return vkMods;
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public static int getChar(int keyCode, int scanCode, int mod) {
if (keyCode == GLFW_KEY_RIGHT_CONTROL) return '\uFFFF';
switch (keyCode) {
case GLFW_KEY_ENTER, GLFW_KEY_KP_ENTER, VK_ENTER:
return '\r';
return '\n';
case GLFW_KEY_SPACE:
return 32;
case GLFW_KEY_BACKSPACE:
Expand Down Expand Up @@ -310,7 +310,14 @@ public void injectKeyPressedByKeyCode(int key, char c, int mods) {
}

default -> {
KeyEvent ev = UnsafeUtil.makeEvent(dc_, remapKeycode(key, c, mods), c, KEY_LOCATION_UNKNOWN, KEY_PRESSED, 0, remapModifiers(mods), mapScanCode(key, c));
int raw = key;
if (c == '\n') raw = 13;

KeyEvent ev = UnsafeUtil.makeEvent(dc_, remapKeycode(key, c, mods), c, KEY_LOCATION_UNKNOWN, KEY_PRESSED, 0, remapModifiers(mods), mapScanCode(key, c), raw);
if (ev.getKeyChar() == '\n') {
System.out.println(ev);
}

sendKeyEvent(ev);
}
}
Expand All @@ -328,7 +335,7 @@ public void injectKeyTyped(int key, int mods) {
if (key == GLFW_KEY_LEFT && canGoBack()) return;
else if (key == GLFW_KEY_RIGHT && canGoForward()) return;
}

char c = (char) key;
int keyRemapped = remapKeycode(key, (char) key, mods);
if (key != VK_UNDEFINED) {
Expand All @@ -339,7 +346,10 @@ public void injectKeyTyped(int key, int mods) {
}

case VK_ENTER, GLFW_KEY_ENTER, GLFW_KEY_KP_ENTER -> {
KeyEvent ev = UnsafeUtil.makeEvent(dc_, key, '\r', KEY_LOCATION_UNKNOWN, KEY_TYPED, 0, remapModifiers(mods), mapScanCode(key, c));
int raw = key;
if (c == '\n') raw = 13;

KeyEvent ev = UnsafeUtil.makeEvent(dc_, key, '\n', KEY_LOCATION_UNKNOWN, KEY_TYPED, 0, remapModifiers(mods), mapScanCode(key, c), raw);
sendKeyEvent(ev);
}

Expand Down

0 comments on commit 81ebbbf

Please sign in to comment.