xkeys: xkey_event_ functions for keyboard key state tracking

Documentation, usage tips and configuration guides for the Titan Two scripts and bytecodes published by the community users.

Moderators: The_Rabid_Taco, pablosscripts, bonefisher

xkeys: xkey_event_ functions for keyboard key state tracking

Postby Scachi » Tue Oct 02, 2018 12:02 pm

This code adds some functions for tracking keyboard key actions.
It includes a small functional example script: xkeys.gpc

Download it via GTuner IV: "Online Resources" tab, search for "xkeys".
Open/run the script xkeys.gpc and press the key A or S to get some information in the output panel.

It adds around 5,5% bytecode to your project (plus some more for the keys to check) and doesn't use much cpu time.
If you only need the xkeys_event_active and xkeys_event_release functions you can reduce the bytecode to 3,5% by adding this line before the #include "xkeys.gph" line:
Code: Select all
#define XKEYS_NOTIME
This will disable the xkeys_time_ and xkeys_check_ functions.


Functions added by this example:
xkeys_event_active, xkeys_event_release, xkeys_time_active, xkeys_time_release, xkeys_check_active, xkeys_check_release, xkeys_is_active, xkeys_is_release


Adding xkeys to your project:
  • Copy the file xkeys.gph into your scripts directory.

  • Add the two lines below somewhere near the top of your script:
    Code: Select all
    #define KEYS_TO_MONITOR_MAX 2 // change 2 to the maximum number of keys you want to keep track of
    #include "xkeys.gph"

  • Add the keys to monitor:
    Code: Select all
    uint8 KEY1, KEY2; // or any other name, this will get an index number assigned by the xkeys_add function
     
    init {
      // use xkeys_add to add each key to monitor, it returns an uint8 number that has to be used with the functions
      KEY1=xkeys_add(KEY_A); // add key KEY_A to monitoring
      KEY2=xkeys_add(KEY_S); // add key KEY_S to monitoring
    }
     

  • Now you have to call the function with the KEY1 (KEY_A) or KEY2 (KEY_S) as an argument to check for events of the specific key like this:

    Code: Select all
    main {  
      // **** events ****
      if (xkeys_event_active(KEY1)) printf("key event active %d",xkeys_to_monitor[KEY1]);
      if (xkeys_event_release(KEY1)) printf("key event release %d",xkeys_to_monitor[KEY1]);
     
      if (xkeys_event_active(KEY2)) printf("key event active %d",xkeys_to_monitor[KEY2]);
      if (xkeys_event_release(KEY2)) printf("key event release %d",xkeys_to_monitor[KEY2]);
     
      // **** state ****
      //if (xkeys_is_active(KEY1)) printf("ACTIVE");
     
      // **** is_ time checking ****
      if (xkeys_is_active(KEY1) && xkeys_time_active(KEY1) == 1000) printf("KEY1 1000 time ACTIVE");
      //if (xkeys_is_release(KEY1) && xkeys_time_release(KEY1) == 1000 ) printf("KEY1 1000 time RELEASE");
     
      // **** check_ time checking ****
      //if (xkeys_check_active(KEY1,2000)) printf("KEY1 check ACTIVE 2000");
      //if (xkeys_check_active(KEY2,2000)) printf("KEY2 check ACTIVE 2000");
      //if (xkeys_check_release(KEY1,2000)) printf("KEY1 check RELEASE 2000");
    }
User avatar
Scachi
Colonel
Colonel
 
Posts: 1645
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Return to Scripts Documentation (Titan Two)

Who is online

Users browsing this forum: JamesCaywood and 14 guests