About Monkey 2 › Forums › Monkey 2 Programming Help › Keyboard doesnt work in OnUpdate using Timer?
This topic contains 6 replies, has 3 voices, and was last updated by
therevills 1 year, 7 months ago.
-
AuthorPosts
-
September 16, 2017 at 6:54 am #10513
What I’m I doing wrong this time?
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142[crayon-5cba8cf148fa2929751925 inline="true" ]Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Const Size:=New Vec2i( 640,360 )Class MyWindow Extends WindowField timer:TimerMethod New()Super.New( "My Window",640,480,WindowFlags.Resizable )Layout="letterbox"timer = New Timer(60, OnUpdate)EndMethod OnUpdate()App.RequestRender()If Keyboard.KeyPressed(Key.Escape, False)App.Terminate()EndEndMethod OnRender( canvas:Canvas ) Overridecanvas.DrawText( "Hello World",Width/2,Height/2,.5,.5 )EndMethod OnMeasure:Vec2i() OverrideReturn SizeEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndSeptember 16, 2017 at 7:05 am #10514Monkey1234If Keyboard.KeyDown( Key.Escape )App.Terminate()EndSeptember 16, 2017 at 7:19 am #10515Thanks Adam, but really need a keyhit or keypressed solution.
September 16, 2017 at 8:12 am #10518Looks like KeyPressed is having problem inside a timer. Works OK inside OnRender so you’ll have to use that for now or KeyDown.
Think I know what’s up, will look into this ASAP. Did it used to work in a timer? What version?
September 16, 2017 at 8:26 am #10519Thanks Mark – Currently using MX2 v1.1.07 (from itch)… dont know if it used to work, your space chimps example has the same issue when firing the laser sound and using a timer.
September 16, 2017 at 11:14 am #10526Ok, to fix this I’ll need to add Keyboard.Flush(), ala ye olde FlushKeys() in blitz3d. A bummer, but I can’t see any way around it if we want to be able to read keypresses asynchronously on timers.
You will need to call Keyboard.Flush() to prevent any keys that have been recently pushed from being detected, eg: after loading screens etc.
The previous system would only report keypress that occured in the same ‘frame’, which worked well if everything is synchronous, but it doesn’t work well with timers which can go off in future frames.
In practice keyboard ‘polling’ is just reverting to how blitz3d and blitzmax used to do it.
September 16, 2017 at 11:33 pm #10562Looks like you’ve had fun trying to fix this!
-
AuthorPosts
You must be logged in to reply to this topic.