'===========================================================================
' Subject: TRAP KEYBOARD INACTIVITY           Date: 08-19-96 (19:53)       
'  Author: Egbert Zijlema                     Code: PB                     
'  Origin: E.Zijlema@uni4nn.iaf.nl          Packet: DATETIME.ABC
'===========================================================================
' NOKEY.BAS  - how to trap keyboard inactivity
' Author     : Egbert Zijlema (E.Zijlema@uni4nn.iaf.nl)
' (up)Date   : August 19, 1996
' Language   : Power Basic 3.2
' Copyright  : Public Domain

' This routine does not demonstrate a sophisticated screen saver.
' Its main purpose is to show the most simple method to trap keyboard
' inactivity for a certain period of time.
' As a sample screen saver it turns the screen black, just to proof
' that it really works.

' Most programmers use the TIMER FUNCTION to calculate the number of
' seconds before screen saver launch. This works. I used it myself
' until an hour ago. There is 1 small problem however: as soon as
' the computer's clock passes midnight, TIMER is (re)set to zero
' which will cause an infinite loop - unless you correct it by
' adding 86400 seconds every round, e.g.:

' start# = TIMER
' DO
'    now# = TIMER
'    IF now# < start# THEN INCR now#, 86400  [ adjust for midnight]
'    IF now# - start# =>  .... THEN
'      (command to start screen saver)
'    END IF
'
'    (code for keyboard trapping)
' LOOP UNTIL ........

' ---------------------------- begin code ---------------------------

DEFINT A - Z
FUNCTION GetKey AS INTEGER
  STATIC t$                     ' alias for TIME$
  DO

    IF seconds = 30 THEN        ' half a minute for this demo
      CALL BlackScreen          ' start screen saver
      EXIT FUNCTION
    END IF

    IF t$ <> TIME$ THEN         ' TIME$ changes every second
      t$ = TIME$
      LOCATE 1, 72 : PRINT t$   ' you may leave this out
      INCR seconds              ' add 1
    END IF

    KeyIn$ = INKEY$
  LOOP UNTIL LEN(KeyIn$)        ' until keypress

  FUNCTION = CVI( KeyIn$ + CHR$(0) )
END FUNCTION

SUB BlackScreen
  DEF SEG = &HB800              ' color card - use &HB000 for monochrome
  OldScreen$ = PEEK$(0, 4000)
  COLOR 7, 0
  LOCATE , , 0                  ' hide cursor
  CLS
  DO
  LOOP UNTIL LEN(INKEY$)
  POKE$ 0, OldScreen$
  DEF SEG
END SUB

SUB MainMenu
  DO
    KeyIn = GetKey
    SELECT CASE KeyIn
      CASE 27
        CLS
        SYSTEM
      CASE ELSE
        ' other keys not supported here
    END SELECT
  LOOP
END SUB

' main

CLS
  COLOR 15, 0
  LOCATE 2, 4
  PRINT "NOKEY.BAS       - traps keyboard inactivity"
  LOCATE 3, 4
  PRINT "Author          : Egbert Zijlema"
  LOCATE 4, 4
  PRINT "Copyright status: Public Domain"
  LOCATE 10, 4
  PRINT "This screen will turn black after 30 seconds"
  LOCATE 11, 4
  PRINT "Press any key to restore it"
  COLOR 7
  LOCATE 14, 4
  PRINT "(or press Esc to finish this demo)"

  MainMenu
END
