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

Scroll Combo with keyboard / text input #1153

Open
rickyviking opened this issue May 22, 2017 · 6 comments
Open

Scroll Combo with keyboard / text input #1153

rickyviking opened this issue May 22, 2017 · 6 comments
Labels
nav keyboard/gamepad navigation scrolling

Comments

@rickyviking
Copy link

I'm trying to implement a feature to allow the Combo to scroll when pressing a keyboard key.
In particular, change the active entry to the first one starting with the letter typed in (this is quite useful when you have a long combo list).
I thought I should use the IO.InputCharacters API to consume the first character input by user.
To receive and read the input char I'm using:

// capture input while showing the popup
       SetActiveID(id, window);
       g.InputTextState.Id = id;
// ...
// get the first input char if any
       ImWchar inputChar = g.IO.InputCharacters[0];

within the combo popup code and it does the trick, but it disables the following mouse inputs.
What's the correct way to listen to user input in this case?
Thank you

@ocornut
Copy link
Owner

ocornut commented May 22, 2017

Hello,

I don't understand the quoted bits of code out of context, in particular I don't understand the first bit.

Are you trying to do this from within imgui.cpp or from your own code?
Reading from InputCharacters[] when calling ClearInputCharacters() should work. Then in the block of code inside Combo() that test for if (IsPopupOpen(id)) you could modify the current item by search within all items.

Please note that those functions are part of bigger library-wise topics like #787 and #456.

@rickyviking
Copy link
Author

Hi @ocornut

my bad I took it for granted that I was patching imgui.cpp code.
The context is the one you mention reference, inside the test if (IsPopupOpen(id)) I've added the code:

if (IsPopupOpen(id))
{
       // capture input while showing the popup
       SetActiveID(id, window);
       g.InputTextState.Id = id;
       // get the first inputed char if any
       ImWchar inputChar = g.IO.InputCharacters[0];       
       g.IO.ClearInputCharacters();
      [...]

and then use the input character a few lines below when drawing the Selectable(s), to scroll to the correct one.
The keyboard shortcut works, but mouse input don't work anymore.
The culprit is probably SetActiveID(id, window), but the call is needed to get the char input.
So I'm not sure how to solve it.
Ricky

P.S. I know there's an ongoing implementation of keyboard navigation, but this seemed to me a nice spot addition to the "regular" navigation, to help scrolling very long combo lists.

@Blick1337
Copy link

@rickyviking

I know there's an ongoing implementation of keyboard navigation

I did not see the implementation of finding an item from the combo by typing text. Is there an implementation of this?

@rickyviking
Copy link
Author

@Blick1337 no there's nothing like that as far as I know in the ongoing implementation.
I only mentioned it as, in the long term, the feature I'm trying to implement would be best integrated into the general keyboard navigation framework rather than a standalone tweak on a single widget.

@ocornut ocornut added nav keyboard/gamepad navigation scrolling labels Sep 10, 2017
@HasKha
Copy link

HasKha commented Dec 30, 2017

I implemented this in my project, you can take a look at it here if you are interested: https://github.com/HasKha/GWToolboxpp/blob/master/GWToolbox/GWToolbox/ImGuiAddons.cpp

Features:

  • type one or more letters to select an entry. Resets after a bit (e.g.: 'a'+b' looks for 'ab...'. 'a'+(pause)+'b' looks for 'b...').
  • arrow keys to move selection up/down
  • return to confirm selection

Code is not the cleanest, but works great for me.

P.S.: sorry for necro'ing this thread, but I noticed this while updating to 1.53. Maybe someone can find my implementation helpful :)

@ocornut
Copy link
Owner

ocornut commented Dec 30, 2017

P.S.: sorry for necro'ing this thread,

No worry! Those threads are open and any useful references/info is good to be posted.

Some notes:

  • The Navigation branch currently handle the Up/Down/Space keys.

  • As BeginCombo() is now a more generic API (we don't have access to a list of member), that sort of feature to be generic would have to be built at a lower-level within the navigation system. Perhaps we can have an ItemFlags to enable the feature (I don't have a name for it, let's call it ItemFlags_TextualSearchAndFocus for now), Nav code would maintain that filter data and functions like MenuItem() or Selectable() would have a way to register their label + ID to an internal function that would handle the focusing.

  • For now a custom solution like yours is probably the best way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nav keyboard/gamepad navigation scrolling
Projects
None yet
Development

No branches or pull requests

4 participants