key press and key release within script editor

Don't know how to make something and can't find a tutorial? Ask for one here.

key press and key release within script editor

Postby master0500 » Fri Sep 20, 2013 1:48 pm

would it be possible for someone to create a brief tutorial with explanations about doing key press and key release events withing the script editor? thank you for your time :D
Posts: 409
Joined: Sun Jun 26, 2011 9:42 pm
Score: 27 Give a positive score

Re: key press and key release within script editor

Postby skydereign » Fri Sep 20, 2013 8:08 pm

I wrote a function that should make it easier to do (from the this topic
Code: Select all
GetKeys ()
  const int key_reg[] = {KEY_RIGHT, KEY_LEFT, -1}; // must end with -1
  // list of keys that can have keyup states
  // that way it doesn't need to loop through every key
  // because rarely do people want to catch gp2x keys

  // static variables are only initialized once
  static int protect = 0; // used to lock the function (only once per frame)
  static char key_prev[500] = {0}; // or however long GetKeyState is
  static char* key; // function remembers last key state
  if(frame%2==protect) // only update once per frame
      int i;
      for(i=0; key_reg[i]!=-1; i++) // loop through registered keys
          if(key_prev[key_reg[i]]==1 && key[key_reg[i]]==0)
            { // if the key was pressed, but isn't anymore
              key_prev[key_reg[i]]=key[key_reg[i]]; // set key previous
              key[key_reg[i]]=-1; // set to keyup
              key_prev[key_reg[i]]=key[key_reg[i]]; // set key previous

      protect=protect==0; // lock until next frame
  return key;

Add that function into global code, and add all the keys you need to track into that first array. Then you can use GetKeys like you would GetKeyState. GetKeys though needs to be called every frame if it is to be accurate all the time, so I recommend putting it in the view's draw actor.
User avatar
Posts: 3513
Joined: Mon Jul 28, 2008 8:29 am
Score: 588 Give a positive score

Return to Tutorial Requests

Who is online

Users browsing this forum: No registered users and 1 guest