|
Post by robjob on May 17, 2007 12:33:35 GMT -5
Ive been racking my brain trying to figure out how to poll a key release as opposed to a key.press. does anyone have any ideas how I might do th s?
|
|
|
Post by matthew on May 17, 2007 14:31:13 GMT -5
Tell me if this works...
dim upkeyreleased : upkeyreleased = 1 dim downkeyreleased : downkeyreleased = 1
If key.pressed(vk_up) = 0 then upkeyreleased = 1 if key.pressed(vk_up) and upkeyreleased = 1 then upkeyreleased = 0 ' put movement code here end if
If key.pressed(vk_down) = 0 then downkeyreleased = 1 if key.pressed(vk_down) and downkeyreleased = 1 then downkeyreleased = 0 ' put movement code here end if
|
|
|
Post by u9 on May 17, 2007 15:48:25 GMT -5
space_released = False If key.pressed( vk_space ) Then space_down = True ElseIf space_down And Not key.pressed( vk_space ) Then space_down = False space_released = True End If
This code could be at the top of your main loop and then you can just use the space_released variable to tell as it will be true exactly when the space button is released and be set to false in the next iteration of the main loop.
Here is an example
Option explicit
graphics.initialize key.initialize
Dim space_released, space_down, running space_released = False space_down = False running = True Do space_released = False If key.pressed( vk_space ) Then space_down = True ElseIf space_down And Not key.pressed( vk_space ) Then system.debugPrint 2 space_down = False space_released = True End If ' Look for the event If space_released Then running = False End If system.processMessages Loop While running = True
key.terminate graphics.terminate
Press space and release it again to terminate the program.
|
|
|
Post by u9 on May 18, 2007 8:32:01 GMT -5
I was bored today, so here's another example. Use escape to exit. Press space every now and then. I hope this is what you were looking for by the way Option explicit
graphics.initialize 1024, 768, False key.initialize
' Font and drawing canvas Dim font, surface font = graphics.createfont( "system", 10 ) surface = graphics.createimage( graphics.getscreenwidth/2, graphics.getscreenheight/2 ) ' Surface is only 1/4 size of window
' Buttons Dim space_released, space_down, escape_released, escape_down, running space_released = False space_down = False escape_released = False escape_down = False
' Main loop running = True Do ' Register key-releases space_released = False If key.pressed( vk_space ) Then space_down = True ElseIf space_down And Not key.pressed( vk_space ) Then space_down = False space_released = True End If escape_released = False If key.pressed( vk_escape ) Then escape_down = True ElseIf escape_down And Not key.pressed( vk_escape ) Then escape_down = False escape_released = True End If ' Set rendering to our temporary canvas graphics.renderimage surface graphics.setrect 0, 0, graphics.getwidth(surface), graphics.getheight(surface), argb( 10, 0, 0, 0 ) Dim i For i = 0 To 2 graphics.setline rnd*graphics.getwidth(surface), rnd*graphics.getheight(surface),rnd*graphics.getwidth(surface), rnd*graphics.getheight(surface), argb(155,rnd*255,rnd*255,rnd*255) Next ' Look for release events If space_released Then graphics.settext "Space released", rnd*(graphics.getwidth(surface)-100), rnd*(graphics.getheight(surface)-10), font, argb(255,255,255,255) End If If escape_released Then running = False End If ' Display graphics graphics.rendernormal graphics.clear graphics.setscalexy surface, 2, 2 graphics.setimage surface graphics.setscalexy surface, 1, 1 graphics.display system.processMessages Loop While running = True
key.terminate graphics.terminate
|
|
|
Post by matthew on May 18, 2007 9:41:13 GMT -5
^^ That's a good Programme.
|
|
|
Post by robjob on May 18, 2007 16:09:01 GMT -5
thanks guys, the examples were perfect. Happy coding
|
|