onsdag 22 juli 2015

GTA V Scripting PC Part 3: Keyboard and Controller support

Keyboard support example

For keyboard support you need these functions:

bool get_key_pressed(int nVirtKey)
{
    return (GetAsyncKeyState(nVirtKey) & 0x8000) != 0;
}

We need to check if the key is valid with isprint(): http://www.cplusplus.com/reference/cctype/isprint/
We also want a delay between possible keystrokes. We use GetTickCount() for this:

DWORD trainerResetTim;

void reset_mod_switch()
{
    trainerResetTime = GetTickCount();
}

We want to get the key pressed every 400 milliseconds. Insert is used as key if activateKeyChar is not a valid key:

bool on_off_switch_pressed(){
    if (isprint(activateKeyChar)){ // activateKeyChar is your key
        return ((GetTickCount() > trainerResetTime + 400) &&   get_key_pressed(activateKeyChar));
    }
    else{
        return ((GetTickCount() > trainerResetTime + 400) && get_key_pressed(0x2D));
    }
}

Read the previous post about how to read from the .ini file. You can in this way let the user choose a key code from here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx and write it in the .ini file so that your mod can get it and use it.

Now you can in your script loop check if your button is pressed:

if (on_off_switch_pressed()){
// do something
}

Controller support example

Controller support is similar:

bool on_off_mod_with_controller_switch_pressed(){
    return ((GetTickCount() > trainerResetTime + 400) && isControllButtonPressed(activateControllerButtonChar)
        && canActivateWithController);
}

canActivateWithController is a bool used to make it so that the controller only is usable when wanted.

Controller button keys:http://pastebin.com/X6X2hUB7

int xInputIndex = 2; // always 2
bool isControllButtonPressed(int controllerButton){
    if (canActivateWithController){
        if (CONTROLS::IS_CONTROL_PRESSED(xInputIndex, controllerButton)){
            return true;
        }
    }
    return false;
}



Inga kommentarer:

Skicka en kommentar