Liberty BASIC Community Forum
« arrow key up »

Welcome Guest. Please Login or Register.
Jan 22nd, 2018, 5:51pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: arrow key up  (Read 233 times)
kevin
Senior Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 418
xx arrow key up
« Thread started on: Jun 21st, 2017, 04:12am »

Good morning
Like there is a command that when the user releases the button on the mouse , Is there command the when you release the arrow key a reaction takes place?

Eg


Code:
leftButtonUp - the left mouse button has been released 

« Last Edit: Jun 21st, 2017, 04:13am by kevin » User IP Logged

KEVIN
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5618
xx Re: arrow key up
« Reply #1 on: Jun 21st, 2017, 05:10am »

You will need to track the key state yourself. You must do this repetitively.

This code shows one example of using GetAsyncKeyState() search out other examples so you understand how best to use it.

This code would be in the drawing loop, there would be no when character event. This eliminates the stop start action that windows auto repeat introduces to programs using the when character event.

notice I change variables that will drive the sprite movement if a key is pressed.

Code:
' read the keyboard

        'escape/quit
        CallDLL #user32, "GetAsyncKeyState",_VK_ESCAPE AS long,k1 AS long
        if k1<0 then Goto [quit]

        'left
        CallDLL #user32, "GetAsyncKeyState",_VK_LEFT AS long,k1 AS long
        CallDLL #user32, "GetAsyncKeyState",4 AS long,k2 AS long
        if k1<0 or k2<0 then deltaX=-10

        'right
        CallDLL #user32, "GetAsyncKeyState",_VK_RIGHT AS long,k1 AS long
        CallDLL #user32, "GetAsyncKeyState",6 AS long,k2 AS long
        if k1<0 or k2<0 then deltaX=10

        'up
        CallDLL #user32, "GetAsyncKeyState",_VK_UP AS long,k1 AS long
        CallDLL #user32, "GetAsyncKeyState",8 AS long,k2 AS long
        if k1<0 or k2<0 then deltaY=-10

        'down
        CallDLL #user32, "GetAsyncKeyState",_VK_DOWN AS long,k1 AS long
        CallDLL #user32, "GetAsyncKeyState",2 AS long,k2 AS long
        if k1<0 or k2<0 then deltaY=10

 



If you explain more about the problem you are trying to solve we can see if GetAsyncKeyState() is the correct solution.
User IP Logged

kevin
Senior Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 418
xx Re: arrow key up
« Reply #2 on: Jun 21st, 2017, 07:52am »

Thank you Mr Rod
in this small Game when you press the left or right key the sprote moves constantly so what I am trying to workout making the sprit sop when the arrow key is depressed
Code:
  Nomainwin
' OldKey holds last pressed key
    OldKey = 0

