KeithXP 发表于 2022-7-6 09:16:57

Initget/Getkword: press any k

Title says it all really. I am trying to allow the user to easily/quickly choose whether to do something by pressing any key (or perhaps the spacebar), or to bypass the option with 'Enter'.
 
I cannot figure out how to do this with getkword or getstring, but I am fairly novice at autolisp and would be grateful for some pointers.
 
thanks

Lee Mac 发表于 2022-7-6 09:34:40

Since a 'Space' is considered 'Enter' I think the closest you will get is:
 

(initget "A B")(if (= "A" (getkword "\nChoose <B>: ")) ... Do A ... ... Do B ...)
 
Perhaps have a read of my tutorial on the topic.
 
I suppose you could use a workaround with getstring, but its not the best practice:
 

(if (= "" (getstring t "\nPress Any Key for A, Enter for B: ")) ... Do B ... ... Do A ...)

Tharwat 发表于 2022-7-6 09:50:16

Check this out buddy .
 
Option A

(if (not   (eq (setq ansA (getstring "\n Press any key to go with A :"))         ""   )   ) (alert " Now you are going with option A ") (alert " It is not going with A"))Option B
 

(if (eq (setq ansB (getstring "\n Press Enter to go with B :"))       ""   ) (alert " Now you are going with option B ") (alert " It is not going with B"))Tharwat

KeithXP 发表于 2022-7-6 10:02:31

Had not seen Tharwat's post when I chose this solution:
 

(initget "Set Not") (setq ans (cond ( (getkword "\nSet planting area in name tag? : ")) ("Set"))) (if (= ans "Set")   (progn   (command "-vbarun" "SetPlantingArea")(princ)   ))
 
The 'setplantingarea' vba routine does not expect any arguments. The line
(command "-vbarun" "setplantingarea") runs in isolation without returning any error. However, when run as part of the code snippet I get:
 
 
Set planting area in tag? : s
 
Set planting area in tag? :
 
*Invalid selection*
Expects a point or Last
 
*Invalid selection*
Expects a point or Last
Function cancelledSelect area object:
Command: Select destination tag:
 
I have no idea what is generating the 'invalid selection' error! The SetPlantingArea routine works perfectly despite the error on the command line.

BIGAL 发表于 2022-7-6 10:11:22

Why not nil = A no matter what else make it "B"
 
 
(setq ans (getstring))
(if (= ans nil)(setq ans "A")(setq ans "B"))

David Bethel 发表于 2022-7-6 10:18:16

I use a this scenario quite a lot:

(initget "Yes No")(if (/= "No" (getkword "\nDo It - (Yes/No) :   "))   (do_it))
 
 
This way a "Yes" or a nil ( enter ) both return T-David
页: [1]
查看完整版本: Initget/Getkword: press any k