1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-04 11:02:54 +00:00

Use Minecraft's method for checking paste events

Fixes #1473.

There's an argument we should use Screen.hasControlDown() (which handles
Cmd vs Ctrl) instead of checking the modifiers, but we then need to
update all the translation strings, and I'm not convinced it's worth it
right now.
This commit is contained in:
Jonathan Coates 2023-06-08 18:48:50 +01:00
parent e157978afc
commit cba207d62d
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06

View File

@ -15,6 +15,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget; import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarratedElementType; import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput; import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
@ -81,6 +82,11 @@ public class TerminalWidget extends AbstractWidget {
@Override @Override
public boolean keyPressed(int key, int scancode, int modifiers) { public boolean keyPressed(int key, int scancode, int modifiers) {
if (key == GLFW.GLFW_KEY_ESCAPE) return false; if (key == GLFW.GLFW_KEY_ESCAPE) return false;
if (Screen.isPaste(key)) {
paste();
return true;
}
if ((modifiers & GLFW.GLFW_MOD_CONTROL) != 0) { if ((modifiers & GLFW.GLFW_MOD_CONTROL) != 0) {
switch (key) { switch (key) {
case GLFW.GLFW_KEY_T -> { case GLFW.GLFW_KEY_T -> {
@ -92,13 +98,25 @@ public class TerminalWidget extends AbstractWidget {
case GLFW.GLFW_KEY_R -> { case GLFW.GLFW_KEY_R -> {
if (rebootTimer < 0) rebootTimer = 0; if (rebootTimer < 0) rebootTimer = 0;
} }
case GLFW.GLFW_KEY_V -> { }
// Ctrl+V for paste }
if (key >= 0 && terminateTimer < KEY_SUPPRESS_DELAY && rebootTimer < KEY_SUPPRESS_DELAY && shutdownTimer < KEY_SUPPRESS_DELAY) {
// Queue the "key" event and add to the down set
var repeat = keysDown.get(key);
keysDown.set(key);
computer.keyDown(key, repeat);
}
return true;
}
private void paste() {
var clipboard = Minecraft.getInstance().keyboardHandler.getClipboard(); var clipboard = Minecraft.getInstance().keyboardHandler.getClipboard();
if (clipboard != null) {
// Clip to the first occurrence of \r or \n // Clip to the first occurrence of \r or \n
var newLineIndex1 = clipboard.indexOf("\r"); var newLineIndex1 = clipboard.indexOf('\r');
var newLineIndex2 = clipboard.indexOf("\n"); var newLineIndex2 = clipboard.indexOf('\n');
if (newLineIndex1 >= 0 && newLineIndex2 >= 0) { if (newLineIndex1 >= 0 && newLineIndex2 >= 0) {
clipboard = clipboard.substring(0, Math.min(newLineIndex1, newLineIndex2)); clipboard = clipboard.substring(0, Math.min(newLineIndex1, newLineIndex2));
} else if (newLineIndex1 >= 0) { } else if (newLineIndex1 >= 0) {
@ -114,21 +132,6 @@ public class TerminalWidget extends AbstractWidget {
if (clipboard.length() > 512) clipboard = clipboard.substring(0, 512); if (clipboard.length() > 512) clipboard = clipboard.substring(0, 512);
computer.queueEvent("paste", new Object[]{ clipboard }); computer.queueEvent("paste", new Object[]{ clipboard });
} }
return true;
}
}
}
}
if (key >= 0 && terminateTimer < KEY_SUPPRESS_DELAY && rebootTimer < KEY_SUPPRESS_DELAY && shutdownTimer < KEY_SUPPRESS_DELAY) {
// Queue the "key" event and add to the down set
var repeat = keysDown.get(key);
keysDown.set(key);
computer.keyDown(key, repeat);
}
return true;
} }
@Override @Override