How to check for ANY keypress

Started by accidentalrebel, Mon 23/05/2011 18:33:51

Previous topic - Next topic

accidentalrebel

Hi guys,

Is there a functionality here in AGS that checks for a keypress regardless of what key was pressed? For example, if I want to skip a cutscene, pressing any key would skip it.

Although I can just manually list down all the keys on the keyboard if ( IsKeyPressed(eKeyA) && IsKeyPressed(eKeyB... and so on) but I was wondering if there was an easier way of doing this?

Thanks!

Khris

First of all, you can set what's going to skip a cutscene using the parameter:

Code: ags
  StartCutscene(eSkipAnyKey);


If you need to do it with IsKeyPressed(), you can iterate through the ascii codes:

Code: ags
bool IsAnyKeyPressed() {
  int i = eKeySpace;  // 32
  while (i <= eKeyUnderscore) {  // 95
    if (IsKeyPressed(i)) return true;
    i++;
  }
  return false;
}


Import it in the header (Global.ash):

Code: ags
import bool IsAnyKeyPressed();


This covers most of the keypresses that generate characters. You can look up the rest here.

monkey0506

Why not just use the on_key_press function?

Code: ags
function on_key_press(eKeyCode keycode)
{
  // A KEY WAS PRESSED!!
}


Now on_key_press isn't fired for some of the keycodes that can be detected by IsKeyPressed, but for what you actually need.

If you need any keycode, even during blocking interactions, including things like Alt-combinations, I wrote a module for that, but it might not play nicely with the built-in on_key_press..I forget, but I think I was planning to update it to make it more useful. In any case, I'd say that you can probably just use the existing built-in function.

SMF spam blocked by CleanTalk