harilalmn Posted September 23, 2011 Posted September 23, 2011 Is there a way to set up a countdown timer for a user input? I am trying to create a routine for a CAD test in AutoCAD itself. Once the command is activated, it will ask 15 questions with options for answers, available under mouse rightclick menu. But I have to set up a maximum time of 30 seconds to pick an answer from the four choices. Code is here. How to set up a countdown timer? (defun c:CADTEST() (setq n 0) (initget 1 "zoomDynamic zoomExtents zoomCenter zoomPan") (setq Answer01 (getkword "Which one of the following commands allows simultaneous pan and zoom? [zoomDynamic/zoomExtents/zoomCenter/zoomPan]")) (if (eq Answer01 "zoomDynamic") (Progn (alert "You are right...") (setq n (1+ n)) ) (alert "Sorry, You are wrong...") ) (alert (strcat "You scored " (itoa n) "/1")) ) Quote
MSasu Posted September 23, 2011 Posted September 23, 2011 You can count the time by reading the DATE system variable and comparing it with a reference value (DATE read at the start of process). I’m not sure how can you link that with the input process – in the way to skip to next question if allowed time is exceeded. Maybe just set a timed-out answer (see below)? (setq RefTime (getvar “DATE”)) ;{ask user for input} (if (> (- (getvar “DATE”) RefTime) TimeLimit) (prompt ”Sorry, your answer exceeded allowed time!”)) Maybe there is a better approach, I will try to think to something else… Regards, Mircea Quote
harilalmn Posted September 23, 2011 Author Posted September 23, 2011 msasu, Thanks a lot... I will try...!! In the above example, I infact wanted to show the options with space. Say, I want to show "Zoom Dynamic" instead of "zoom_Dynamic" Is there a way to add sentences (words with space in between) as arguments for initget function? Quote
MSasu Posted September 23, 2011 Posted September 23, 2011 In the above example, I infact wanted to show the options with space. Say, I want to show "Zoom Dynamic" instead of "zoom_Dynamic" Is there a way to add sentences (words with space in between) as arguments for initget function? This is not possible since the space is reserved as keywords separators. You can use GETSTRING instead of GETKWORD, but this will require extra code to validate the answers. Regards, Mircea Quote
Lee Mac Posted September 23, 2011 Posted September 23, 2011 Since multi-threading is not possible with LISP you cannot monitor the time whilst simultaneously prompting the user, one of these processes must take focus. However, you can monitor the time after each question is issued: (defun c:test ( / _lst->str _askquestion answer answers intime question questions start time ) (setq time 10) ;; Seconds (defun _lst->str ( lst del / str ) (setq str (car lst)) (foreach x (cdr lst) (setq str (strcat str del x))) str ) (defun _askquestion ( init ques ) (initget (_lst->str init " ")) (getkword (strcat ques "[" (_lst->str init "/") "]: ")) ) (setq questions '( (("Male" "Female" "Other") "\nAre you ") (("Apple" "Banana" "Pear") "\nWhich is your favourite fruit? ") (("Red" "Blue" "Green" "Yellow") "\nWhich is your favourite colour? ") ) ) (setq time (* time 1000.)) (setq start (getvar 'MILLISECS)) (while (and (setq intime (< (- (getvar 'MILLISECS) start) time)) (setq question (car questions)) ) (princ (strcat "\nTime Remaining: " (rtos (/ (- time (- (getvar 'MILLISECS) start)) 1000.) 2 2) " seconds." ) ) (if (setq answer (apply '_askquestion question)) (setq answers (cons answer answers)) (setq answers (cons "No Answer" answers)) ) (setq questions (cdr questions)) ) (terpri) (if (not intime) (princ "\n==> Out of time! <==") (princ (_lst->str (reverse answers) "\n")) ) (princ) ) Bad example ^^ Quote
harilalmn Posted September 23, 2011 Author Posted September 23, 2011 Thanks for that code Lee...!!! I have something in hand to play with. Thanks a lot... Quote
Lee Mac Posted September 23, 2011 Posted September 23, 2011 Thanks for that code Lee...!!! It's not a very good example, but it gives you some idea of how to accomplish your goal Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.