[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:
These also need to go somewhere near the top of the file Code:
If you don't already have these forwards, add them, else just add the contents Code:
Now you can use these function to detect the buttons Code:
List of detectable buttons at time of writing (full list here): Code:
|
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)) 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? |
Re: [SNIPPET] Detecting button presses (and releases)
It checks if button is in buttons using bitwise operation.
|
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:
|
Re: [SNIPPET] Detecting button presses (and releases)
Wouldn't that be better written like this?
PHP Code:
|
Re: [SNIPPET] Detecting button presses (and releases)
Quote:
|
Re: [SNIPPET] Detecting button presses (and releases)
How to create pressing IN_USE ~several seconds for activate something?
|
Re: [SNIPPET] Detecting button presses (and releases)
What about?
Pressed IN_USE - start 3s timer Released IN_USE - destroy that timer OnTimer - do something |
Re: [SNIPPET] Detecting button presses (and releases)
KissLick, thanks, i will try
|
Re: [SNIPPET] Detecting button presses (and releases)
@psychonic why are you creating the variable inside the loop?
|
Re: [SNIPPET] Detecting button presses (and releases)
Quote:
|
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.
|
Re: [SNIPPET] Detecting button presses (and releases)
What makes you think that "defining a variable inside a loop" is a bad habit????
|
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. :)
|
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.
|
Re: [SNIPPET] Detecting button presses (and releases)
Quote:
|
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 |
Re: [SNIPPET] Detecting button presses (and releases)
Quote:
|
Re: [SNIPPET] Detecting button presses (and releases)
PHP Code:
PHP Code:
|
All times are GMT -4. The time now is 08:51. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.