check_active() with keyboards?
36 posts
• Page 1 of 4 • 1, 2, 3, 4
check_active() with keyboards?
is it possible to use functions like check_active() or time_active() with a key press?
- creedkiller
- Lieutenant
- Posts: 293
- Joined: Fri Mar 17, 2017 4:08 pm
Re: check_active() with keyboards?
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.......
- bonefisher
- Lieutenant General
- Posts: 5413
- Joined: Thu Jan 29, 2015 10:49 am
Re: check_active() with keyboards?
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.......
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
- Posts: 293
- Joined: Fri Mar 17, 2017 4:08 pm
Re: check_active() with keyboards?
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
- Posts: 293
- Joined: Fri Mar 17, 2017 4:08 pm
Re: check_active() with keyboards?
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
-
J2Kbr - General of the Army
- Posts: 20323
- Joined: Tue Mar 18, 2014 1:39 pm
Re: check_active() with keyboards?
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
- creedkiller
- Lieutenant
- Posts: 293
- Joined: Fri Mar 17, 2017 4:08 pm
Re: check_active() with keyboards?
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
-
J2Kbr - General of the Army
- Posts: 20323
- Joined: Tue Mar 18, 2014 1:39 pm
Re: check_active() with keyboards?
thanks very much J2Kbr, will give it a go and let you know what happens.
- creedkiller
- Lieutenant
- Posts: 293
- Joined: Fri Mar 17, 2017 4:08 pm
Re: check_active() with keyboards?
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.
EDIT: also found it interesting that 1 script can have multiple "main" sections, something else for me to read about
Last edited by creedkiller on Tue Jul 11, 2017 12:30 pm, edited 1 time in total.
- creedkiller
- Lieutenant
- Posts: 293
- Joined: Fri Mar 17, 2017 4:08 pm
Re: check_active() with keyboards?
Great stuff. I'm sure I'll find a use for this
What's the maximum number of keys that can be monitored?
What's the maximum number of keys that can be monitored?
-
antithesis - Colonel
- Posts: 1912
- Joined: Sat May 28, 2016 10:45 pm
36 posts
• Page 1 of 4 • 1, 2, 3, 4
Return to GPC2 Script Programming
Who is online
Users browsing this forum: No registered users and 108 guests