[Tutorial] Key Presses, and how to use them!(KeyStateChange)
#1

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.
Код:
#include a_samp
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
Reply
#2

Try and use the tags or the tags mate, makes it easier for others to understand.
Reply
#3

Updated.
Reply
#4

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

could you add the ids of all the keys?
Reply
#6

Quote:
Originally Posted by Sagna
Посмотреть сообщение
could you add the ids of all the keys?
https://sampwiki.blast.hk/wiki/Keys
Reply
#7

nice tutorial....
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)