How do I query if a binded key is unpressed and how long it has been pressed?

Started by ahznb on

Topic category: Help with Minecraft modding (Java Edition)

Joined Nov 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How do I query if a binded key is unpressed and how long it has been pressed?
Wed, 11/27/2019 - 04:35 (edited)

Hi,

I've created a new key binding procedure. The generated procedure only activates on key press and does not have the pressed "key" as dependency, and there are no preset procedures to query key state. I'd like the procedure to query the key press state, like if it has been released, how long the key has been pressed, etc. How do I do that with minimal coding, or better yet without coding?

Thanks!

Edited by ahznb on Wed, 11/27/2019 - 04:35
Joined Nov 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This is what I did in the…
Mon, 12/30/2019 - 09:37

This is what I did in the mean time before the new feature is rolled out:

	@SubscribeEvent
	@SideOnly(Side.CLIENT)
	public void onKeyInput(InputEvent.KeyInputEvent event) {
		if (!FMLClientHandler.instance().isGUIOpen(GuiChat.class)) {
			if (org.lwjgl.input.Keyboard.getEventKey() == keys.getKeyCode() || org.lwjgl.input.Keyboard.isKeyDown(keys.getKeyCode()))  {
				myMod.PACKET_HANDLER.sendToServer(new KeyBindingPressedMessage(org.lwjgl.input.Keyboard.isKeyDown(keys.getKeyCode())));
			}
            // do this to maintain repeated key events. 
            // for some reason vanilla will turn this off when you open the gui
			if (!org.lwjgl.input.Keyboard.areRepeatEventsEnabled()) {
				org.lwjgl.input.Keyboard.enableRepeatEvents(true);
			}
		}
	}

	public static class KeyBindingPressedMessageHandler implements IMessageHandler<KeyBindingPressedMessage, IMessage> {
		@Override
		public IMessage onMessage(KeyBindingPressedMessage message, MessageContext context) {
			EntityPlayerMP entity = context.getServerHandler().player;
			entity.getServerWorld().addScheduledTask(() -> {
				World world = entity.world;
				int x = (int) entity.posX;
				int y = (int) entity.posY;
				int z = (int) entity.posZ;
				boolean is_pressed = (boolean) message.is_pressed;
				// security measure to prevent arbitrary chunk
				// generation
					if (!world.isBlockLoaded(new BlockPos(x, y, z)))
						return;
					{
						java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
						$_dependencies.put("is_pressed", is_pressed);
						$_dependencies.put("entity", entity);
						$_dependencies.put("world", world);
						$_dependencies.put("x", x);
						$_dependencies.put("y", y);
						$_dependencies.put("z", z);
						MCreatorOnKeyPressedReleased.executeProcedure($_dependencies);
					}
				});
			return null;
		}
	}

	public static class KeyBindingPressedMessage implements IMessage {
		boolean is_pressed;

		public KeyBindingPressedMessage() {
		}

		public KeyBindingPressedMessage(boolean is_pressed) {
			this.is_pressed = is_pressed;
		}
		
		@Override
		public void toBytes(io.netty.buffer.ByteBuf buf) {
			buf.writeBoolean(is_pressed);
		}

		@Override
		public void fromBytes(io.netty.buffer.ByteBuf buf) {
			is_pressed = buf.readBoolean();
		}
	}

This will call your keybinding proc repeatedly when your key is pressed or released. It will also pass to your proc a dependency "is_pressed".