Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Easy Keystate and Single Press Key Function

Author
Message
Qqite
12
Years of Service
User Offline
Joined: 28th Feb 2010
Location: Florida, U.S.
Posted: 4th Oct 2011 14:29 Edited at: 4th Oct 2011 21:09
This is a very simple, yet extremely helpful function to have on any project.

What it does:
Every call, it will see if you have just pressed a key to send only once and the general keystate.

Instead of doing this every time you want a key to only do something once (like toggle a debug window or fire a weapon)



Now you only need to call this:


Since I needed a lot of toggle buttons, I find myself using this for every time I use Keystate.

The second parameter is: 0 for general keystate, 1 will =1 only the first loop the key is pressed.

Heres the code:
<<<<<>>>>>



<<<<<>>>>>

Ventures of the worlds around us are limited only by our imagination.
noobnerd
12
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 10th Oct 2011 18:18
i usually use a very similiar piece of code:



the advantage with this one is that you wont have to do the for loop.

use it like this:

if keypress(key) = 1 (for normal mode )
if keypress(key) = 2 (for toggle mode )
Qqite
12
Years of Service
User Offline
Joined: 28th Feb 2010
Location: Florida, U.S.
Posted: 10th Oct 2011 19:50
@noobnerd (nice name )

I know this gets so nit-picky that it doesn't make a difference, but doing the function once and then calling on a variable is slightly faster than the other way around.

Otherwise saying if I used your code, that function would be called quite a bit testing the same thing multiple times.

Both are viable on today's computers, but my function might run slightly faster.

Ventures of the worlds around us are limited only by our imagination.
noobnerd
12
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 10th Oct 2011 23:06 Edited at: 10th Oct 2011 23:08
hehe

im not sure about that

if you look at your code
it does about the same as mine but it does it 88 times.

and as calling a function doesnt noticeably slow down your code, i think mine would be faster as long as you use less than 60 or so keys?

does seem so to me

but yes checking variables takes practically 0 time so if you have to check keys like 1000 times in your code then your would be lots faster ( by lots i mean like 5 times faster or 1 millisecond or so )
basjak
12
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 13th Oct 2011 04:10
Qqite
12
Years of Service
User Offline
Joined: 28th Feb 2010
Location: Florida, U.S.
Posted: 13th Oct 2011 06:35
@basjak

Your example is the whole reason noobnerd and I created these functions. Because creating variables or functions for EVERY keypress would get tedious and bloated. Instead to typing your code for every time you need a keypress, you could just use either my way (variable) or noobnerd's way (general function). These ways cut down a LOT of code and makes things easier to read.

Ventures of the worlds around us are limited only by our imagination.
basjak
12
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 13th Oct 2011 16:19
you function is cool but it may create trouble when a loop starts to get complicated. I tried to give you an example where the app runs smoothly with minimum amount of internal loops and memory wastage.

here you go a better code:
noobnerd
12
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 13th Oct 2011 23:02
oh basjak nice, but what if you press 4 buttons at the same time?
basjak
12
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 14th Oct 2011 00:29
in fact am not fan of scancode and keystate commands because in some systems (not all), if the user pressed caps lock by mistake, the scancode command will be corrupted silently.

about the code a wrote can be easily modified using inkey$() which accept multiple buttons entry.

Login to post a reply

Server time is: 2022-11-30 14:52:29
Your offset time is: 2022-11-30 14:52:29