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

Feature request - Intercept certain keypresses in text input fields? #68

Closed
JarrettBillingsley opened this issue Oct 28, 2014 · 11 comments

Comments

@JarrettBillingsley
Copy link
Contributor

I was looking to make a sort of script console with ImGui but as it is the text input is pretty un-customizable. I'd like to be able to hit the up arrow to go through history (which I could manage myself, no need to put input history in ImGui) and use tab for autocompletion (again, I can handle that).

InputText looks for a bunch of specific keys with IsKeyPressedMap() and then delegates them to edit_state or the stb_textedit control. Obviously that's where the customized keys need to be intercepted. Maybe it could take a structure that holds two arrays, one that flags which keys have custom behavior, and one to put the state of the key in:

struct InputTextKeyOverrides
{
    bool IsOverridden[ImGuiKey_Count];
    bool IsPressed[ImGuiKey_Count];
};

...

bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, InputTextKeyOverrides* overrides)
{
...

if (IsKeyPressedMap(ImGuiKey_UpArrow)) {
    if(overrides->IsOverridden[ImGuiKey_UpArrow])
        overrides->IsKeyPressed[ImGuiKey_UpArrow] = true;
    else
        edit_state.OnKeyboardPressed(STB_TEXTEDIT_K_UP | k_mask);
}

And then user code would check for a keypress after calling InputText and handle it appropriately. (Would be nice to have an ImGuiKey_Tab for this as well.)

I dunno, just a thought.

@xythobuz
Copy link
Contributor

👍 I have exactly the same problem trying to implement a console.

@ocornut
Copy link
Owner

ocornut commented Oct 28, 2014

I'll have to think how to handle that. To handle cursor-position based completion (and cursor preserving history) your code will need to have access to data such as cursor position, selection.

Once InputText is able to expose those data into a small structure and read them back if modified then it could just call a callback and you can do what you want within it. So maybe the system doesn't need to be created with keystroke in mind in particular.

Note that if you don't need the cursor position you could probably get away with just using a function that can tell you if the current widget has focus, and then run your code after. This function is currently missing in ImGui but trivial to implement so I could add that as a first step.

What do you mean by "(Would be nice to have an ImGuiKey_Tab for this as well,)" ?

@JarrettBillingsley
Copy link
Contributor Author

What do you mean by "(Would be nice to have an ImGuiKey_Tab for this as well,)" ?

Oh, I mean currently Tab is treated as a "text" character and wouldn't be intercepted by the code in InputText. But I suppose the "text input" loop could check for '\t' as well.

@ocornut
Copy link
Owner

ocornut commented Oct 28, 2014

Yes, however this is implemented it would have to work for TAB obviously.

@ocornut
Copy link
Owner

ocornut commented Oct 29, 2014

I am now working on implementing a nice console with those example as a test ground for adding the missing features.

@JarrettBillingsley
Copy link
Contributor Author

Yay!

On Wed, Oct 29, 2014 at 5:19 AM, omar notifications@github.com wrote:

I am now working on implementing a nice console with those example as a
test ground for adding the missing features.


Reply to this email directly or view it on GitHub
#68 (comment).

@ocornut
Copy link
Owner

ocornut commented Nov 7, 2014

Not much progress on the actual requests (history/completion) but I have implemented a basic console (that doesn't have those features yet!). However toward doing this I already made a few others small changes/additions to the library.
Stay tuned!

edit the example in included as part of the demo window.

imgui_console_wip

@JarrettBillingsley
Copy link
Contributor Author

Coool! :D

@ocornut
Copy link
Owner

ocornut commented Nov 23, 2014

I have added the callback options now.

( It was messier than initially expected because the text input manipulates wchar internally and I wanted to provide UTF-8 to the user because that is what the user provides as input. With variable-size characters in UTF-8 have to convert thing like cursor-pos and selection range from character-to-byte and vice-versa, along with the text itself, and InputText() has a bunch of hoops already. I may want to clean that mess in the future. )

I have implemented a demo of completion in the console.
If you TAB It lists commands, input "h" and TAB it replace with "HELP" (the example replace casing in what you typed). If there are multiple matches it displays the matches and complete as much as possible, so "C" become "CL".

imgui_completion

I have not implemented history in the demo yet but the callback should be there (untested).

The console demo app is 170 lines which is starting to be a little too much but that is due to the fancy completion scheme. It might be time to move sample code to a separate location? hmm?

@ocornut
Copy link
Owner

ocornut commented Dec 28, 2014

Closing this now. Will add a sample code for History using up/down later on but the feature is available.

@ocornut ocornut closed this as completed Dec 28, 2014
@ocornut
Copy link
Owner

ocornut commented Dec 30, 2014

fyi - the Console example now handles history with Up/Down arrows along with a command "History.

tom-seddon pushed a commit to tom-seddon/imgui that referenced this issue Dec 17, 2023
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

3 participants