' xDir and yDir hold moving directions
    xDir = 0: yDir = 0
    UpperLeftX = 10
    UpperLeftY = 10
    WindowWidth = 757
    WindowHeight = 595

    'UpperLeftX = Int((DisplayWidth - WindowWidth) /2)
    'UpperLeftY = Int((DisplayHeight - WindowHeight) /2)
    'Barel1

    Menu #demo, "&Options", "E&xit", [Quit]
    Graphicbox #demo.gb1, 0, 0, 750, 550

    Open "Controlling Sprites" for Window as #demo
    #demo, "Trapclose [Quit]"
    Lives = 3
          '=================================


    hndl = hwnd(#demo)
    struct winRect, orgX as long, orgY as long, cornerX as long, cornerY as long
    calldll #user32, "GetClientRect", hndl as ulong, winRect as struct, result as boolean
    winx=winRect.cornerX.struct-winRect.orgX.struct
    winy=winRect.cornerY.struct-winRect.orgY.struct
    'notice "inside of window frame width = "; x; " height = "; y
         '=================================

' Load the background bmp
    Loadbmp "bg", "SPRITES\BG1.bmp"

    Loadbmp "Barel1", "SPRITES\Barel1.bmp"
    Loadbmp "Barel2", "SPRITES\Barel2.bmp"
    Loadbmp "Barel3", "SPRITES\Barel3.bmp"

    #demo.gb1, "Addsprite Barel Barel1 Barel2 Barel3"

percent=35
    #demo.gb1, "spritescale Barel ";percent

    #demo.gb1, "cyclesprite Barel -1"
   ' #demo.gb1, "cyclesprite Barel 1 once"




    x=670:y=470
    #demo.gb1, "spritexy Barel ";x;" ";y


    #demo.gb1, "Down; Background bg; Drawsprites"

' Load the sprites
    Loadbmp "cm1", "SPRITES\cave1.bmp"
    Loadbmp "cm2", "SPRITES\cave2.bmp"
    #demo.gb1, "Addsprite cm cm1 cm1 cm2 cm2"

' Set the initial cyclesprite command to 0
    #demo.gb1, "Cyclesprite cm 0"

' Set inital x, y variables (cm facing right to start)
    #demo.gb1, "Spritexy cm 350 450"

' Trap keypresses
    #demo.gb1, "When characterInput [KeyPress]"
    #demo.gb1, "Setfocus"

' Set the timer
    Timer 20, [SeeSprites]
    Wait

[Quit]
    Timer 0
    Unloadbmp "bg"
     unLoadbmp "Barel1"
      unLoadbmp "Barel2"
       unLoadbmp "Barel3"
       if over = 1 then Unloadbmp "over"
    Close #demo
    End

[KeyPress]
    NewKey = Asc(Right$(Inkey$, 1))
    #demo.gb1, "Spritexy? cm x y"
    Select Case NewKey
        Case 37
            #demo.gb1, "Spriteorient cm mirror"
            If OldKey = NewKey Then
                xDir = 0
                NewKey = 0
            Else
                xDir = 1
            End If
        Case 38
            yDir = 1
        Case 39
            #demo.gb1, "Spriteorient cm normal"
            If OldKey = NewKey Then'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

                xDir = 0
                NewKey = 0
            Else
                xDir = 2
            End If
    End Select
    OldKey = NewKey
    Wait

[SeeSprites]
IF Lives = 0 then
'load a bitmap from file
loadbmp "over", "sprites\over.bmp"
over = 1
'cause loaded bitmap to be the sprite background
#demo.gb1 "background over"
timer 0
end if
#demo.gb1, "spriteround Barel";
#demo.gb1, "spriteround cm";


#demo.gb1, "centersprite cm"
#demo.gb1, "centersprite Barel"
'COLLISION DITTECTION
#demo.gb1, "spritecollides cm list$";
IF list$ = "Barel" THEN
 Lives = Lives - 1
 #demo.gb1, "spritexy Barel 750 470"
 end if


#demo.gb1 "spritexy? cm x y"
if x > 700 then
cmx=1
cmy=450
#demo.gb1, "spritexy cm ";cmx;" ";cmy
end if

#demo.gb1 "spritexy? Barel x y"
if x < 1 then
#demo.gb1, "spritexy Barel 750 470";
end if
'#demo.gb1, "spritexy Barel 100 137";
'#demo.gb1,"spritexy Barel 100 137";
#demo.gb1, "spritemovexy Barel -2 0";
'IF SPRITE < 1 REPEAT
#demo.gb1 "spritexy? Barel x y"
'if x <5 then
    #demo.gb1, "Spritexy? cm x y"
    Select Case yDir
        Case 1 ' Up
            y = y - 10
            If y < 350 Then
                yDir = 2
                y = 350
            End If
        Case 2 ' Down
            y = y + 10
            If y > 450 Then
                yDir = 0
                y = 450
            End If
    End Select
    Select Case xDir
        Case 1 ' Left
            x = x - 7
            If x < 5 Then
                xDir = 0
                x = 10
            End If
        Case 2 ' Right
            x = x + 7
            If x > 710 Then
                xDir = 0
                x = 700
            End If
    End Select
    If xDir + yDir > 0 Then
        #demo.gb1, "Cyclesprite cm 1"
    Else
        #demo.gb1, "Cyclesprite cm 0"
    End If
    #demo.gb1, "Spritexy cm ";x;" ";y
    #demo.gb1, "Setfocus; Drawsprites"
    Wait

 
kevin
User IP Logged

KEVIN
tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1732
xx Re: arrow key up
« Reply #3 on: Jun 21st, 2017, 09:37am »

Quote:
in this small Game when you press the left or right key the sprote moves constantly so what I am trying to workout making the sprit sop when the arrow key is depressed


1. There is no "keyUp" event. but you might do something with GetAsyncKeyState
2. If you need iy stop then key is released - why not make it run ONLY then in pressed?
3. If you hold key it will autorepeat - om my machine every 30 or so ms. So you hold key - it runs. You release - it stops
4. BUT autorepeat kicks off after some delay. On my machine it's about 450 ms
So you hold key - it moves a bit - waits half a second - starts moving. Not so good.
( though then you release key, it stops. - Good)
5. GetAsyncKeyState has no autorepeat delay
so check it out
Smoother Animation with When characterInput

Bottom line.
You likely will do better with GetAsyncKeyState, but your event model / program structure better be changed.

EDIT
where am I find "Barel1.bmp",( 2, 3) ?
« Last Edit: Jun 21st, 2017, 09:41am by tsh73 » User IP Logged

damned Dog in the Manger
kevin
Senior Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 418
xx Re: arrow key up
« Reply #4 on: Jun 21st, 2017, 4:19pm »

Thanks again Mr tsh73 for the link

I have already worked out how I will implement (GetAsyncKeyState) but that will come in the morning
Thank you again

Kevin
User IP Logged

KEVIN
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls