Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.input.CharInput;
import net.minecraft.client.input.KeyInput;

import net.fabricmc.fabric.api.event.Event;
Expand Down Expand Up @@ -50,7 +51,7 @@ public static Event<AllowKeyPress> allowKeyPress(Screen screen) {
}

/**
* An event that is called before a key press is processed for a screen.
* An event called before a key press is processed for a screen.
*
* @return the event
*/
Expand All @@ -61,7 +62,7 @@ public static Event<BeforeKeyPress> beforeKeyPress(Screen screen) {
}

/**
* An event that is called after a key press is processed for a screen.
* An event called after a key press is processed for a screen.
*
* @return the event
*/
Expand All @@ -83,7 +84,7 @@ public static Event<AllowKeyRelease> allowKeyRelease(Screen screen) {
}

/**
* An event that is called after the release of a key is processed for a screen.
* An event called before the release of a key is processed for a screen.
*
* @return the event
*/
Expand All @@ -94,7 +95,7 @@ public static Event<BeforeKeyRelease> beforeKeyRelease(Screen screen) {
}

/**
* An event that is called after the release a key is processed for a screen.
* An event called after the release a key is processed for a screen.
*
* @return the event
*/
Expand All @@ -104,6 +105,39 @@ public static Event<AfterKeyRelease> afterKeyRelease(Screen screen) {
return ScreenExtensions.getExtensions(screen).fabric_getAfterKeyReleaseEvent();
}

/**
* An event that checks if typing a character should be allowed.
*
* @return the event
*/
public static Event<AllowCharType> allowCharType(Screen screen) {
Objects.requireNonNull(screen, "Screen cannot be null");

return ScreenExtensions.getExtensions(screen).fabric_getAllowCharTypeEvent();
}

/**
* An event called before typing a character is processed for a screen.
*
* @return the event
*/
public static Event<BeforeCharType> beforeCharType(Screen screen) {
Objects.requireNonNull(screen, "Screen cannot be null");

return ScreenExtensions.getExtensions(screen).fabric_getBeforeCharTypeEvent();
}

/**
* An event called after typing a character is processed for a screen.
*
* @return the event
*/
public static Event<AfterCharType> afterCharType(Screen screen) {
Objects.requireNonNull(screen, "Screen cannot be null");

return ScreenExtensions.getExtensions(screen).fabric_getAfterCharTypeEvent();
}

private ScreenKeyboardEvents() {
}

Expand Down Expand Up @@ -180,4 +214,41 @@ public interface AfterKeyRelease {
*/
void afterKeyRelease(Screen screen, KeyInput context);
}

@FunctionalInterface
public interface AllowCharType {
/**
* Checks if typing a character should be allowed.
*
* @param context the context of typing the character, containing the codepoint and modifiers
* @return whether the character should be typed
* @see CharInput#asString()
* @see <a href="https://www.glfw.org/docs/3.3/group__mods.html">Modifier key flags</a>
*/
boolean allowCharType(Screen screen, CharInput context);
}

@FunctionalInterface
public interface BeforeCharType {
/**
* Called before a character is typed.
*
* @param context the context of typing the character, containing the codepoint and modifiers
* @see CharInput#asString()
* @see <a href="https://www.glfw.org/docs/3.3/group__mods.html">Modifier key flags</a>
*/
void beforeCharType(Screen screen, CharInput context);
}

@FunctionalInterface
public interface AfterCharType {
/**
* Called after a character is typed.
*
* @param context the context of typing the character, containing the codepoint and modifiers
* @see CharInput#asString()
* @see <a href="https://www.glfw.org/docs/3.3/group__mods.html">Modifier key flags</a>
*/
void afterCharType(Screen screen, CharInput context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,34 @@ public static Event<ScreenKeyboardEvents.AfterKeyRelease> createAfterKeyReleaseE
});
}

public static Event<ScreenKeyboardEvents.AllowCharType> createAllowCharTypeEvent() {
return EventFactory.createArrayBacked(ScreenKeyboardEvents.AllowCharType.class, callbacks -> (screen, context) -> {
for (ScreenKeyboardEvents.AllowCharType callback : callbacks) {
if (!callback.allowCharType(screen, context)) {
return false;
}
}

return true;
});
}

public static Event<ScreenKeyboardEvents.BeforeCharType> createBeforeCharTypeEvent() {
return EventFactory.createArrayBacked(ScreenKeyboardEvents.BeforeCharType.class, callbacks -> (screen, context) -> {
for (ScreenKeyboardEvents.BeforeCharType callback : callbacks) {
callback.beforeCharType(screen, context);
}
});
}

public static Event<ScreenKeyboardEvents.AfterCharType> createAfterCharTypeEvent() {
return EventFactory.createArrayBacked(ScreenKeyboardEvents.AfterCharType.class, callbacks -> (screen, context) -> {
for (ScreenKeyboardEvents.AfterCharType callback : callbacks) {
callback.afterCharType(screen, context);
}
});
}

// Mouse Events

