Detect keyboard on-the-fly

Just starting out? Need help? Post your questions and find answers here.
epog10
User
User
Posts: 90
Joined: Sat May 29, 2010 11:46 am
Location: UK

Detect keyboard on-the-fly

Post by epog10 »

I want to rotate three or four 'idle' pictures, each for about ten seconds, until interrupted by a keyboard press.

However whatever I try in terms of timing does not interrupt the sequence like it should. It may or may not work. it may require multiple key presses. All rather untidy.

Unfortunately the rather excellent keyboard.pb example that comes with purebasic does not include a timer. Is there an example of this?

Can't get to work whether with Delay(x) or a Timer, whether with Window or Screen functions.

Guess what I want is:-

Code: Select all

Back:Load picture or sprite
	  Display it
	  Start the timing
	  Is a key pressed? --------- Exit the loop
	  At timer end, update required picture
	  Goto Back
Regards,
epog10
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Detect keyboard on-the-fly

Post by Olliv »

Code: Select all

;*********************************************************************************************
LapLimit = ElapsedMilliseconds() + 5000
Repeat
 Delay(10)
 ExamineKeyboard()
 ClearScreen(Rgb(31, 63, 191) )
 FlipBuffers()
Until KeyboardPushed(#PB_Key_All) Or LapLimit < ElapsedMilliseconds()
epog10
User
User
Posts: 90
Joined: Sat May 29, 2010 11:46 am
Location: UK

Re: Detect keyboard on-the-fly

Post by epog10 »

Of course it makes sense to chop the delay up to under reaction time!

It ended up like this, with iDLE and aNS pre-set:-

Code: Select all

Procedure IDLE()
  Repeat
    Delay(10)
    ExamineKeyboard()
    LoadSprite(#Sprite_0, aNS)
    DisplaySprite(#Sprite_0, 0, 0)
    FlipBuffers()
    If L < ElapsedMilliseconds()
      iDLE = iDLE + 1
      If iDLE > 5 : iDLE = 1 : EndIf
      aNS = "C:\SIdle\Thepic" + Str(iDLE) + ".jpg"
      L = ElapsedMilliseconds() + 10000    
    EndIf
  Until KeyboardPushed(#PB_Key_All)
EndProcedure
Thank you very much for putting me right.

Regards,
epog10
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Detect keyboard on-the-fly

Post by Olliv »

You maybe could access to your hardrive less often than previously.

Code: Select all

Procedure IDLE()
LoadSprite(#Sprite_0, aNS)
Repeat
   Delay(10)
   ExamineKeyboard()
   DisplaySprite(#Sprite_0, 0, 0)
   FlipBuffers()
   If L < ElapsedMilliseconds()
      iDLE = iDLE + 1
      If iDLE > 5 : iDLE = 1 : EndIf
      aNS = "C:\SIdle\Thepic" + Str(iDLE) + ".jpg"
      LoadSprite(#Sprite_0, aNS)
      L = ElapsedMilliseconds() + 10000
   EndIf
Until KeyboardPushed(#PB_Key_All)
EndProcedure
epog10
User
User
Posts: 90
Joined: Sat May 29, 2010 11:46 am
Location: UK

Re: Detect keyboard on-the-fly

Post by epog10 »

Point taken in - many thanks.
Post Reply