Raised This Month: $51 Target: $400
 12% 

[SNIPPET] Detecting button presses (and releases)


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
psychonic

BAFFLED
Join Date: May 2008
Old 02-22-2011 , 11:53   [SNIPPET] Detecting button presses (and releases)
Reply With Quote #1

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

Last edited by psychonic; 03-03-2012 at 11:51. Reason: typofix
psychonic is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:24.


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