Skip to content

Commit

Permalink
feat(game.input): KeyboardUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Oct 27, 2022
1 parent 709648d commit 095affc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
@@ -0,0 +1,27 @@
package org.auioc.mcmod.arnicalib.game.input;

import org.auioc.mcmod.arnicalib.base.function.VoidPredicate;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public enum KeyDownRule implements VoidPredicate {

ALWAYS(() -> true), NEVER(() -> false), //
ON_SHIFT_KEY_DOWN(KeyboardUtils::isShiftKeyDown), //
ON_CTRL_KEY_DOWN(KeyboardUtils::isCtrlKeyDown), //
ON_ALT_KEY_DOWN(KeyboardUtils::isAltKeyDown), //
ON_SPACE_KEY_DOWN(KeyboardUtils::isSpaceKeyDown);

private final VoidPredicate predicate;

private KeyDownRule(VoidPredicate predicate) {
this.predicate = predicate;
}

@Override
public boolean test() {
return predicate.test();
}

}
@@ -0,0 +1,34 @@
package org.auioc.mcmod.arnicalib.game.input;

import org.lwjgl.glfw.GLFW;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class KeyboardUtils {

private static final Minecraft MC = Minecraft.getInstance();

public static boolean isKeyDown(int key) {
return InputConstants.isKeyDown(MC.getWindow().getWindow(), key);
}

public static boolean isShiftKeyDown() {
return isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT) || isKeyDown(GLFW.GLFW_KEY_RIGHT_SHIFT);
}

public static boolean isCtrlKeyDown() {
return isKeyDown(GLFW.GLFW_KEY_LEFT_CONTROL) || isKeyDown(GLFW.GLFW_KEY_RIGHT_CONTROL);
}

public static boolean isAltKeyDown() {
return isKeyDown(GLFW.GLFW_KEY_LEFT_ALT) || isKeyDown(GLFW.GLFW_KEY_RIGHT_ALT);
}

public static boolean isSpaceKeyDown() {
return isKeyDown(GLFW.GLFW_KEY_SPACE);
}

}

0 comments on commit 095affc

Please sign in to comment.