Replies: 1 comment
-
|
Something like this worked for me: #include "raylib.h"
typedef struct {
bool LoggerTextInputEditMode;
char LoggerTextInputText[128];
} GuiLayoutState;
GuiLayoutState global_state = {0};
void InitGuiLayout(float screen_width, float screen_height, float padding);
void InitGuiLayout(float screen_width, float screen_height, float padding) {
global_state.LoggerTextInputEditMode = false;
strcpy(global_state.LoggerTextInputText, "Placeholder");
}
// Forward declaration of ShowKeyboard
void ShowKeyboard(bool show);
void GuiLayout() {
if (GuiTextBox(state->layoutRecs[LO_LOGGER_TEXT_INPUT], state->LoggerTextInputText, 128, state->LoggerTextInputEditMode)) {
state->LoggerTextInputEditMode = !state->LoggerTextInputEditMode;
ShowKeyboard(state->LoggerTextInputEditMode);
}
}
// ---------- keyboard input handling ----------
#define MAX_INPUT_CHARS 32
#define MAX_INPUT_KEYS 32
static int inputChars[MAX_INPUT_CHARS];
static int inputCharsCount = 0;
static int inputKeys[MAX_INPUT_KEYS];
static int inputKeysCount = 0;
static int activeKeysThisFrame[MAX_INPUT_KEYS];
static int activeKeysCount = 0;
void PushChar(int character) {
if (inputCharsCount < MAX_INPUT_CHARS) {
inputChars[inputCharsCount++] = character;
}
}
void PushKey(int key) {
if (inputKeysCount < MAX_INPUT_KEYS) {
inputKeys[inputKeysCount++] = key;
}
}
void UpdateInputQueues(void) {
activeKeysCount = 0;
if (inputKeysCount > 0) {
activeKeysThisFrame[activeKeysCount++] = inputKeys[0];
for (int i = 0; i < inputKeysCount - 1; i++) inputKeys[i] = inputKeys[i + 1];
inputKeysCount--;
}
}
int CustomGetCharPressed(void) {
if (inputCharsCount > 0) {
int character = inputChars[0];
for (int i = 0; i < inputCharsCount - 1; i++) inputChars[i] = inputChars[i + 1];
inputCharsCount--;
return character;
}
return GetCharPressed();
}
bool CustomIsKeyPressed(int key) {
for (int i = 0; i < activeKeysCount; i++) {
if (activeKeysThisFrame[i] == key) return true;
}
return IsKeyPressed(key);
}
#if !defined(GUI_KEY_DOWN)
#define GUI_KEY_DOWN(key) IsKeyDown(key)
#endif
#define GUI_INPUT_KEY CustomGetCharPressed()
#define GUI_KEY_PRESSED(key) CustomIsKeyPressed(key)
// ---------/ keyboard input handling /---------
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
// Forward declaration of GetAndroidApp (defined in rcore_android.c)
struct android_app *GetAndroidApp(void);
void ShowKeyboard(bool show) {
struct android_app *app = GetAndroidApp();
if (!app) return;
JNIEnv *env = NULL;
JavaVM *vm = app->activity->vm;
(*vm)->AttachCurrentThread(vm, &env, NULL);
jobject activity = app->activity->clazz;
jclass activityClass = (*env)->GetObjectClass(env, activity);
jmethodID showKeyboardMethod = (*env)->GetMethodID(env, activityClass, "showKeyboard", "(Z)V");
if (showKeyboardMethod) {
(*env)->CallVoidMethod(env, activity, showKeyboardMethod, (jboolean)show);
}
(*env)->DeleteLocalRef(env, activityClass);
(*vm)->DetachCurrentThread(vm);
}
JNIEXPORT void JNICALL
Java_io_dvubajby_rgkhbmnlqk_MainActivity_nativePushInput(JNIEnv *env, jobject this, jint keyCode, jint character) {
if (character > 0) PushChar(character);
// Map Android KeyCodes to Raylib Keys
switch (keyCode) {
case 67: PushKey(KEY_BACKSPACE); break; // AKEYCODE_DEL
case 66: PushKey(KEY_ENTER); break; // AKEYCODE_ENTER
case 21: PushKey(KEY_LEFT); break; // AKEYCODE_DPAD_LEFT
case 22: PushKey(KEY_RIGHT); break; // AKEYCODE_DPAD_RIGHT
case 112: PushKey(KEY_DELETE); break; // AKEYCODE_FORWARD_DEL
case 122: PushKey(KEY_HOME); break; // AKEYCODE_MOVE_HOME
case 123: PushKey(KEY_END); break; // AKEYCODE_MOVE_END
default: break;
}
}
int main(int argc, char *argv[]) {
InitWindow(0, 0, "native window");
float screen_width = (float) GetScreenWidth();
float screen_height = (float) GetScreenHeight();
float padding = screen_height * .02f;
GuiLoadStyleCrimsonKnight();
InitGuiLayout(screen_width, screen_height, padding);
SetTargetFPS(90);
while (!WindowShouldClose() && !global_state.WindowClosed) {
UpdateInputQueues();
UpdateGui();
BeginDrawing();
ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
GuiLayout();
EndDrawing();
}
CloseWindow();
exit(0);
return 0;
}package io.dvubajby.rgkhbmnlqk;
import android.app.NativeActivity;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
public class MainActivity extends NativeActivity {
static {
System.loadLibrary("rgkhbmnlqk");
}
public native void updateNativeStatus(String status);
public native void nativePushInput(int key, int character);
public void showKeyboard(boolean show) {
runOnUiThread(() -> {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (show) {
imm.showSoftInput(getWindow().getDecorView(), InputMethodManager.SHOW_IMPLICIT);
} else {
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
});
}
@Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
int character = event.getUnicodeChar();
nativePushInput(keyCode, character);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyMultiple(int keyCode, int repeatCount, android.view.KeyEvent event) {
String characters = event.getCharacters();
if (characters != null) {
for (int i = 0; i < characters.length(); i++) {
nativePushInput(0, characters.charAt(i));
}
}
return super.onKeyMultiple(keyCode, repeatCount, event);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Does anyone have experience getting input from a soft keyboard?
This is my current setup:
main.c:
NativeLoader.java:
But this doesn't seem to be working.
PS: I edited the raylib.h header and added the GetAndroidApp() definition
EDIT: It is working, but it is super slow and unresponsible
Beta Was this translation helpful? Give feedback.
All reactions