AutoLISP is one of the most used programming languages for creating custom AutoCAD applications. Many custom commands that are created with AutoLISP, request input from the user. Input requests are commongly made with the GetXXX functions. While these functions are great for getting basic input such as points, numbers, strings, and keywords; they are also limited as well.
The GetInt function while it limits the return value to an integer value, it allows the user to enter any character they want. Once the user presses Enter or Space, the text entered is then validated to see if it is an integer. The GetString function allows you to enter a text string, but does not provide a way to mask the characters as they are entered.
This is where the Grread function comes in; an often over looked function by many AutoLISP programmers. This function can be helpful in creating your own custom Get behavior as it works with keyboard, mouse, and digitizer input. For example, you could create functionality that only allows numbers to be entered or masks the text being entered for a password like feature.
Syntax for the Grread function is:
(grread [cursor_tracking] [input_filters [cursor_type]])
For information on the arguments and values that the Grread function supports, see GRREAD (AutoLISP) in the AutoCAD online help.
The following example code, allows the user to enter a numeric value and masks the entered text as it is displayed at the Command prompt. While it does not show all the possible error handling that is needed, it does show how you can prompt the user for input and then determine if the value supplied is what is desired.
(defun c:MyPINCode ( / number code ch)
(setq number "")
(prompt "\nEnter a whole number [backspace to clear]: ")
(setq code (grread))
(while (and (= 2 (car code))
(and (/= 13 (cadr code))
(/= 32 (cadr code))))
(if (and (>= (cadr code) 48)
(<= (cadr code) 57))
(progn
(setq ch (chr (cadr code)))
(setq number (strcat number ch))
(princ "*")
)
)
;; Support backspace
(if (= (cadr code) 8)
(progn
(repeat (strlen number)
(princ (chr 8))
)
(setq number "")
)
)
;; Ask for more input if the user did not press Enter or Space
(if (or (/= 13 (cadr code))(/= 32 (cadr code)))
(setq code (grread))
)
)
(prompt (strcat "\nPIN entered was: " number))
(princ)
)
Once the code is loaded into AutoCAD, you will see the following prompt after the command MyPINCode is executed:
Enter a whole number [Backspace to clear]:
As you type values on the keyboard, only whole numbers 0 - 9 are accepted. Pressing keys such as 'A', 'c', or ';' are ignored by the program. When a valid number is entered, an asterick appears at the Command prompt in its place. Pressing Spacebar or Enter ends input and displays the entered value, while pressing Backspace clears all the numbers that the has user entered.
Example of input and return value at the Command prompt:
Enter a whole number [Backspace to clear]: *****
PIN entered was: 63537
Sincerely,
Lee
Comments