GLBasic forum

Main forum => GLBasic - en => Topic started by: quick2code on 2009-Nov-10

Title: KeyHit Problems
Post by: quick2code on 2009-Nov-10
I'm having problems with KeyHit that Gernot created. I want to press a key and start a sprite moving. I don't want the sprite to stop until I press the same key again. What I do doesn't seem to work. Here's the code. Thanks for any info.
Code (glbasic) Select

KeyHitUpdate()

IF KeyHit(57) = 2
    locX = locX + dirX
ELSE
    locX = locX
ENDIF
Title: Re: KeyHit Problems
Post by: Hemlos on 2009-Nov-10
Gernots keyhit has multiple status', perhaps you arent using the correct one?

However, in case you cant get that to work for your needs, i have different key hit sample here.
The trick with this function is it always returns -1, except when a key is initially pressed.
When a key is hit initially, it will return the key code number, for this frame only.
Basically, it is a "light" version of Keyhit sample, with much less options.
It is specialized for only operating during that initial key hit.
One thing to note: The function scans key codes 0 through 220, you might want to tinker with this, if it isnt scanning the your button code.

Code (glbasic) Select


WHILE TRUE

KeyPressed=KeyBoardIO()

IF KeyPressed=57
IsSpriteMoving=IsSpriteMoving+1
IF IsSpriteMoving>1 THEN IsSpriteMoving=0
ENDIF

//this is just a sample of a moving sprite:
IF IsSpriteMoving=1 THEN angle=angle+1
x=200+COS(angle)*100
y=200+SIN(angle)*100
PRINT "Player1",x,y

PRINT "Press Space to Start/Stop Movement",10,10
SHOWSCREEN

WEND



// KeyBoardIO() will always return -1, except when a key is hit, return that key code one time.
FUNCTION KeyBoardIO:

LOCAL KeyIndex
LOCAL IsKeyPressed=0
LOCAL IsKeyLocked=1

//Prepare the memory:
STATIC INITIALIZE
IF INITIALIZE=FALSE; INITIALIZE=TRUE;
DIM KeyBuffer[221][2]
KeyBuffer[0][0]=0
ENDIF

//Check all key holding states:
FOR KeyIndex=0 TO 220
IF KEY(KeyIndex)
KeyBuffer[KeyIndex][IsKeyPressed]=TRUE //Key pressed for the first time.
IF KeyBuffer[KeyIndex][IsKeyLocked]=FALSE
KeyBuffer[KeyIndex][IsKeyLocked]=TRUE
RETURN KeyIndex
ENDIF
ELSE
KeyBuffer[KeyIndex][IsKeyPressed]=FALSE //Key held
KeyBuffer[KeyIndex][IsKeyLocked]=FALSE
ENDIF
NEXT
RETURN -1 //dont send anything
ENDFUNCTION
Title: Re: KeyHit Problems
Post by: Moru on 2009-Nov-10
Quote from: quick2code on 2009-Nov-10
I'm having problems with KeyHit that Gernot created. I want to press a key and start a sprite moving. I don't want the sprite to stop until I press the same key again. What I do doesn't seem to work. Here's the code. Thanks for any info.
Code (glbasic) Select

KeyHitUpdate()
global move% = 0   // This will store the state we are in. 0 for still, -1 for moving

IF KeyHit(57) = 2  // If user pressed key, start movement
    move = bNOT(move)   // This will alternate between 0 and -1
ENDIF

IF move = -1          // If key was pressed earlier sometime, keep moving
    locX = locX + dirX
ENDIF


You are using the KeyHit correctly but you need to save the state somewhere when the user hits the button. See the changed code above
Title: Re: KeyHit Problems
Post by: quick2code on 2009-Nov-10
Thank you kindly gentlemen.