Jump to content

How to force an ENTER after typing a value to a command


Recommended Posts

Posted

Hello,

 

I am writing an auto lisp script to draw leaders from an object to point to other items, (see part of the script below). I want to know if there is any way that after I press "Y" (Yes) to avoid the need to hit enter so the program ask me for the "Connect to:". I will appreciate your help. Thank you!! :D

 

(setq ldr2 "Y")

(while (= ldr2 "Y")

 

;-- Get the leader point & draw it

(setq pt1 (getpoint ip "\nConnect to: "))

(setq pt2 (polar ip (angle ip pt1) 0.1625)) ;--This is the pt on the BOM circle

(command "_line" pt2 pt1 "")

;-- Ask for another leader

 

(initget "Y")

(setq ldr2 (getkword "\nDo you need an extra leader? (Y/N): "))

Posted

Why not just use a default, so that the user only has to hit enter?

Posted

A default can be specified like this:

 

(or ldr2 (setq ldr2 "Yes"))
(initget "Yes No")
(setq temp (getkword (strcat "\nDo you need an extra leader? (Y/N) <" ldr2 ">: ")))
(or (not temp) (setq ldr2 temp)))

Posted

Check getkword its what you want

 

to quote lisp manual

(initget 1 "Yes No")

(setq x (getkword "choose Yes or No" ))

 

The case I think can give two answers Y v's y the capital I think in "Yes" makes it work if you add other words as lower case they may not work as you would have thought.

Posted

Some variations:

  (initget "Yes No")
 (setq return (cond ((getkword "\nQuestion: Yes/No/<Yes>:")) 
                    ("Yes")))

    (INITGET 0 "Yes No")
   (OR (SETQ return (GETKWORD "\nAre you sure? [Yes/No] < Yes >: "))
       (SETQ return "Yes")
   )

(DEFUN c:Yesno (/ return)
 (initget "Yes No")
 (cond ((getkword "\nQuestion: Yes/No/<Yes>:")) 
          ("Yes"))
)

Posted

I prefer the /= test for that scenario

 

(initget "Yes No")
(if (/= "No" (getkword "\nDo this?  (Yes/No)  <Y>:   "))
   (yayda_yada))

-David

Posted

Some good examples guys ^^

 

But they don't remember the last entered option... :P

Posted

Lee Mac, I used your code, but I still have to hit enter after I enter "N" if I decide not to have any more leaders. What I am looking for is to make the "Y" or the "N" an automatic value for the function so it takes it as a "response plus the enter" together without needing to press enter everytime I enter "Y" or "N".

Posted
Lee Mac, I used your code, but I still have to hit enter after I enter "N" if I decide not to have any more leaders. What I am looking for is to make the "Y" or the "N" an automatic value for the function so it takes it as a "response plus the enter" together without needing to press enter everytime I enter "Y" or "N".

 

I realise this, but I doubt that you can avoid hitting enter in this circumstance.

Posted

Why not just use the while statement like this:

 

(while (setq pt1 (getpoint ip "\nConnect to: "))
 (setq pt2 (polar ip (angle ip pt1) 0.1625))
 (command "_line" pt2 pt1 ""))

Posted

OK, here is my One Key Press routine like GetKword.

I made it somewhat generic.

;;  GetKeyPress.lsp
;;  CAB  version 1.0  03/26/09
;;  Get one key press from user similar to GetKword
;;  keys = list of key char & return value  '(("Y" "Yes")("N" "No"))
;;  def = result if ENTER is pressed  nil or "Yes" or "No" etc
;;        if nil then Enter is dissallowed
;;  msg = the prompt nil = "Press a key"
;;  emsg = the error message nil = "Incorrect keypress."
(defun GetKeyPress (keys def msg emsg / input result)
 (or msg (setq msg "Press a key"))
 (or emsg (setq emsg "Incorrect keypress."))
 (princ (strcat "\n" msg))
 (while (null result)
   (and (= (car (setq input (grread))) 2) ; keyboard entry
        (< 31 (setq input (cadr input)) 255) ; a key was pressed
   )
   (cond
     ((listp input)) ; not a keypress
     ((setq result (assoc (strcase (chr input)) keys))
      (setq result (cadr result))
     )
     ((and (= input 13) def)
      (setq result def)
     )
     ((princ (strcat "\n" emsg "\n" msg)))
   )
 )
 result
)

 

(defun c:test(/ ans)
 (setq ans (GetKeyPress
             '(("Y" "Yes")("N" "No"))  ; keys allowed, not case sensitive, & return value
             "Yes" ; default when ENTER is pressed
             "Do you need an extra leader? (Y/N) [Yes]:" ; message prompt
             nil ; use default error message
             ))
 (princ ans)
 (princ)
)

Posted

Thank you CAB!! So how do I call that function in my code? I got confused now :oops:

 

This is my complete code:

 

(defun c:bom ()

(command "-osnap" "")

(command "ortho" "Off")

 

;-- Get the parameter reqd to insert the BOM block

(setq ip (getpoint "\nInsertion Point: "))

(setq int1 (getint "\nItem Number: "))

(setq st1 (getstring "\nLeft Text: "))

(setq st2 (getstring "\nRight Text: "))

 

;-- Set layer to TEXT

(command "-layer" "set" "TEXT" "")

 

;--Insert the block with the given params

(command "_insert" "bom" ip "" "" "" int1 st1 st2)

(setq ldr2 "Y")

(while (= ldr2 "Y")

 

;-- Get the leader point & draw it

(setq pt1 (getpoint ip "\nConnect to: "))

(setq pt2 (polar ip (angle ip pt1) 0.1625)) ;--This is the pt on the BOM circle

(command "_line" pt2 pt1 "")

 

I THINK I HAVE TO CALL YOUR FUNCTION HERE BUT I AM NOT SURE NOW HOW TO CALL IT :oops:

;-- Ask for another leader (original code)

(initget "Y")

(setq ldr2 (getkword "\nDo you need an extra leader? (Y/N): "))

 

)

 

(command "-layer" "T" "0" "ON" "0" "set" "0" "")

(princ)

)

 

 

I will appreciate your help.

 

Thank you!

Posted

Try this:

(defun c:bom ()
 (command "-osnap" "")
 (command "ortho" "Off")

 ;;-- Get the parameter reqd to insert the BOM block
 (setq ip (getpoint "\nInsertion Point: "))
 (setq int1 (getint "\nItem Number: "))
 (setq st1 (getstring "\nLeft Text: "))
 (setq st2 (getstring "\nRight Text: "))

 ;;-- Set layer to TEXT
 (command "-layer" "set" "TEXT" "")

 ;;--Insert the block with the given params
 (command "_insert" "bom" ip "" "" "" int1 st1 st2)
 (setq ldr2 "Y")
 (while (= ldr2 "Y")

   ;;-- Get the leader point & draw it
   (setq pt1 (getpoint ip "\nConnect to: "))
   (setq pt2 (polar ip (angle ip pt1) 0.1625))
   ;;--This is the pt on the BOM circle
   (command "_line" pt2 pt1 "")

   ;;-- Ask for another leader (original code)
   (setq ldr2 (GetKeyPress
                '(("Y" "Yes") ("N" "No")); keys allowed, not case sensitive, & return value
                "Yes"                  ; default when ENTER is pressed
                "Do you need an extra leader? (Y/N) [Yes]:" ; message prompt
                nil                    ; use default error message
              )
   )
 )

 (command "-layer" "T" "0" "ON" "0" "set" "0" "")
 (princ)
)

;;  GetKeyPress.lsp
;;  CAB  version 1.0  03/26/09
;;  Get one key press from user similar to GetKword
;;  keys = list of key char & return value  '(("Y" "Yes")("N" "No"))
;;  def = result if ENTER is pressed  nil or "Yes" or "No" etc
;;        if nil then Enter is dissallowed
;;  msg = the prompt nil = "Press a key"
;;  emsg = the error message nil = "Incorrect keypress."
(defun GetKeyPress (keys def msg emsg / input result)
 (or msg (setq msg "Press a key"))
 (or emsg (setq emsg "Incorrect keypress."))
 (princ (strcat "\n" msg))
 (while (null result)
   (and (= (car (setq input (grread))) 2) ; keyboard entry
        (< 31 (setq input (cadr input)) 255) ; a key was pressed
   )
   (cond
     ((listp input))                   ; not a keypress
     ((setq result (assoc (strcase (chr input)) keys))
      (setq result (cadr result))
     )
     ((and (= input 13) def)
      (setq result def)
     )
     ((princ (strcat "\n" emsg "\n" msg)))
   )
 )
 result
)

Posted

Thank you CAB,

 

I loaded the code but I still can't get it to run whenever the function asks me for another leader it ends the function pressing "Y" or "N" or "enter". I can't find what am I doing wrong.

Posted

Try this Ham,

 

I have included an Error Handler and have updated the variable setting and included coding to accomodate for user errors.

 

(defun c:bom  (/ *error ovar vlst ip int1 st1 st2 ldr2 pt2)

 (defun *error*  (msg)
   (if    ovar
     (mapcar 'setvar vlst ovar))
   (princ (strcat "\nError: " (strcase msg)))
   (princ))

 (setq    vlst '("CMDECHO" "ORTHOMODE" "OSMODE" "ATTREQ" "CLAYER")
   ovar (mapcar 'getvar vlst))

 (mapcar 'setvar (cdr (reverse vlst)) '(1 0 0 0))
 (if (not (tblsearch "LAYER" "TEXT"))
   (command "-layer" "m" "TEXT" "")
   (setvar "CLAYER" "TEXT"))

 (if (findfile "bom.dwg")
   (progn
     (if (and (setq ip (getpoint "\nInsertion Point: "))
          (not (initget 5))
          (setq int1 (getint "\nItem Number: ")
            st1  (getstring "\nLeft Text: ")
            st2  (getstring "\nRight Text: ")))
   (progn
     (command "-insert" "bom" ip "" "" "" int1 st1 st2)
     (setq ldr2 "Yes")
     (while (and (= ldr2 "Yes")
             (setq pt1 (getpoint ip "\nConnect to: ")))
       (setq pt2 (polar ip (angle ip pt1) 0.1625))
       (command "_line" pt2 pt1 "")

       (setq ldr2 (GetKeyPress
            '(("Y" "Yes") ("N" "No")) ; keys allowed, not case sensitive, & return value
            "Yes" ; default when ENTER is pressed
            "Do you need an extra leader? (Y/N) [Yes]:" ; message prompt
            nil ; use default error message
            ))))
   (princ "\n<!> Block Information Not Correct <!>")))
   (princ "\n<!> Block Not Found <!>"))
 (mapcar 'setvar vlst ovar)
 (princ))

;;  GetKeyPress.lsp
;;  CAB  version 1.0  03/26/09
;;  Get one key press from user similar to GetKword
;;  keys = list of key char & return value  '(("Y" "Yes")("N" "No"))
;;  def = result if ENTER is pressed  nil or "Yes" or "No" etc
;;        if nil then Enter is dissallowed
;;  msg = the prompt nil = "Press a key"
;;  emsg = the error message nil = "Incorrect keypress."
(defun GetKeyPress  (keys def msg emsg / input result)
 (or msg (setq msg "Press a key"))
 (or emsg (setq emsg "Incorrect keypress."))
 (princ (strcat "\n" msg))
 (while (null result)
   (and (= (car (setq input (grread))) 2) ; keyboard entry
    (< 31 (setq input (cadr input)) 255) ; a key was pressed
    )
   (cond
     ((listp input)) ; not a keypress
     ((setq result (assoc (strcase (chr input)) keys))
      (setq result (cadr result))
      )
     ((and (= input 13) def)
      (setq result def)
      )
     ((princ (strcat "\n" emsg "\n" msg)))
     )
   )
 result
 )

Posted

Oops, I left out the code to prevent it from being case sensitive.

Revised version:

;;  GetKPress.lsp
;;  CAB  version 1.0  03/26/09
;;  Get one key press from user similar to GetKword
;;  keys = list of key character & return value not case sensitive
;;          '(("Y" "Yes")("N" "No"))  Note that the key character must
;;          be a string but the return value may be any data type
;;  def = result if ENTER is pressed  nil or "Yes" or "No" etc
;;        (any data type) if nil then Enter is dissallowed
;;  msg = the prompt nil = "Press a key"
;;  emsg = the error message nil = "Incorrect keypress."
(defun GetKPress (keys def msg emsg / input result klst)
 (or msg (setq msg "Press a key"))
 (or emsg (setq emsg "Incorrect keypress."))
 (mapcar '(lambda(x) (setq klst (cons(list(strcase (car x))(cadr x)) klst))) keys)
 (princ (strcat "\n" msg))
 (while (null result)
   (and (= (car (setq input (grread))) 2)    ; keyboard entry
        (< 31 (setq input (cadr input)) 255) ; a key was pressed
   )
   (cond
     ((listp input)) ; not a keypress
     ((setq result (assoc (strcase (chr input)) klst))
      (setq result (cadr result))
     )
     ((and (= input 13) def) (setq result def))
     ((princ (strcat "\n" emsg "\n" msg)))
   )
 )
 result
)

Posted

Wow!!!! You are amazing CAB!!! The code seems kind of complicated but I went through it and now I understand a lot more. Thank you!!!

 

Have a great day!!!

Posted

Thank you Lee Mac I appreciate your help!!! You guys are amazing!!!

Posted
Thank you Lee Mac I appreciate your help!!! You guys are amazing!!!

 

 

No probs :) - I do need to study the grread function in a lot more detail - I have only used it once or twice myself, but it is such a powerful command to invoke when you know how to use it properly ;)

 

Cheers guys,

 

Lee

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...