Page 1 of 1

xkeys: xkey_event_ functions for keyboard key state tracking

PostPosted: Tue Oct 02, 2018 12:02 pm
by Scachi
Update v1.04 changes: lowered cpu usage, monitoring of 40+ keys working now while cpu usage < 15%

This code adds some functions for tracking keyboard key actions:
xkeys_event_active, xkeys_event_release, xkeys_time_active, xkeys_time_release, xkeys_check_active, xkeys_check_release, xkeys_is_active, xkeys_is_release

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.


If you need event_active and event_release functions for keyboard only you can use the compact and fast header file "key_events" by DontAtMe. Search for it in the online resource.

Or you can stick to xkeys.gph and reduce the used bytecode a bit by adding this line above the #include "xkeys.gph" line:
Code: Select all
#define XKEYS_NOTIME
This will disable the xkeys_time_ and xkeys_check_ functions.


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,
    the order is important, xkeys specific #define lines first:
    Code: Select all
    #define XKEYS_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");
    }

Re: xkeys: xkey_event_ functions for keyboard key state trac

PostPosted: Sat Jul 06, 2019 7:58 pm
by Scachi
Update v1.04 changes: lowered cpu usage, monitoring of 40+ keys working now while cpu usage < 15%
Thanks to Nstarnoe for testing :smile0517:

Re: xkeys: xkey_event_ functions for keyboard key state trac

PostPosted: Thu Aug 15, 2019 3:52 pm
by Nstarnoe
Hi Sachi

A quick follow up. I have been using it for a good while now and still flawless :-)

And a quick question, can it be used for mouse buttons as well?

Re: xkeys: xkey_event_ functions for keyboard key state trac

PostPosted: Thu Aug 15, 2019 4:26 pm
by Scachi
Nstarnoe wrote:Hi Sachi

A quick follow up. I have been using it for a good while now and still flawless :-)

And a quick question, can it be used for mouse buttons as well?

Thank you for your feedback.
No sorry, keyboard only. Its named "xkeys" for a reason :wink:
I have created this one long time ago for use with my own add on controller (see link in signature) and this controller can do keys/buttons only.

Adding mouse inputs to it would increase the size too much and requires too much work. Writing something new would be more effective.
The T2 has different functions for reading mouse states and keyboard states (include file mouse.gph or keyboard.gph) so I think it would be better to have another header file for mouse events instead of one for key&mouse to save script size when one want to use one only (key or mouse).

DontAtMe did a great job creating a fast and easy to use "key_events" header file for key_event_active and key_event_release while using small bytecode size only.
Someone with his skill might be able to create a fast and easy to use header file for mouse input mouse_event_active mouse_event_release (button/wheel) too ? :innocent_smile_1:
I am not brave enough to ask him if he is willing to create a mouse event header file :wink: :whistling:

Re: xkeys: xkey_event_ functions for keyboard key state trac

PostPosted: Sat Aug 17, 2019 12:22 pm
by Nstarnoe
I was kinda expecting that, but was hoping :)

I am fairly proud that i got it to work using this code, but it not as clean and nice as what you guys can do :)
Code: Select all
    if(event_active(R3)) 
    {
        if (!n_drop_r3)     {n_drop_r3  =TRUE;}   
        else            {n_drop_r3  =FALSE;}   
    }   
 
        if(mouse_status(MBUTTON_2) && n_drop ==1 && !n_drop_r3)
            {combo_run(drop); n_drop =2;}
 
        if(!mouse_status(MBUTTON_2) && n_drop ==2 && !n_drop_r3)
            {combo_run(drop); n_drop =1;}