SA-MP Forums Archive
OnPlayerKeyStateChange. - 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)
+--- Thread: OnPlayerKeyStateChange. (/showthread.php?tid=569734)



OnPlayerKeyStateChange. - Ritzy2K - 02.04.2015

Hello, I'm writing a script in which i want that that the KEY_FIRE normally LMB should close all the textdraws that the player is having on the screen either its a menu or whatever..
Though, i have been reading through wiki and managed to make it. It works like it should do.
Since now directional keys are NOT identified by SA-MP so it doesnt matter if i press W, A, S And D. while pressing these keys if i still press LMB it works...but while sprinting or crouching or jumping it doesnt work.. my code is like -

pawn Код:
if (newkeys == KEY_FIRE)
    {
        HideAllServerTextDraws(playerid);
        HideTextDrawMenu(playerid);
Ill quote wiki once.

Код:
Let's presume that you want to detect when a player presses their FIRE button, the obvious code would be:
if (newkeys == KEY_FIRE)
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".
[edit]
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:
if (newkeys & KEY_FIRE)
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?
soo do i change mine to -

pawn Код:
if (newkeys & KEY_FIRE)
    {
        HideAllServerTextDraws(playerid);
        HideTextDrawMenu(playerid);
This will close the textdraw when pressing any other key with LMB.. But i think it ll ONLY CLOSE the textdraw while i press any other key with LMB? I simply want to change it into rather i only press LMB or any keys With LMB


Re: OnPlayerKeyStateChange. - Crayder - 02.04.2015

it will only work when you press the key. if the binary operation confuses you, you should use the defines provided on the wiki. Id explain but im typing on my phone. Using binary &, it check if the key is flagged in the newkeys variable, if it is the expression is true.


Re: OnPlayerKeyStateChange. - CalvinC - 02.04.2015

It will work if you press any other keys, combined with LMB, or just pressing LMB.
It doesn't require you to press other keys.


Re: OnPlayerKeyStateChange. - Ritzy2K - 02.04.2015

Thanks for the help Both :P