public static Event<ScreenMouseEvents.AllowMouseClick> createAllowMouseClickEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ static ScreenExtensions getExtensions(Screen screen) {

Event<ScreenKeyboardEvents.AfterKeyRelease> fabric_getAfterKeyReleaseEvent();

Event<ScreenKeyboardEvents.AllowCharType> fabric_getAllowCharTypeEvent();

Event<ScreenKeyboardEvents.BeforeCharType> fabric_getBeforeCharTypeEvent();

Event<ScreenKeyboardEvents.AfterCharType> fabric_getAfterCharTypeEvent();

// Mouse

Event<ScreenMouseEvents.AllowMouseClick> fabric_getAllowMouseClickEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import net.minecraft.client.Keyboard;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.input.CharInput;
import net.minecraft.client.input.KeyInput;

import net.fabricmc.fabric.api.client.screen.v1.ScreenKeyboardEvents;
Expand All @@ -36,7 +37,7 @@ private boolean invokeKeyPressedEvents(Screen screen, KeyInput ctx, Operation<Bo

if (screen != null) {
if (!ScreenKeyboardEvents.allowKeyPress(screen).invoker().allowKeyPress(screen, ctx)) {
// Set this press action as handled
// Set this action as handled
return true;
}

Expand All @@ -59,7 +60,7 @@ private boolean invokeKeyReleasedEvents(Screen screen, KeyInput ctx, Operation<B

if (screen != null) {
if (!ScreenKeyboardEvents.allowKeyRelease(screen).invoker().allowKeyRelease(screen, ctx)) {
// Set this release action as handled
// Set this action as handled
return true;
}

Expand All @@ -74,4 +75,27 @@ private boolean invokeKeyReleasedEvents(Screen screen, KeyInput ctx, Operation<B

return result;
}

@WrapOperation(method = "onChar", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;charTyped(Lnet/minecraft/client/input/CharInput;)Z"))
private boolean invokeCharTypedEvents(Screen screen, CharInput charInput, Operation<Boolean> operation) {
// The screen passed to events is the same as the screen the handler method is called on,
// regardless of whether the screen changes within the handler or event invocations.

if (screen != null) {
if (!ScreenKeyboardEvents.allowCharType(screen).invoker().allowCharType(screen, charInput)) {
// Set this action as handled
return true;
}

ScreenKeyboardEvents.beforeCharType(screen).invoker().beforeCharType(screen, charInput);
}

boolean result = operation.call(screen, charInput);

if (screen != null) {
ScreenKeyboardEvents.afterCharType(screen).invoker().afterCharType(screen, charInput);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ abstract class ScreenMixin implements ScreenExtensions {
private Event<ScreenKeyboardEvents.BeforeKeyRelease> beforeKeyReleaseEvent;
@Unique
private Event<ScreenKeyboardEvents.AfterKeyRelease> afterKeyReleaseEvent;
@Unique
private Event<ScreenKeyboardEvents.AllowCharType> allowCharTypeEvent;
@Unique
private Event<ScreenKeyboardEvents.BeforeCharType> beforeCharTypeEvent;
@Unique
private Event<ScreenKeyboardEvents.AfterCharType> afterCharTypeEvent;

// Mouse
@Unique
Expand Down Expand Up @@ -152,6 +158,9 @@ private void beforeInit(MinecraftClient client, int width, int height) {
this.allowKeyReleaseEvent = ScreenEventFactory.createAllowKeyReleaseEvent();
this.beforeKeyReleaseEvent = ScreenEventFactory.createBeforeKeyReleaseEvent();
this.afterKeyReleaseEvent = ScreenEventFactory.createAfterKeyReleaseEvent();
this.allowCharTypeEvent = ScreenEventFactory.createAllowCharTypeEvent();
this.beforeCharTypeEvent = ScreenEventFactory.createBeforeCharTypeEvent();
this.afterCharTypeEvent = ScreenEventFactory.createAfterCharTypeEvent();

// Mouse
this.allowMouseClickEvent = ScreenEventFactory.createAllowMouseClickEvent();
Expand Down Expand Up @@ -256,6 +265,21 @@ public Event<ScreenKeyboardEvents.AfterKeyRelease> fabric_getAfterKeyReleaseEven
return ensureEventsAreInitialized(this.afterKeyReleaseEvent);
}

@Override
public Event<ScreenKeyboardEvents.AllowCharType> fabric_getAllowCharTypeEvent() {
return ensureEventsAreInitialized(this.allowCharTypeEvent);
}

@Override
public Event<ScreenKeyboardEvents.BeforeCharType> fabric_getBeforeCharTypeEvent() {
return ensureEventsAreInitialized(this.beforeCharTypeEvent);
}

@Override
public Event<ScreenKeyboardEvents.AfterCharType> fabric_getAfterCharTypeEvent() {
return ensureEventsAreInitialized(this.afterCharTypeEvent);
}

// Mouse

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ private void afterInitScreen(MinecraftClient client, Screen screen, int windowWi
.orElseThrow(() -> new AssertionError("Failed to find the \"Stop Sound\" button in the screen's elements"));

ScreenKeyboardEvents.allowKeyPress(screen).register((_screen, context) -> {
LOGGER.info("After Pressed, Context: {}", context);
LOGGER.info("Allow Key Press, Context: {}", context);
return true; // Let actions continue
});

ScreenKeyboardEvents.afterKeyPress(screen).register((_screen, context) -> {
LOGGER.warn("Pressed, Context: {}", context);
LOGGER.warn("After Key Press, Context: {}", context);
});

ScreenKeyboardEvents.allowCharType(screen).register((_screen, context) -> {
LOGGER.warn("Allow Char Type, Context: {}, Character: {}", context, context.asString());
return true;
});
} else if (screen instanceof CreativeInventoryScreen) {
Screens.getButtons(screen).add(new TestButtonWidget());
Expand Down
Loading