Author Topic: Wait for user input for a given time and proceed  (Read 1515 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 494
Wait for user input for a given time and proceed
« on: May 18, 2017, 01:55:49 AM »
Is it possible to wait for user to give input for few seconds and if no input is received, the program proceeds with default value.

Input can be taken using getint, getreal, getkword, getpoint functions etc.


Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Wait for user input for a given time and proceed
« Reply #1 on: May 18, 2017, 06:50:58 AM »
Input can be taken using getint, getreal, getkword, getpoint functions etc.

Not really, since when you are already prompted and doing "nothing", you can't close the user prompt just like that, and continue.
However its possible to set some flag to T/nil for a given time and then reverse it:
Code - Auto/Visual Lisp: [Select]
  1. ; (setq wait (seconds 10))
  2. ; (> wait (seconds 0)) -> T, then nil
  3. ; (< wait (seconds 0)) -> nil, then T
  4. (defun seconds ( delay / d s )
  5.   (setq d (rtos (getvar 'CDATE) 2 6))
  6.   (setq s
  7.     (+ delay
  8.       (apply '+
  9.         (list
  10.           (* 60 60 (read (substr d 10 2))) ; extract the hours
  11.           (* 60 (read (substr d 12 2))) ; extract the minutes
  12.           (read (substr d 14 2)) ; extract the seconds
  13.         )
  14.       )
  15.     )
  16.   )
  17. )
  18.  
  19. ; (Wait 5) ; will pause user for 5 seconds
  20. (defun Wait ( secs / wt now )
  21.   (setq wt (seconds secs)) (princ (strcat "\nPaused for " (vl-prin1-to-string secs) " seconds."))
  22.   (while (setq flg (> wt (setq now (seconds 0)))) )
  23.   (alert "\nPause ended.")
  24.   (princ)
  25. )
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: Wait for user input for a given time and proceed
« Reply #2 on: May 18, 2017, 10:38:47 AM »
Grrr1337, what if you start your code at 23:59:58 with 5 sec delay? ;)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Wait for user input for a given time and proceed
« Reply #3 on: May 18, 2017, 11:48:57 AM »
Grrr1337, what if you start your code at 23:59:58 with 5 sec delay? ;)

Yeah I thought about that, right after I posted.
Wrote this on the fly and was lazy to include day / month / years, heres source info if anyone intends to use that subfunction and wants to "fix" it.

Anyway what I tried to say was that once you prompt user with say getint you cannot interrupt the prompt only with the code, without the user doing anything.
But thats just my impression, I might be wrong.
:)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Wait for user input for a given time and proceed
« Reply #4 on: May 18, 2017, 01:26:21 PM »
You would have to roll your own getXXX functions, using grread to capture the user input whilst maintaining the timer, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun getint-timed ( msg msc / dif end gr1 gr2 rtn str )
  2.     (setq end (+ msc (getvar 'millisecs))
  3.           str ""
  4.     )
  5.     (while
  6.         (and (< 0 (setq dif (- end (getvar 'millisecs))))
  7.             (progn
  8.                 (princ (strcat "\r" msg " (" (rtos (/ dif 1000.0) 2 0) " seconds remaining): " str))
  9.                 (setq gr1 (grread t 14 1)
  10.                       gr2 (cadr gr1)
  11.                       gr1 (car  gr1)
  12.                 )
  13.                 (cond
  14.                     (   (= 025 gr1) nil)
  15.                     (   (= 002 gr1)
  16.                         (cond
  17.                             (   (< 031 gr2 127)
  18.                                 (setq str (strcat str (chr gr2)))
  19.                             )
  20.                             (   (= 013 gr2)
  21.                                 (if (/= 'int (type (setq rtn (read str))))
  22.                                     (progn
  23.                                         (setq str "" rtn nil)
  24.                                         (princ (strcat "\nRequires an integer value.\n"))
  25.                                     )
  26.                                 )
  27.                             )
  28.                             (   (and (= 008 gr2) (< 0 (strlen str)))
  29.                                 (setq str (substr str 1 (1- (strlen str))))
  30.                             )
  31.                             (   t   )
  32.                         )
  33.                     )
  34.                     (   t   )
  35.                 )
  36.             )
  37.         )
  38.     )
  39.     rtn
  40. )

Though, since AutoLISP runs in the GUI processor thread and doesn't permit multiple threads, one cannot update the timer unless the user supplies input of some form.