OnPlayerKeyStateChange
#1

Hello so, I'm asking for you help guys about OnPlayerKeyStateChange.

I was trying to do a multiple key detection (if he use the 3 key in the same time) - But I don't really see how I can do that, I've tryed this one but it's doesn't work.

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(!IsPlayerInAnyVehicle(playerid) && /*idk*/)
    {
        if(Test== 1)
        {
            TestCount[playerid] ++;
            if(TestCount[playerid] >= 3)
            {

            }
        }
    }
    return 1;
}
I visit the wiki sa-mp and I still don't understand how to do it, so if you can explain me how and show me a example code, it's would be nice thanks.
Reply
#2

Copy this function to the end of your script:

Код:
IsKeyPressed(newkeys, oldkeys, key)
{
if(newkeys & key && !(oldkeys & key)) return 1;
return 0;
}
Depending on which keys should be pressed (List Of Keys) use it this way:

In OnPlayerKeyStateChange:

Код:
if(IsKeyPressed(newkeys, oldkeys, KEY_JUMP) && IsKeyPressed(newkeys, oldkeys, KEY_SPRINT))
{
//This code will be executed if both JUMP and SPRINT are pressed!
}
Reply
#3

Try to check if key pressed or released. Try
if(newkeys > oldkeys) test++ else test--;
Reply
#4

I've tryed this one Borg, but still don't work. NaS, I've tryed this :

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(IsKeyPressed(newkeys, oldkeys, KEY_UP) && IsKeyPressed(newkeys, oldkeys, KEY_SPRINT) && IsKeyPressed(newkeys, oldkeys, KEY_JUMP))
    {
        if(Test == 1)
        {
            TestCount[playerid] ++;
            if(TestCount[playerid] >= 3)
            {

            }
        }
    }
    return 1;
}
but still don't work..
Reply
#5

bumpy.
Reply
#6

You shouldn't use TestCount in NaS variant, 'coz it's already check 3 buttons(KEY_UP, KEY_SPRINT, KEY_JUMP And not forget to set Test to 1. If you want to check if player pressed ANY 3 buttons, try this, it works.
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(!IsPlayerInAnyVehicle(playerid))
    {
        new count = 0;
        for(new i = 0; i < 20; i++)
            count += (newkeys >> i) & 1;
        if(count == 3)
        {
            SendClientMessageToAll(-1, "3 buttons!");
            //TODO...
        }
    }
    return 1;
}
Reply
#7

Quote:

If you want to check if player pressed ANY 3 buttons, try this, it works.

I can use a easy way to do this one, so with your example codes "It's will check if he press all of the 3 buttons in the same time"?
Reply
#8

My example checks all possible buttons and count pressed ones. So, if count == 3, it will perform action.



If you need to check specified buttons, you should use this example.

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(IsKeyPressed(newkeys, oldkeys, KEY_FIRE) && IsKeyPressed(newkeys, oldkeys, KEY_SPRINT) && IsKeyPressed(newkeys, oldkeys, KEY_JUMP))
    {
         //TODO...
    }
    return 1;
}
Reply
#9

As Borg said above, if you want 3 specific keys to be checked, use my code without your Test code.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)