Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying what's in an ImGui::Input #7482

Closed
sunnywinterday opened this issue Apr 11, 2024 · 4 comments
Closed

Modifying what's in an ImGui::Input #7482

sunnywinterday opened this issue Apr 11, 2024 · 4 comments

Comments

@sunnywinterday
Copy link

Version/Branch of Dear ImGui:

I believe I cloned around this commit: 286cd5b

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl2.cpp

Compiler, OS:

RaspiOS (basically linux), and just CodeLite IDE

Full config/build information:

No response

Details:

I am working with a keypad module, and I have a python script that works it, and outputs to a file ("keyPress.txt") a 1 and then the value of the key pressed. For example, if I press 4 on the keypad, 14 will be output to the file. The 1 is to tell the main program that we can go ahead and treat the number after the 1 as the button that was pressed. I want to read the file into a char array, and then check for the 1 at the front, if the 1 is there, set the next position in the textbox equal to the 2nd number held in the char array, the button that was actually pressed. Then afterwards move forward one index so that the next position can be set equal to the next button pressed. Currently it seems not to alter the value of the textbox at all, at least it doesn't show on my ImGui window if there is anything in the InputBox. I was wondering if I could please get some more clarity on whether or not this is possible, and if so what the correct method would be.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

#include<fstream>



int main() {

	std::ifstream passFile;
	std::ofstream passFileDelete;
	static char keyPressed[2];
	static char passwordTB[63];
	int pressedCounter = 0;
	int passwordCounter = 0;
	bool show_pass_window = true;

	// start imGui window loop

	if (show_pass_window) {

		passFile.open("keyPress.txt");

		while (!passFile.eof()) {

			passFile >> keyPressed[pressedCounter];
			pressedCounter++;

		}

		passFile.close();


		if (keyPressed[0] == 1) {

			passwordTB[passwordCounter] = keyPressed[1];
			passwordCounter++;

			passFileDelete.open("keyPress.txt", std::ios::out | std::ios::trunc);
                        passFileDelete.close();
		}

		ImGui::Begin("password window", NULL, window_flags);

		ImGui::InputText("##pininput", passwordTB, IM_ARRAYSIZE(passwordTB));

		ImGui::End();

	}
	return 0;
}
@ocornut
Copy link
Owner

ocornut commented Apr 11, 2024

It is a known issue, it has been asked and answered many times here.
InputText() currently doesn't refresh its state from user-buffer when it is being edited (unless read-only).

You can forcefully instruct it to update it with code highlighted here:
#2890 (comment)

@ocornut ocornut closed this as completed Apr 11, 2024
@sunnywinterday
Copy link
Author

Ah I'm sorry, I was really exhausted when I sent in the issue, so I didn't think to check. I apologize and thank you.

@sunnywinterday
Copy link
Author

sunnywinterday commented Apr 11, 2024

Thank you again for pointing me in the right direction. I am getting this error when I try to use this example specifically:

if (ImGuiInputTextState* input_state = ImGui::GetItemID())
    input_state->ReloadUserBufAndSelectAll();

My code is the exact same, and imgui_internal.h is included properly. The error is as follows:

error: invalid conversion from 'ImGuiID' {aka 'unsigned int'} to 'ImGuiInputTextState*' [-fpermissive]


506 |              if (ImGuiInputTextState* input_state = ImGui::GetItemID()) {
                                                                                 ~~~~~~~~~~^~
                                                                                                           |
                                                                                                           ImGuiID {aka unsigned int}

I realize this may be because I am inexperienced, so I appreciate the responses.

@ocornut
Copy link
Owner

ocornut commented Apr 11, 2024

I introduced a typo in the linked post earlier today, look at it again.

The right call is if (ImGuiInputTextState* input_state = ImGui::GetInputTextState(ImGui::GetItemID())) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants