check_active() with keyboards?

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

Re: check_active() with keyboards?

Postby J2Kbr » Tue Jul 11, 2017 11:35 am

creedkiller wrote: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:

Slip in two header files is a very nice idea!! :joia: :joia:
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 J2Kbr » Tue Jul 11, 2017 11:37 am

antithesis wrote:What's the maximum number of keys that can be monitored?

There is no limit for that, all key could be monitored. But, for performance/optimization reasons, it is better just monitor the ones used in the script. :smile0517:
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 Scachi » Fri Aug 04, 2017 3:29 pm

Could you please upload this to the online resource examples ?
Or add it to an include file that comes with gtunerIV ? This really deserves a section in the online documentation, too.
User avatar
Scachi
Brigadier General
Brigadier General
 
Posts: 3044
Joined: Wed May 11, 2016 6:25 am
Location: Germany

Re: check_active() with keyboards?

Postby J2Kbr » Fri Aug 11, 2017 10:26 am

Definitively, I will be improving the posted code and publishing on the Online Resources.
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 antithesis » Wed May 09, 2018 2:17 pm

How is key_time_release used correctly? It seems to produce random results, even in Scachi's example usage code in Online Resources.

Both the active and release states are correctly detected, and key_time_active works perfectly, but key_time_release doesn't seem to be working. Is there something I'm missing?

I worked around the problem and got exactly the result I wanted using Scachi's updated version, I'm just a tad confused about key_time_release. Is its purpose to measure time between key presses?

And why does it spit out random values in Device Monitor - is it because it's not being called correctly?

Other than that, these are brilliant additions to the kb functions and perhaps should be added to the GPC Language rather than tucked away in an obscure thread :smile0517:
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

Re: check_active() with keyboards?

Postby Buffy » Wed May 09, 2018 7:55 pm

antithesis wrote:How is key_time_release used correctly? It seems to produce random results, even in Scachi's example usage code in Online Resources.

Both the active and release states are correctly detected, and key_time_active works perfectly, but key_time_release doesn't seem to be working. Is there something I'm missing?

I worked around the problem and got exactly the result I wanted using Scachi's updated version, I'm just a tad confused about key_time_release. Is its purpose to measure time between key presses?

And why does it spit out random values in Device Monitor - is it because it's not being called correctly?

Other than that, these are brilliant additions to the kb functions and perhaps should be added to the GPC Language rather than tucked away in an obscure thread :smile0517:


I've made a more optimized version if you'd like to check it out:

you need to define the EXTRA_KEY_VAR and NUM_OF_EXTRA_KEYS before including the header though.

Code: Select all

#ifndef EXTRA_KEY_GPC_
#define EXTRA_KEY_GPC_

#include <keyboard.gph>

#ifndef EXTRA_KEY_VAR
    #define EXTRA_KEY_VAR        extra_keys_list
    #define NUM_OF_EXTRA_KEYS    1
    uint16 EXTRA_KEY_VAR[1] = { 0 };
#endif

#ifndef NUM_OF_EXTRA_KEYS
    #define NUM_OF_EXTRA_KEYS    0
#endif

#define _EXTRA_KEY_STATUS_RELEASED            0
#define _EXTRA_KEY_STATUS_JUST_RELEASED        1
#define _EXTRA_KEY_STATUS_PRESSED            2
#define _EXTRA_KEY_STATUS_JUST_PRESSED        3

uint8 extra_keys_status[NUM_OF_EXTRA_KEYS] = { 0 };
uint32 extra_keys_status_press_time[NUM_OF_EXTRA_KEYS] = { 0 };
uint32 extra_keys_status_rel_time[NUM_OF_EXTRA_KEYS] = { 0 };

main {
   
    uint8 _extra_key_i;
    for(_extra_key_i = 0; _extra_key_i < NUM_OF_EXTRA_KEYS; _extra_key_i++) {
        if(key_status(EXTRA_KEY_VAR[_extra_key_i])) {
            if( (extra_keys_status[_extra_key_i] == _EXTRA_KEY_STATUS_RELEASED) || (extra_keys_status[_extra_key_i] == _EXTRA_KEY_STATUS_JUST_RELEASED) ) {
                extra_keys_status[_extra_key_i] = _EXTRA_KEY_STATUS_JUST_PRESSED;
                extra_keys_status_press_time[_extra_key_i] = system_time();
            }
            else if(extra_keys_status[_extra_key_i] == _EXTRA_KEY_STATUS_JUST_PRESSED){
                extra_keys_status[_extra_key_i] = _EXTRA_KEY_STATUS_PRESSED;
            }
        }
        else {
            if( (extra_keys_status[_extra_key_i] == _EXTRA_KEY_STATUS_PRESSED) || (extra_keys_status[_extra_key_i] == _EXTRA_KEY_STATUS_JUST_PRESSED) ) {
                extra_keys_status[_extra_key_i] = _EXTRA_KEY_STATUS_JUST_RELEASED;
                extra_keys_status_rel_time[_extra_key_i] = system_time();
            }
            else extra_keys_status[_extra_key_i] = _EXTRA_KEY_STATUS_RELEASED;
        }
    }
    _extra_key_i = 0;
}

uint8 get_key_index(uint8 key) {
    uint8 _extra_key_i;
    for(_extra_key_i = 0; _extra_key_i < NUM_OF_EXTRA_KEYS; _extra_key_i++) {
        if(EXTRA_KEY_VAR[_extra_key_i] == key) return _extra_key_i;
    }
    return 0;
}

bool key_event_active(uint8 key) {
    return (extra_keys_status[get_key_index(key)] == _EXTRA_KEY_STATUS_JUST_PRESSED);
}

bool key_event_release(uint8 key) {
    return (extra_keys_status[get_key_index(key)] == _EXTRA_KEY_STATUS_JUST_RELEASED);
}

uint32 key_time_active(uint8 key) {
    if(key_status(key))
        return (system_time() - extra_keys_status_press_time[get_key_index(key)]);
    //else
    return (extra_keys_status_rel_time[get_key_index(key)] - extra_keys_status_press_time[get_key_index(key)]);
}

uint32 key_time_release(uint8 key) {
    if(key_status(key))
        return (extra_keys_status_press_time[get_key_index(key)] - extra_keys_status_rel_time[get_key_index(key)]);
    //else
    return (system_time() - extra_keys_status_rel_time[get_key_index(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);
}

#endif /* EXTRA_KEY_GPC_ */
Last edited by Buffy on Wed May 09, 2018 11:43 pm, edited 1 time in total.
ConsoleTuner Support Team || Discord || Custom Scripts
User avatar
Buffy
Lieutenant
Lieutenant
 
Posts: 422
Joined: Wed Jul 20, 2016 5:23 am

Re: check_active() with keyboards?

Postby bonefisher » Wed May 09, 2018 8:27 pm

Its still looks like to me the benefit is still using the input translator...... :smile0517:
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

Re: check_active() with keyboards?

Postby antithesis » Wed May 09, 2018 9:03 pm

Input Translators only take you part of the way. If using Apex > Titan Two, the only scriptable kb function is key_status, which allows for key press detection, but not time active, nor release state.


Buffy wrote:I've made a more optimized version if you'd like to check it out:

you need to define the EXTRA_KEY_VAR and NUM_OF_EXTRA_KEYS before including the header though.

I defined the additional keys on line 8 and the number of keys on line 9. Nothing else was changed.

On compile, there's a GPC error on line 10 -
Code: Select all
 
GPC error: xkeys_opt.gph(10): syntax error, unexpected NUMBER_LITERAL, expecting IDENTIFIER or MUL_OP '0x1D'.
 
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

Re: check_active() with keyboards?

Postby Buffy » Thu May 10, 2018 8:54 am

antithesis wrote:I defined the additional keys on line 8 and the number of keys on line 9. Nothing else was changed.

On compile, there's a GPC error on line 10 -
Code: Select all
 
GPC error: xkeys_opt.gph(10): syntax error, unexpected NUMBER_LITERAL, expecting IDENTIFIER or MUL_OP '0x1D'.
 


I've uploaded a full example and attached it as a zip. I've also changed the initial code to allow mouse buttons to be tracked too. :smile0517:
Attachments
Extra Buttons.zip
(2 KiB) Downloaded 88 times
ConsoleTuner Support Team || Discord || Custom Scripts
User avatar
Buffy
Lieutenant
Lieutenant
 
Posts: 422
Joined: Wed Jul 20, 2016 5:23 am

Re: check_active() with keyboards?

Postby antithesis » Thu May 10, 2018 9:55 am

Nice work Buffy!

These kb and mouse functions should be officially integrated into GPC. They're very powerful and provide far greater scripting flexibility.

We've seen a move lately in the m/kb direction and this sort of functionality capitalises on the new audience for T2.
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

PreviousNext

Return to GPC2 Script Programming

Who is online

Users browsing this forum: No registered users and 79 guests