SA-MP Forums Archive
Detecting if player click a key. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Detecting if player click a key. (/showthread.php?tid=266159)



Detecting if player click a key. - Madsen - 03.07.2011

I want to detect if a player rightclicks and then they get a boost/turbo. i do not know how to check if they rightclick i have tried to look through wiki but did not really understand it.
my code:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if (newkeys == 128)
    {
        new vehid = GetPlayerVehicleID(playerid);
        if(IsPlayerInAnyVehicle(playerid))
        {
            if (turbo == 1)
            {
                new Float:Velocity[3];
                GetVehicleVelocity(vehid, Velocity[0], Velocity[1], Velocity[2]);
                if(Velocity[0] < maxspeed  && Velocity[1] < maxspeed && Velocity[0] > -maxspeed && Velocity[1] > -maxspeed)
                {
                    SetVehicleVelocity(vehid, Velocity[0]*speed, Velocity[1]*speed, 0.0);
                }
            }
        }
    }
    return 1;
}
Thank you


Re: Detecting if player click a key. - Bakr - 03.07.2011

You will be using the bit-wise AND check. I would go into detail why, but it explains thoroughly on the wiki. Take a look at this page: https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange

Also, to check if the player clicked the right mouse button (which would be fire button), you can use the KEY_FIRE definition.
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{    
   if (newkeys & KEY_FIRE)    
   {        
      new vehid = GetPlayerVehicleID(playerid);        
      if(IsPlayerInAnyVehicle(playerid))        
      {            
         if (turbo == 1)            
         {                
            new Float:Velocity[3];                
            GetVehicleVelocity(vehid, Velocity[0], Velocity[1], Velocity[2]);                
            if(Velocity[0] < maxspeed  && Velocity[1] < maxspeed && Velocity[0] > -maxspeed && Velocity[1] > -maxspeed)                
            {                    
               SetVehicleVelocity(vehid, Velocity[0]*speed, Velocity[1]*speed, 0.0);                
            }            
         }        
      }    
   }    
   return 1;
}



Re: Detecting if player click a key. - Madsen - 03.07.2011

Quote:
Originally Posted by Bakr
Посмотреть сообщение
You will be using the bit-wise AND check. I would go into detail why, but it explains thoroughly on the wiki. Take a look at this page: https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange

Also, to check if the player clicked the right mouse button (which would be fire button), you can use the KEY_FIRE definition.
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{    
   if (newkeys & KEY_FIRE)    
   {        
      new vehid = GetPlayerVehicleID(playerid);        
      if(IsPlayerInAnyVehicle(playerid))        
      {            
         if (turbo == 1)            
         {                
            new Float:Velocity[3];                
            GetVehicleVelocity(vehid, Velocity[0], Velocity[1], Velocity[2]);                
            if(Velocity[0] < maxspeed  && Velocity[1] < maxspeed && Velocity[0] > -maxspeed && Velocity[1] > -maxspeed)                
            {                    
               SetVehicleVelocity(vehid, Velocity[0]*speed, Velocity[1]*speed, 0.0);                
            }            
         }        
      }    
   }    
   return 1;
}
you sure your not thinking of left mouse buttom now ? anyway i will check it but fire key is normally left.


Re: Detecting if player click a key. - Vince - 03.07.2011

Right mouse button is the aim key, for your information (unless you're left handed and you have your mouse on the left side of the keyboard). However, keys are different when in a vehicle. I would suggest printing the values for RMB under OnPlayerKeyStateChange.

pawn Код:
printf("oldkeys: %d - newkeys: %d", oldkeys, newkeys);
Go ingame and get in a vehicle. Press right mouse button and then /q right away. It should print a value other than 0 for newkeys in the server windows.


Re: Detecting if player click a key. - Bakr - 03.07.2011

Ah yes, sorry about that. You will need to use the KEY_AIM definition.

Note, that it's not defined by SA:MP includes (for some reason), so you'll need to manually do it yourself.
pawn Код:
#define KEY_AIM      (128) // According to Wiki
EDIT: Vince may be right, I heard about keys having different values while on foot/in a vehicle. Use his suggestion.


Re: Detecting if player click a key. - MadeMan - 03.07.2011

Quote:
Originally Posted by Bakr
Посмотреть сообщение
I heard about keys having different values while on foot/in a vehicle.
Also, players can change their keys in Options.


Re: Detecting if player click a key. - Bakr - 03.07.2011

If I understand correctly, that's not how the keys are detected. Whether you have fire button set to LMB, RMB or LCTR, it will always return the KEY_FIRE value. Please correct me if I'm wrong, but I'm pretty confident that is correct.


Re: Detecting if player click a key. - Madsen - 03.07.2011

Quote:
Originally Posted by Vince
Посмотреть сообщение
Right mouse button is the aim key, for your information (unless you're left handed and you have your mouse on the left side of the keyboard). However, keys are different when in a vehicle. I would suggest printing the values for RMB under OnPlayerKeyStateChange.

pawn Код:
printf("oldkeys: %d - newkeys: %d", oldkeys, newkeys);
Go ingame and get in a vehicle. Press right mouse button and then /q right away. It should print a value other than 0 for newkeys in the server windows.
right click does not show anything so i dont think you can use that. I will just use another buttom.

thank you for your help.