xkeys: xkey_event_ functions for keyboard key state tracking
Posted: Tue Oct 02, 2018 12:02 pm
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:
Adding xkeys to your project:
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
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");
}