home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / homonlib.zip / GETKEY.BAS < prev    next >
BASIC Source File  |  1995-04-13  |  5KB  |  127 lines

  1. DEFINT A-Z
  2.  
  3. ' $INCLUDE: 'PARM.INC'
  4. ' $INCLUDE: 'SETCURS.INC'
  5. ' $INCLUDE: 'TRUEFALS.INC'
  6.  
  7. DECLARE FUNCTION GetKey$ (parm())
  8. DECLARE FUNCTION LastKey$ (k$)
  9.  
  10. 'External functions:
  11.  
  12. DECLARE SUB RestScreen (f$)
  13. DECLARE SUB SaveScreen (f$)
  14. DECLARE SUB SetView (top, bot, parm())
  15. DECLARE FUNCTION TempName$ (p$)
  16. DECLARE FUNCTION VPage (p)
  17.  
  18. FUNCTION GetKey$ (parm()) STATIC
  19. '****************************************************************************
  20. 'Used to control user input.  It includes a screensaver routine and a way to
  21. ' trap hotkeys with polling rather than ON KEY.  Use it where you would
  22. ' normally place an INKEY$ loop.
  23. '
  24. 'Chances are, you will want to modify this function for each program that you
  25. ' write.  See below for information about how the screensaver works and how
  26. ' to add hotkey procedures.
  27. '
  28. '****************************************************************************
  29.  
  30. STATIC busy
  31.  
  32. idle! = TIMER
  33.  
  34. DO
  35.  
  36.      'If it has been 5 minutes since a keypress, do the screensaver routine:
  37.  
  38.      IF TIMER > (idle! + 300) THEN
  39.           oldcursor = SetCursor(SCNONE)      'Turn the cursor off.
  40.           savepage = VPage(0)                'Allocate a video page for a
  41.           IF savepage = 0 THEN               'screen save.  If none are
  42.                savefile$ = TempName$("")     'available, use the slower
  43.                SaveScreen savefile$          'save to file method.
  44.           ELSE
  45.                PCOPY 0, savepage
  46.           END IF
  47.           VIEW PRINT                         'Clear any active viewport.
  48.           COLOR 7, 0
  49.           DO                                 'Show the time on the screen in
  50.                CLS                           'random colors and locations
  51.                row = INT(RND * 24) + 1       'until a key is pressed.
  52.                col = INT(RND * 72) + 1
  53.                c = INT(RND * 7) + 9
  54.                LOCATE row, col
  55.                COLOR c
  56.                PRINT TIME$;
  57.                SLEEP (2)
  58.           LOOP WHILE INKEY$ = ""
  59.           IF savepage = 0 THEN               'Restore the screen according to
  60.                RestScreen savefile$          'how we saved it.
  61.                KILL savefile$
  62.           ELSE
  63.                PCOPY savepage, 0
  64.           END IF
  65.           SetView -1, -1, parm()             'Restore the previous viewport.
  66.           c = SetCursor(oldcursor)           'Restore the previous cursor.
  67.           COLOR parm(FGN), parm(BGN)         'Set colors back to normal.
  68.           idle! = TIMER                      'Reset the idle timer.
  69.      END IF
  70.  
  71.      'Otherwise check the keyboard for activity:
  72.  
  73.      x$ = INKEY$
  74.  
  75.      'If a key was pressed, see if it was a hotkey before returning it:
  76.  
  77.      IF LEN(x$) AND NOT busy THEN            'Check for hotkeys
  78.           SELECT CASE x$
  79.  
  80.                'Put your hotkey procedures here.  If one is called, be sure
  81.                'to set busy to TRUE.  This prevents hotkey procedures from
  82.                'being called from within other hotkey procedures.  Don't
  83.                'forget to reset busy to FALSE when finished.
  84.                '
  85.                'Example:  SELECT CASE x$
  86.                '              CASE CHR$(0) + CHR$(68)       'F10
  87.                '                   busy = TRUE
  88.                '                   MyHotKeySub  (Whatever your procedure is)
  89.                '                   busy = FALSE
  90.                '              CASE...
  91.  
  92.                CASE ELSE
  93.                     'Not a hotkey.
  94.           END SELECT
  95.      END IF
  96.  
  97. LOOP UNTIL LEN(x$)
  98.  
  99. GetKey$ = x$                            'Return the value of the keypress.
  100. x$ = LastKey$(x$)                       'Update LastKey$() with this value.
  101.  
  102. END FUNCTION
  103.  
  104. FUNCTION LastKey$ (k$) STATIC
  105. '****************************************************************************
  106. 'When used in conjunction with GetKey$(), LastKey$() will return the value
  107. ' returned by the previous call to GetKey$().  One possible use would be to
  108. ' find out the key used to exit a previously called function (assuming that
  109. ' function used GetKey$() for all its input).
  110. '
  111. 'To use the function, merely pass a null string as the argument.  This will
  112. ' return the stored value without changing it.  Should you wish to update
  113. ' LastKey$() with a value other than the one placed there by the last call to
  114. ' GetKey$(), just pass it a non-null string.
  115. '****************************************************************************
  116.  
  117. STATIC last$
  118.  
  119. LastKey$ = last$                   'Retain previous value for return.
  120.  
  121. IF LEN(k$) THEN                    'If anything other than a null string is
  122.      last$ = k$                    'passed, the value of LastKey$() will be
  123. END IF                             'updated.
  124.  
  125. END FUNCTION
  126.  
  127.