check_active() with keyboards?

GPC2 script programming for Titan Two. Code examples, questions, requests.

check_active() with keyboards?

Postby creedkiller » Fri Jul 07, 2017 9:07 pm

is it possible to use functions like check_active() or time_active() with a key press?
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: check_active() with keyboards?

Postby bonefisher » Sat Jul 08, 2017 12:12 am

creedkiller wrote:is it possible to use functions like check_active() or time_active() with a key press?

I haven't tried but do a quick test and see....... :smile0517:
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

Re: check_active() with keyboards?

Postby creedkiller » Sat Jul 08, 2017 3:55 am

bonefisher wrote:
creedkiller wrote:is it possible to use functions like check_active() or time_active() with a key press?

I haven't tried but do a quick test and see....... :smile0517:

i actually did before posting this thread, i should have mentioned that. not sure if i got the syntax correct, but it didnt work, here is what i tried

Code: Select all
#define KEYA    0x04
    if(check_active(KEYA, 450) && !SpotTrig) {
        SpotTog = !SpotTog;  SpotTrig = !SpotTrig;
        if(!SpotTog) { printf("Auto Spot -OFF"); Notify('R'); }
        else if(SpotTog) { printf("Auto Spot -ON"); Notify('G'); }
    } else if(event_release(KEYA) && time_active(KEYA) >= 450 && SpotTrig) SpotTrig = FALSE;


EDIT: to be clear, it compiled, but didnt actually work
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: check_active() with keyboards?

Postby creedkiller » Mon Jul 10, 2017 6:35 am

I have now found that I can use all the nice functions like is_active, is_release, time_active, time_release, event_active, event_release, check_active & check_release with keyboard keys if I apply a input translator to my script which assigns those keys to buttons. J2Kbr, or anyone else who might know, is there anyway to use these functions with a keyboard without having to "input translate" them first? it would be more useful to me this way, because having the keys independent of the buttons will mean I can do so much more. To give an idea of my setup: viewtopic.php?f=14&t=6573
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: check_active() with keyboards?

Postby J2Kbr » Mon Jul 10, 2017 1:50 pm

creedkiller wrote:J2Kbr, or anyone else who might know, is there anyway to use these functions with a keyboard without having to "input translate" them first?

The key must be mapped to a GPC input (input translate) to use these builtin functions.

However, you can create your own functions to do the same operations for the not mapped keys. Some data structures is needed to monitor the state of keys, but totally possible. I will try code something and post here soon.
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: check_active() with keyboards?

Postby creedkiller » Tue Jul 11, 2017 3:58 am

J2Kbr wrote:
creedkiller wrote:J2Kbr, or anyone else who might know, is there anyway to use these functions with a keyboard without having to "input translate" them first?

The key must be mapped to a GPC input (input translate) to use these builtin functions.

However, you can create your own functions to do the same operations for the not mapped keys. Some data structures is needed to monitor the state of keys, but totally possible. I will try code something and post here soon.

thank you :smile0202:
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: check_active() with keyboards?

Postby J2Kbr » Tue Jul 11, 2017 7:55 am

Alright!! here some initial implementation of these functions for not mapped keys. Please note you must define the keys to "monitor" at the beginning of the script.

Code: Select all
#include <keyboard.gph>
 
#define KEYS_TO_MONITOR     4
 
const uint8 keys_to_monitor[KEYS_TO_MONITOR] = {
    KEY_SPACEBAR, KEY_W, KEY_A, KEY_S, KEY_D,
};
 
bool monitor_previous[KEYS_TO_MONITOR];
uint32 monitor_time_active[KEYS_TO_MONITOR];
uint32 monitor_time_release[KEYS_TO_MONITOR];
int k;
 
// This part should be on the very beginnning of the script code
main {
    for(k = 0; k < KEYS_TO_MONITOR; ++k) {
        if(key_status(keys_to_monitor[k]) != monitor_previous[k]) {
            if(monitor_previous[k]) {
                monitor_time_release[k] = system_time();
            } else {
                monitor_time_active[k] = system_time();
            }
        }
    }
}
 
uint8 key_idx(uint8 key) {
    for(k = 0; k < KEYS_TO_MONITOR; ++k) {
        if(keys_to_monitor[k] == key) {
            return(k);
        }
    }
    return(0);
}
 
#define key_is_active(k)        key_status(k)
 
#define key_is_release(k)       (!key_status(k))
 
