User Tools

Site Tools


t2:gpc_scripting:keyboard

Keyboard & More

This article is about utilizing unmapped inputs like keys/mouse-buttons/pedals/… for extra script actions.

If you are using keyboard & mouse you have mapped your favorite controls via the Input Translator to gamepad controls already. Leaving the rest of the keys/buttons/.. for direct scripting usage.

get_val/get_actual/event_active/.. and similar functions are for (mapped) gamepad controls only, so you can't use those for reading key or mouse states.

There are seperate functions for reading keyboard states and mouse states.
Keyboard: key_check , key_status
Mouse: mouse_status

When you have a mouse with more than 5 buttons the function mouse_status might only be able to read 3, 4 or 5 buttons directly. For the rest of the buttons you may need to assign key presses to them using the configuration software of your mouse and store this profile on the mouse memory.1)

Keys

Requires use of the header file keyboard.gph, supplied by GTuner IV. It contains all known key names.

#include <keyboard.gph>

This isn't for keyboards only. It is for any device that you can assign a keypress to and save the settings to the persistent memory of that device.

Functions to use:
key_check — Retrieve code of a pressed key
key_status — Status of a specific keyboard key

Simple Example

Set BUTTON_21 to 99.99 as long as the key F1 is pressed:

#include <keyboard.gph>
 
main {
  if (key_status(KEY_F1)) set_val(BUTTON_21,99.99);
}

event_.. Functions

GPC2 doesn't have event_active/event_release functions for keys.

Create Own

You can mimic the event_ functions by storing the key_status result and compare it on the next interaction of main. You have to do this for each key individually with its own bool bKeyName variable.

#include <keyboard.gph>
 
bool bF1=FALSE; // stored state
 
main {
  bool bk;
 
  // F1
  bk=key_status(KEY_F1); // read current F1 state
  if (bk && !bF1) {  // bk == TRUE and bF1 == FALSE
    printf("event_active : key F1");
    // your code to run on event_active here
  } 
  if (bk && bF1) {  // bk == TRUE and bF1 == TRUE
    set_val(BUTTON_21,99.99);  //printf("already active : key F1");
  }
  if (!bk && bF1) {  // bk == FALSE and bF1 == TRUE
    printf("event_release : key F1");
    // your code to run on event_release here
  }
  bF1=bk; // update F1 stored state
 
}

key_events Header File

When you need event_active and event_release functions only you can use the “key_events” header file from the Online Resource.
It will add the functions: key_event_active and key_event_release.

As with all user made .gph header files, download it from the Online Resource using GTuner IV and place it into the same directory where your script is located.

There is also an example script available for download: “key_events_example_1”. The code of the example:

#include <keyboard.gph>
#include "key_events.gph"
 
main{
  if(key_event_active(KEY_SPACEBAR)){
    printf("Spacebar Active");
  }
  if(key_event_release(KEY_SPACEBAR)){
    printf("Spacebar Released");
  }
 
  if(key_event_active(KEY_7)){
    printf("Key 7 Active");
  }  
  if(key_event_release(KEY_7)){
    printf("Key 7 Released");
  }
}

xkeys Header File

Another option is to use the xkeys.gph header file from the Online Resource.
It will add the functions:
xkeys_event_active, xkeys_event_release, xkeys_time_active, xkeys_time_release, xkeys_check_active, xkeys_check_release, xkeys_is_active, xkeys_is_release.

As with all user made .gph header files, download it from the Online Resource using GTuner IV and place it into the same directory where your script is located.

Here is a small example using the header file:

#define KEYS_TO_MONITOR_MAX 4 // set to the max number of keys you need
#include "xkeys.gph"
 
uint8 KEY1, KEY2, KEYF1, KEYF2;
 
init {
  // use xkeys_add to add a key to monitoring, store the return value
  KEY1 =xkeys_add(KEY_1);
  KEY2 =xkeys_add(KEY_2);
  KEYF1=xkeys_add(KEY_F1);
  KEYF2=xkeys_add(KEY_F2);
}
 
main { 
  // NOW using the stored return values from above
  if (xkeys_event_active(KEY1)) printf("Key 1 event active");
  if (xkeys_event_release(KEY1)) printf("Key 1 event release");
  if (xkeys_event_active(KEY2)) printf("Key 2 event active");
  if (xkeys_event_release(KEY2)) printf("Key 2 event release");
  if (xkeys_is_active(KEYF1)) printf("ACTIVE F1");
  if (xkeys_check_active(KEYF2,1000)) printf("F2 ACTIVE >= 1000ms");
}

XKEYS Forum Post for more information and getting help.

1) Not all mice have onboard memory or can't output key presses when directly connected to the Titan Two. That is not a limitation of the T2. It is the mouse that needs to support the keyboard output.
t2/gpc_scripting/keyboard.txt · Last modified: 2019/08/30 12:11 by scachi