GetKeywords Method (.NET)

The GetKeywords method prompts the user for input of a keyword at the Command prompt. The PromptKeywordOptions object allows you to control the input entered and how the prompt message appears. The Keywords property of the PromptKeywordOptions object allows you to define keywords that can be entered at the Command prompt.

Note: The underscore character ("_") has a special meaning and can't be used as a keyword or as part of a keyword.

Get a keyword from the user at the command line

The following example forces the user to enter a keyword by setting the property AllowNone to false, which disallows NULL input (pressing Enter). The Keywords property is used to add the valid keywords allowed.

C# Example

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("GetKeywordFromUser")]
public static void GetKeywordFromUser()
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;

    PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
    pKeyOpts.Message = "\nEnter an option ";
    pKeyOpts.Keywords.Add("Line");
    pKeyOpts.Keywords.Add("Circle");
    pKeyOpts.Keywords.Add("Arc");
    pKeyOpts.AllowNone = false;

    PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);

    Application.ShowAlertDialog("Entered keyword: " +
                                pKeyRes.StringResult);
}

A more user-friendly keyword prompt is one that provides a default value if the user presses Enter (NULL input). Notice the minor modifications to the following example.

C# Example

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("GetKeywordFromUser2")]
public static void GetKeywordFromUser2()
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;

    PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
    pKeyOpts.Message = "\nEnter an option ";
    pKeyOpts.Keywords.Add("Line");
    pKeyOpts.Keywords.Add("Circle");
    pKeyOpts.Keywords.Add("Arc");
    pKeyOpts.Keywords.Default = "Arc";
    pKeyOpts.AllowNone = true;

    PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);

    Application.ShowAlertDialog("Entered keyword: " +
                                pKeyRes.StringResult);
}