[SOLVED] Intercepting keypresses in a modal dialog

Started by dreammaster, Mon 11/10/2004 01:25:27

Previous topic - Next topic

dreammaster

I have a problem.. I'm currently building a custom save/restore game dialog, and in my save dialog I want to handle the Enter key to fire off the acceptance code, since a user's normal behaviour would be to type in a new savegame and press Enter.

The only trouble is that when the dialog is showing, the on_key_press method doesn't seem to get called. I tried putting a Display("%d", keypress) in it to register any keypresses, and it won't display anything whilst the dialog is active. Is there any particular reason why the on_key_press method isn't getting called, and is there some way around it?

Ashen

#1
I guess it's because a popup modal GUI pauses the game, and this line in on_key_press:
Code: ags
if (IsGamePaused() == 1) keycode=0;

means it gets ignored.

I think the way round it is to use if (button == X) , where X is the textbox.
Alternatively, you could try IsKeyPressed in the interface_click section for the Save/Load GUI.

The long winded way would be to replace:
Code: ags

if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses

with something like:
Code: ags

  if (IsGamePaused() == 1) {
    if (IsGUIOn (SAVEGUI) == 1) { // SAVEGUI is on, so
      if (keycode == 13) {// Enter
        // whatever (Save the game)
      }
    }
    else keycode=0;  // game paused, so don't react to keypresses
  }

However, I don't know how well this would work.
I know what you're thinking ... Don't think that.

dreammaster

#2
Actually, I put the Display line before the IsGamePaused() check in the on_key_press method, so I know it's not that that's causing it. The problem is that the method isn't getting called at all.

Now that I think about it, maybe I can get around it by using IsKeyPressed(13) in the repeatedly execute rather than interface click, which wouldn't (as I understand it) get called at the proper time, since it's a keypress rather than a click.

Even if putting it in repeatedly execute would work, it seems a bit roundabout though, and I'd still be interested in finding out why the on_key_press doesn't get called at all.

Gilbert

Since you can type in the name in the GUI, it's a textbox right?
If you're using a textbox on a GUI, you don't really need to check for ENTER key in on_key_pressed(), because:
Quote
If a text box is on a currently displayed GUI, all standard keypresses (ie. letter keys, return and backspace) are diverted to the textbox instead of being passed to the on_key_press function. When the player presses Return in the text box, the interface_click function is called with the text box's GUI and object number. You can then use the GetTextBoxText text script function to retrieve what they typed in.
So you can just regard that textbox as a button, and check if that's pressed (which actually was an ENTER pressed), for example, in interface_click():

if (GUI==SAVE){
  if (button==5) {//Say the textbox is object #5 of GUI SAVE
    //Do whatever you need when enter pressed
  }

}

dreammaster

Ah. I understand now. Thanks for the explanation. It turns out it was a simple answer after all.   8)

SMF spam blocked by CleanTalk