TGC Codebase Backup



Stopping Key And Mouse Button Repeating by WhizzRich

13th May 2004 5:56
Summary

To make a key or mouse button work only once, no matter how long you hold it for.



Description

For use in every single game you could possibly make.

For example: when you need to press S to save the game, you don't want the game saving for every game loop the S button is held down. You only want it to save the one time you press S, and to save it again you would let go and press S again.

Should be easy enough to adapt the code for joysticks. You don't need the repeat part of the code, it is just simply much quicker than writing the code for every button on the keyboard.

*Attached is a picture of a keyboard and the ASCII values for each button.

Thanks,
Rich (WhizzRich)



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    dim key(250) : dim oldkey(250)

if mouseclick > 0 then oldmouse = 1
if mouseclick() > 0 and oldmouse = 0 : mouseclick = mouseclick() : else : mouseclick = 0 : endif
if mouseclick() = 0 then oldmouse = 0

for a = 1 to 250
   if key(a) = 1 then oldkey(a) = 1
   if keystate(a) = 1 and oldkey(a) = 0 : key(a) = 1 : else : key(a) = 0 : endif
   if keystate(a) = 0 then oldkey(a) = 0
next a

`when actually checking the key, simply do this:
if key(scancode of key) = 1

`and for checking a mouse button (left button = 1, right button = 2 etc):
if mouseclick = 1