This callback is called when the state of any supported key is changed (pressed/released).
Directional keys do not trigger OnPlayerKeyStateChange (up/down/left/right).
|playerid||The ID of the player that pressed or released a key.|
|newkeys||A map (bitmask) of the keys currently held - see here|
|oldkeys||A map (bitmask) of the keys held prior to the current change - see here.|
- This callback does not handle returns.
- It is always called first in gamemode.
This callback can also be called by NPC.
- GetPlayerKeys: Check what keys a player is holding.
This callback is called whenever a player presses or releases one of the supported keys (see Keys).
The keys which are supported are not actual keyboard keys, but San Andreas mapped function keys, this means that, for example, you can't detect when someone presses the spacebar, but can detect when they press their sprint key (which may, or may not, be assigned to the spacebar (it is by default)).
The parameters to this function are a list of all keys currently being held down and all the keys held down a moment ago. The callback is called when a key state changes (that is, when a key is either pressed or released) and passes the states or all keys before and after this change. This information can be used to see exactly what happened but the variables can not be used directly in the same way as parameters to other functions. To reduce the number of variables only a single BIT is used to represent a key, this means that one variable may contain multiple keys at once and simply comparing values will not always work.
How to NOT to check for a key
Let's presume that you want to detect when a player presses their FIRE button, the obvious code would be:
This code may even work in your testing, but it is wrong and your testing is insufficient. Try crouching and pressing fire - your code will instantly stop working. Why? Because "newkeys" is no longer the same as "KEY_FIRE", it is the same as "KEY_FIRE" COMBINED WITH "KEY_CROUCH".
How to check for a key
So, if the variable can contain multiple keys at once, how do you check for just a single one? The answer is bit masking. Each key has its own bit in the variable (some keys have the same bit, but they are onfoot/incar keys, so can never be pressed at the same time anyway) and you need to check for just that single bit:
Note that the single & is correct - this is a bitwise AND, not a logical AND, which is what the two ampersands are called.
Now if you test this code it will work whether you are crouching or standing when you press the fire key. However there is still one slight problem - it will fire as long as you are holding the key. OnPlayerKeyStateChange is called every time a key changes and that code is true whenever the the fire key is held down. If you press fire the code will fire, if that key is held and you press crouch - that code will fire again because a key (crouch) has changed and fire is still held down How do you detect when a key is first pressed, but not trigger again when it's still held and another key changes?
How to check for a key that has been pressed
This is where "oldkeys" comes in. To check if a key has just been pressed you need to first check whether it is set in "newkeys" - meaning it's held down, and then check that it's NOT in "oldkeys" - meaning it's only just been held down. The following code does this:
That will ONLY be true when the FIRE key is first pressed, not when it's held and another key changes.
How to check for a key being released
Exactly the same principle as above, but reversed:
How to check for multiple keys
If you want to check for players HOLDING crouch and fire then the following code will work fine:
However if you want to detect when they FIRST press fire and crouch the following code WILL NOT work. It will work if they manage to press the two keys at exactly the same time, but if they're fractionally out (far less than half a second) it won't:
Why not? Because OnPlayerKeyStateChange is called every time a single key changes. So they press "KEY_FIRE" - OnPlayerKeyStateChange is called with "KEY_FIRE" in "newkeys" and not in "oldkeys", then they press "KEY_CROUCH" - OnPlayerKeyStateChange is called with "KEY_CROUCH" and "KEY_FIRE" in "newkeys", but "KEY_FIRE" is now also in "oldkeys" as it's already been pressed, so "!(oldkeys & KEY_FIRE)" will fail. Fortunately the solution is very simple (in fact simpler than the original code):
This may look complicated, but it checks that both keys are set in "newkeys" and that both the keys were not set in "oldkeys", if one of them was set in "oldkeys" that doesn't matter as not both of them were. All these things can be simplified greatly with defines.
Detecting holding a key
Holding one key:
Holding multiple keys:
Detecting first pressing a key
Pressed one key:
Pressed multiple keys:
Detecting if a player is pressing a key currently
Pressing one key:
Pressing multiple keys:
Detecting releasing a key
Released one key:
Released multiple keys:
Attach NOS when the player presses fire
God mode while holding use
You don't need to worry about HOW it's done, just that it is. HOLDING detects if they're pressing a key (or keys), regardless of wether they were pressing it before, PRESSED detects if they only just pressed the key(s) and RELEASED detects if they just released a key(s). However if you want to know more - read on.
The reason why you need to do it this way, not just using & or ==, is to detect exactly the keys you want while ignoring others which may or may not be pressed. In binary KEY_SPRINT is:
and KEY_JUMP is:
thus ORing them into the wanted keys (we could also add them in this example but that's not always the case) gives:
If we were only using & and OnPlayerKeyStateChange was called for a player pressing jump we would get the following code:
The AND of the two numbers is not 0, thus the result of the check is true, which isn't what we want.
If we only used == the two numbers are clearly not the same thus the check would fail, which is what we want.
If the player was pressing jump, sprint and crouch we would get the following code:
The ANDed version is the same as the required keys and also not 0, thus will give the correct answer, however the two original numbers are not the same so == will fail. In both the examples one of the two has given the right answer and one has given the wrong answer. If we compare the first one using & and == we get:
Obviously wanted and ANDed are not the same so the check fails, which is correct. For the second example:
Wanted and ANDed are the same so comparing them as equal will result in a true result, which again is correct.
So using this method we can accurately check if certain keys are pressed ignoring all other keys which may or may not be pressed. the oldkeys check just uses != instead of == to ensure that the required keys were not previously pressed, so we know one of them was just pressed.