Key Presses, and how to use them!(KeyStateChange) -
Abagail - 01.02.2014
Key Presses!
Tutorial by: Abagail
https://sampwiki.blast.hk/wiki/OnPlayerKeyStateChange
Edit: This is quite an old, albeit functional, tutorial. I have taken a few minutes to revisit and revise this tutorial, hopefully it clears any grammatical errors.
Before continuing with anything else, we need to let the compiler know that we are including an include file. In this case, we only need the default SA-MP include.
The following defines allow you to detect if a key is being pressed, held, or if it has just been released. Note that you can only detect the keys listed
here.
Код:
#define PRESSED(%0) \
(((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
// The key has been pressed.
Код:
#define RELEASED(%0) \
(((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
// The key has been released.
// HOLDING(keys)
#define HOLDING(%0) \
((newkeys & (%0)) == (%0))
// The key is being held down.
We will be interacting with key presses through the OnPlayerKeyStateChange callback. This callback is called when the player uses one of the available detection keys(not any other). It can be called quite frequently, so avoid placing unconditional code.
Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
The list of available keys and their defined macros are available below.
here.
For this example, we will be giving the player NOS upon them pressing the fire key(defined as
KEY_FIRE). If we want to give the player nitro when they are holding the key, we can use the HOLDING macro as such:
Код:
if (HOLDING( KEY_FIRE )
If we want to give the player nitro after they press the key, we can use the PRESSED macro as such:
Код:
if (PRESSED( KEY_FIRE )
If we want to give the player nitro after they release the key, we can use the RELEASED macro as such:
Код:
if (RELEASED( KEY_FIRE )
If we put this together, we can put code in the underlying code block and proceed to do what we want.
Код:
if (PRESSED( KEY_FIRE )
{
AddVehicleComponent(GetPlayerVehicleID(playerid),101);
}
Reference:
OnPlayerKeyStateChange - SAMP Wiki
Detectable keys - SAMP Wiki
Re: Key Presses, and how to use them!(KeyStateChange) -
ross8839 - 03.02.2014
Try and use the tags or the tags mate, makes it easier for others to understand.
Re: Key Presses, and how to use them!(KeyStateChange) -
Abagail - 19.02.2014
Updated.
Re: Key Presses, and how to use them!(KeyStateChange) -
redneckvideogamer - 20.02.2014
There are errors on the script on every if statement:
Код:
if(PRESSED (KEY FIRE)
... rest of code
should be:
Код:
if(PRESSED (KEY FIRE))
... rest of code
and so on.
Re: Key Presses, and how to use them!(KeyStateChange) -
Sagna - 27.02.2014
could you add the ids of all the keys?
Re: Key Presses, and how to use them!(KeyStateChange) -
Jimmy0wns - 27.02.2014
Quote:
Originally Posted by Sagna
could you add the ids of all the keys?
|
https://sampwiki.blast.hk/wiki/Keys
Re: Key Presses, and how to use them!(KeyStateChange) -
Gnabry - 28.02.2014
nice tutorial....