Jump to content

Recommended Posts

Posted (edited)

Hi,

Is it possible to simulate a shift press? I need to get hasShiftDown() true from Screen.class (try to get a tooltip with shift press and without from itemStack#getTooltipLines()). 

Also, I tried with follow, but it did not trigger:

// Simulate holding down the shift key
Minecraft.getInstance().player.setShiftKeyDown(true);
Minecraft.getInstance().options.keyShift.setDown(true);
KeyMapping.set(Minecraft.getInstance().options.keyShift.getKey(), true);

Leave code here, maybe some still need it.

ScreenMixin.class

import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import ShiftStateUtility;

@Mixin(Screen.class)
public class ScreenMixin {

    @Inject(method = "hasShiftDown", at = @At("HEAD"), cancellable = true)
    private static void onHasShiftDown(CallbackInfoReturnable<Boolean> cir) {
        if(ShiftStateUtility.shouldShiftBeDown) {
            cir.setReturnValue(ShiftStateUtility.shouldShiftBeDown);
        } else {
            // Call a method that checks the actual key state.
            cir.setReturnValue(checkActualShiftState());
        }
    }

    @Inject(method = "hasControlDown", at = @At("HEAD"), cancellable = true)
    private static void hasControlDown(CallbackInfoReturnable<Boolean> cir) {
        if(ShiftStateUtility.shouldControlBeDown) {
            cir.setReturnValue(ShiftStateUtility.shouldControlBeDown);
        } else {
            // Call a method that checks the actual key state.
            cir.setReturnValue(checkActualControlState());
        }
    }

    @Inject(method = "hasAltDown", at = @At("HEAD"), cancellable = true)
    private static void hasAltDown(CallbackInfoReturnable<Boolean> cir) {
        if(ShiftStateUtility.shouldAltBeDown) {
            cir.setReturnValue(ShiftStateUtility.shouldAltBeDown);
        } else {
            // Call a method that checks the actual key state.
            cir.setReturnValue(checkActualAltState());
        }
    }

    private static boolean checkActualShiftState() {
        return InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 340) || InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 344);
    }

    private static boolean checkActualControlState() {
        if (Minecraft.ON_OSX) {
            return InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 343) || InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 347);
        } else {
            return InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 341) || InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 345);
        }
    }

    private static boolean checkActualAltState() {
        return InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 342) || InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), 346);
    }
}

ShiftStateUtility.class

public class ShiftStateUtility {
    public static boolean shouldShiftBeDown = false;
    public static boolean shouldControlBeDown = false;
    public static boolean shouldAltBeDown = false;
}

Usage:

ShiftStateUtility.shouldShiftBeDown = true / false;
ShiftStateUtility.shouldControlBeDown = true / false;
ShiftStateUtility.shouldAltBeDown = true / false;

The default behavior of hasShiftDown, hasAltDown, and hasControlDown still work as default, and you can control if want to simulate a press of any key.

Edited by balex25
Fixed it using Mixin on the hasShiftDown method.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.