Detecting if player click a key.
#1

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
Reply
#2

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;
}
Reply
#3

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.
Reply
#4

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.
Reply
#5

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.
Reply
#6

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.
Reply
#7

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.
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)