Set focus to listbox [solved]

Started by arj0n, Thu 25/02/2021 10:51:14

Previous topic - Next topic

arj0n

Info:
The game is keyboard only
It has a GUI containing a listbox, the GUI is hidden by default.
Pressing TAB opens up the listboxgui.
DownArrow and UpArrow are for scrolling through the list.
After pressing Return (Enter), the code checks what the current selectedItemText is and then displays a specific custom description string.

The issue:
But when the list is shown, pressing Enter doesn't bring up the description.
I first have to cycle one step through the list with DownArrow or UpArrow. After that, pressing enter does give the correct description. (If I close and re-open the listboxgui, pressing enter still does give the correct description)
So it looks like the listbox doesn't have the focus.

'Workaround':
To set the focus to the listbox I currently do the following:
- pressing TAB opens up a listbox.
- check if listbox ItemCount == 1. If so, run a Game.DoOnceOnly Game.SimulateKeyPress (eKeyDownArrow)
- pressing enter now does give correctly the listbox item description

Question:
Is there a (more delicate) way to set the focus to the listbox codewise?

Crimson Wizard

#1
I never knew listbox could get focus or was handling keyboard input at all. Looking in the engine code, I can only see textbox receiving keyboard input.

Are you sure this is not controlled by some custom script?

arj0n

Quote from: Crimson Wizard on Thu 25/02/2021 11:16:21
Are you sure this is not controlled by some custom script?
I'm sure no custom script is controlling this.
Strange thing is when I use a SimulateKeyPress one time, the focus is correct:

Code: ags

else if (keycode == eKeyTab)
{
  if (gInvListBox.Visible == false)
  {
    gInvListBox.Visible = true;
      
    if (ListBoxInv.ItemCount == 1)
    {
      if (Game.DoOnceOnly ("scrollInvListBox"))
      {
        Game.SimulateKeyPress (eKeyDownArrow);
      }
    }
  }
}

Crimson Wizard

Maybe I misunderstand something... are you saying that it's not the script that scrolls list box when you press DownArrow and UpArrow?

arj0n

This is how the arrow keys for scrolling through the listbox items is done in the global script:

Code: ags

function on_key_press(eKeyCode keycode)
{
  if (gInvListBox.Visible == true)
  {
    if (ListBoxInv.ItemCount > 0)
    {
      if (keycode == eKeyDownArrow)
      {
        if (ListBoxInv.SelectedIndex < ListBoxInv.ItemCount-1)
        {
          ListBoxInv.SelectedIndex = ListBoxInv.SelectedIndex + 1;
        }
        else if (ListBoxInv.SelectedIndex == ListBoxInv.ItemCount-1)
        {
          ListBoxInv.SelectedIndex = 0;
        }
        selectedItemText = ListBoxInv.Items[ListBoxInv.SelectedIndex];
      }
      else if (keycode == eKeyUpArrow)
      {
        if (ListBoxInv.SelectedIndex > 0)
        {
          ListBoxInv.SelectedIndex = ListBoxInv.SelectedIndex - 1;
        }
        else if (ListBoxInv.SelectedIndex == 0)
        {
          ListBoxInv.SelectedIndex = ListBoxInv.ItemCount-1;
        }
        selectedItemText = ListBoxInv.Items[ListBoxInv.SelectedIndex];
      }
    }
  }

arj0n

And here's the Enter key part for displaying a custom description for a selected listbox item.
When no focus, the line "no item selected" is shown. After using up or down to scroll first, the description line is correctly shown.

Code: ags

else if (keycode == eKeyReturn)
{
  if (gInvListBox.Visible == true) // gInvListBox
  {
    if (selectedItemText == "data cartridge #01") Display ("'I'm hurt bad...'");
    else if (selectedItemText == "data cartridge #02") Display ("'For security reasons I had to change the Level 2 Access Code.'");
    ...
  }
  else Display ("no item selected");
}

Crimson Wizard

#6
Ok, so there is a script that controls this.

I think there's some misunderstanding here, as this is all controlled by script, not the engine, and list box in the engine does not have any focus at all, it's simply not the part of its behavior.

From the look of the scripts, the problem seems to be that selectedItemText is not set when you make list box visible?
I.e. you may try doing "selectedItemText = ListBoxInv.Items[ListBoxInv.SelectedIndex];" after making it visible.

or rather
Code: ags

if (ListBoxInv.ItemCount > 0)
{
  selectedItemText = ListBoxInv.Items[ListBoxInv.SelectedIndex];
}
else
{
  selectedItemText = null;
}



arj0n

Quote from: Crimson Wizard on Thu 25/02/2021 13:54:42
Ok, so there is a script that controls this.
sorry for the confusion, yes there is a script but no custom script.

Quote from: Crimson Wizard on Thu 25/02/2021 13:54:42
From the look of the scripts, the problem seems to be that selectedItemText is not set when you make list box visible?
Drat, I tried that but somehow made a mistake by setting the SelectedIndex instead of the selectedItemText.
This is working correctly: selectedItemText = ListBoxInv.Items[0];

Thanx for the info!

Crimson Wizard

Quote from: arj0n on Thu 25/02/2021 14:06:09
sorry for the confusion, yes there is a script but no custom script.

What do you call "custom script"? :) I was thinking there's some dedicated script module for listbox, but maybe I should not use word "custom".

arj0n

#9
Quote from: Crimson Wizard on Thu 25/02/2021 14:10:46
What do you call "custom script"? :) I was thinking there's some dedicated script module for listbox, but maybe I should not use word "custom".

Because of your question...:
Quote from: Crimson Wizard on Thu 25/02/2021 11:16:21
Are you sure this is not controlled by some custom script?

...I was assuming you meant any script other than a room or global script...  :)

SMF spam blocked by CleanTalk