AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [SNIPPET] Detecting button presses (and releases) (https://forums.alliedmods.net/showthread.php?t=151142)

psychonic 02-22-2011 11:53

[SNIPPET] Detecting button presses (and releases)
 
It seems some people still struggle with this so here is a copy/paste way of detecting click button presses/releases in your plugins.

Make sure to include sdktools at the top of your plugin if it's not there already
Code:
#include <sdktools>

These also need to go somewhere near the top of the file
Code:
#define MAX_BUTTONS 25 new g_LastButtons[MAXPLAYERS+1];

If you don't already have these forwards, add them, else just add the contents
Code:
public OnClientDisconnect_Post(client) {     g_LastButtons[client] = 0; } public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon) {     for (new i = 0; i < MAX_BUTTONS; i++)     {         new button = (1 << i);                 if ((buttons & button))         {             if (!(g_LastButtons[client] & button))             {                 OnButtonPress(client, button);             }         }         else if ((g_LastButtons[client] & button))         {             OnButtonRelease(client, button);         }     }         g_LastButtons[client] = buttons;         return Plugin_Continue; }

Now you can use these function to detect the buttons
Code:
OnButtonPress(client, button) {     // do stuff } OnButtonRelease(client, button) {     // do stuff }

List of detectable buttons at time of writing (full list here):
Code:
#define IN_ATTACK       (1 << 0) #define IN_JUMP         (1 << 1) #define IN_DUCK         (1 << 2) #define IN_FORWARD      (1 << 3) #define IN_BACK         (1 << 4) #define IN_USE          (1 << 5) #define IN_CANCEL       (1 << 6) #define IN_LEFT         (1 << 7) #define IN_RIGHT        (1 << 8) #define IN_MOVELEFT     (1 << 9) #define IN_MOVERIGHT        (1 << 10) #define IN_ATTACK2      (1 << 11) #define IN_RUN          (1 << 12) #define IN_RELOAD       (1 << 13) #define IN_ALT1         (1 << 14) #define IN_ALT2         (1 << 15) #define IN_SCORE        (1 << 16)       // Used by client.dll for when scoreboard is held down #define IN_SPEED        (1 << 17)   // Player is holding the speed key #define IN_WALK         (1 << 18)   // Player holding walk key #define IN_ZOOM         (1 << 19)   // Zoom key for HUD zoom #define IN_WEAPON1      (1 << 20)   // weapon defines these bits #define IN_WEAPON2      (1 << 21)   // weapon defines these bits #define IN_BULLRUSH     (1 << 22) #define IN_GRENADE1     (1 << 23)   // grenade 1 #define IN_GRENADE2     (1 << 24)   // grenade 2

mysticssjgoku4 06-10-2011 23:51

Re: [SNIPPET] Detecting button presses (and releases)
 
This does not work fully with the most recent version of CSS.

If I press the use key, it does not detect it until I hold a movement key as well.

I do have sort of a newbish question what does:
Code:

        if ((buttons & button) && (g_LastButtons[id] & ~button))
        {
            OnButtonPress(id, button);
        }

do?

I understand that the if statement is looking for both sets to return true, but what does "(buttons & button)" mean? I understand it looks for "button" as apart of "buttons" but is that all?

DJ Tsunami 06-11-2011 03:30

Re: [SNIPPET] Detecting button presses (and releases)
 
It checks if button is in buttons using bitwise operation.

Cookies.net 06-12-2011 13:58

Re: [SNIPPET] Detecting button presses (and releases)
 
I just noticed this thread and tried this method and it doesn't work properly (tried it myself instead of the way i currently do it), here's my explanation (it might not be the best, but i guess its fine)

e.g. if "button" in this case = "IN_JUMP" then it would check the current buttons if they contains "IN_JUMP" ("buttons & IN_JUMP") then it will proceed to "g_LastButtons[client] & ~IN_JUMP", here it will check if the last buttons contains anything else than IN_JUMP, so it will trigger if you held any other buttons than "IN_JUMP" the last frame. So psychonics snippet wont trigger when you jump, but it will trigger several times if you're e.g. walking forward and pressing your jump button.

what i did before and after i tried this would be something like this
PHP Code:

    if ((buttons button) && !(g_LastButtons[client] & button))
    {
        
OnButtonPress(clientbutton);
    }
        
    if ((
g_LastButtons[client] & button) && !(buttons button))
    {
        
OnButtonRelease(clientbutton);
    } 

which works just perfect

bl4nk 06-13-2011 02:12

Re: [SNIPPET] Detecting button presses (and releases)
 
Wouldn't that be better written like this?
PHP Code:

if ((buttons button)) {
    if (!(
g_LastButtons[client] & button)) {
        
OnButtonPress(clientbutton);
    }
} else if ((
g_LastButtons[client] & button)) {
    
OnButtonRelease(clientbutton);



Despirator 06-18-2011 13:57

Re: [SNIPPET] Detecting button presses (and releases)
 
Quote:

Originally Posted by bl4nk (Post 1486959)
Wouldn't that be better written like this?
PHP Code:

if ((buttons button)) {
    if (!(
g_LastButtons[client] & button)) {
        
OnButtonPress(clientbutton);
    }
} else if ((
g_LastButtons[client] & button)) {
    
OnButtonRelease(clientbutton);



ye it's better

_pHabb 01-31-2016 15:09

Re: [SNIPPET] Detecting button presses (and releases)
 
How to create pressing IN_USE ~several seconds for activate something?

KissLick 01-31-2016 15:14

Re: [SNIPPET] Detecting button presses (and releases)
 
What about?

Pressed IN_USE - start 3s timer
Released IN_USE - destroy that timer
OnTimer - do something

_pHabb 01-31-2016 15:34

Re: [SNIPPET] Detecting button presses (and releases)
 
KissLick, thanks, i will try

mottzi 04-05-2016 06:01

Re: [SNIPPET] Detecting button presses (and releases)
 
@psychonic why are you creating the variable inside the loop?

shavit 04-09-2016 16:33

Re: [SNIPPET] Detecting button presses (and releases)
 
Quote:

Originally Posted by mottzi (Post 2408302)
@psychonic why are you creating the variable inside the loop?

nothing stops you from making it static globally and setting a value inside

mottzi 04-09-2016 17:38

Re: [SNIPPET] Detecting button presses (and releases)
 
True, but since this is the Snippets and Tutorials section and many newcommers take code from here I suggest that such bad habits like defining variables inside loops would be avoided. No static needed, the best thing would be to declare the variable outside the loop.

humbugtheman 04-10-2016 06:01

Re: [SNIPPET] Detecting button presses (and releases)
 
What makes you think that "defining a variable inside a loop" is a bad habit????

mottzi 04-10-2016 10:04

Re: [SNIPPET] Detecting button presses (and releases)
 
After some research on the internet I found that I'm wrong about the variables. Somehow I thought it would be inefficient. :)

Spirit_12 01-17-2018 19:40

Re: [SNIPPET] Detecting button presses (and releases)
 
Is there a way to hook other buttons that are not on the list? To be specific, I'm trying to hook lastinv or slot1 key presses.

psychonic 01-17-2018 19:53

Re: [SNIPPET] Detecting button presses (and releases)
 
Quote:

Originally Posted by Spirit_12 (Post 2572626)
Is there a way to hook other buttons that are not on the list? To be specific, I'm trying to hook lastinv or slot1 key presses.

Those are client commands that do not get transmitted to the server (as commands nor buttons). You'd have to watch the weapon parameter in OnPlayerRunCommand for having a value (and then take as-is or infer the command from there).

hmmmmm 03-09-2018 17:55

Re: [SNIPPET] Detecting button presses (and releases)
 
I've found a bit of a sexier way of doing this, just have to get m_afButtonReleased and m_afButtonPressed data props
These variables store the buttons released/pressed that frame, or 0 if nothing was released/pressed

Spirit_12 08-15-2018 16:26

Re: [SNIPPET] Detecting button presses (and releases)
 
Quote:

Originally Posted by hmmmmm (Post 2582164)
I've found a bit of a sexier way of doing this, just have to get m_afButtonReleased and m_afButtonPressed data props
These variables store the buttons released/pressed that frame, or 0 if nothing was released/pressed

Would you be able to post a snippet for that?

hmmmmm 08-16-2018 02:49

Re: [SNIPPET] Detecting button presses (and releases)
 
PHP Code:

if( GetEntPropclientProp_Data"m_afButtonPressed" ) & IN_USE 

PHP Code:

if( GetEntPropclientProp_Data"m_afButtonReleased" ) & IN_ATTACK 

This works on CS:GO, not sure about other games.


All times are GMT -4. The time now is 08:51.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.