uint32 key_time_active(uint8 key) {
    return(system_time() - monitor_time_active[key_idx(key)]);
}
 
uint32 key_time_release(uint8 key) {
    return(system_time() - monitor_time_release[key_idx(key)]);
}
 
bool key_event_active(uint8 key) {
    return(key_status(key) && !monitor_previous[key_idx(key)]);
}
 
bool key_event_release(uint8 key) {
    return(!key_status(key) && monitor_previous[key_idx(key)]);
}
 
bool key_check_active(uint8 key, uint32 ms) {
    return(key_status(key) && key_time_active(key) >= ms);
}
 
bool key_check_release(uint8 key, uint32 ms) {
    return(!key_status(key) && key_time_release(key) >= ms);
}
 
// This part should be on the very end of the script code
main {
    for(k = 0; k < KEYS_TO_MONITOR; ++k) {
        monitor_previous[k] = key_status(keys_to_monitor[k]);
    }
}
 
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: check_active() with keyboards?

Postby creedkiller » Tue Jul 11, 2017 9:12 am

thanks very much J2Kbr, will give it a go and let you know what happens.
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: check_active() with keyboards?

Postby creedkiller » Tue Jul 11, 2017 9:54 am

J2Kbr wrote:Alright!! here some initial implementation of these functions for not mapped keys. Please note you must define the keys to "monitor" at the beginning of the script.

Code: Select all
#include <keyboard.gph>
 
#define KEYS_TO_MONITOR     4
 
const uint8 keys_to_monitor[KEYS_TO_MONITOR] = {
    KEY_SPACEBAR, KEY_W, KEY_A, KEY_S, KEY_D,
};
 
bool monitor_previous[KEYS_TO_MONITOR];
uint32 monitor_time_active[KEYS_TO_MONITOR];
uint32 monitor_time_release[KEYS_TO_MONITOR];
int k;
 
// This part should be on the very beginnning of the script code
main {
    for(k = 0; k < KEYS_TO_MONITOR; ++k) {
        if(key_status(keys_to_monitor[k]) != monitor_previous[k]) {
            if(monitor_previous[k]) {
                monitor_time_release[k] = system_time();
            } else {
                monitor_time_active[k] = system_time();
            }
        }
    }
}
 
uint8 key_idx(uint8 key) {
    for(k = 0; k < KEYS_TO_MONITOR; ++k) {
        if(keys_to_monitor[k] == key) {
            return(k);
        }
    }
    return(0);
}
 
#define key_is_active(k)        key_status(k)
 
#define key_is_release(k)       (!key_status(k))
 
uint32 key_time_active(uint8 key) {
    return(system_time() - monitor_time_active[key_idx(key)]);
}
 
uint32 key_time_release(uint8 key) {
    return(system_time() - monitor_time_release[key_idx(key)]);
}
 
bool key_event_active(uint8 key) {
    return(key_status(key) && !monitor_previous[key_idx(key)]);
}
 
bool key_event_release(uint8 key) {
    return(!key_status(key) && monitor_previous[key_idx(key)]);
}
 
bool key_check_active(uint8 key, uint32 ms) {
    return(key_status(key) && key_time_active(key) >= ms);
}
 
bool key_check_release(uint8 key, uint32 ms) {
    return(!key_status(key) && key_time_release(key) >= ms);
}
 
// This part should be on the very end of the script code
main {
    for(k = 0; k < KEYS_TO_MONITOR; ++k) {
        monitor_previous[k] = key_status(keys_to_monitor[k]);
    }
}
 

brilliantly done J2Kbr. I split your code into 2 header files and now use them 1 at the top and the 2nd at the bottom of my scripts. :smile0202: :smile0203: :joia:

EDIT: also found it interesting that 1 script can have multiple "main" sections, something else for me to read about :smile0517:
Last edited by creedkiller on Tue Jul 11, 2017 12:30 pm, edited 1 time in total.
creedkiller
Lieutenant
Lieutenant
 
Posts: 293
Joined: Fri Mar 17, 2017 4:08 pm

Re: check_active() with keyboards?

Postby antithesis » Tue Jul 11, 2017 10:21 am

Great stuff. I'm sure I'll find a use for this :joia:

What's the maximum number of keys that can be monitored?
Official Australian retailer for Titan One, Titan Two and XIM APEX at Mod Squad
User avatar
antithesis
Colonel
Colonel
 
Posts: 1912
Joined: Sat May 28, 2016 10:45 pm

Next

Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 108 guests