McBasic™Get McBasic™ ↗
Build 881
Hands-on tutorial · Input & events

Build a keyboard-friendly dialog

Make Return, Escape and Tab part of the design, not an afterthought.

2 Form / control designs2 complete BASIC filesGuided sample project

The default command is the action the user expects from Return. A cancel command is a separate escape route. Disabled actions must stay disabled even when activated from the keyboard. Check the tab order within each parent container, not just the visual position of the controls.

Step 1Prepare a safe practice project#

Extract the archive into a new writable folder, separate from your own application. Open KeyboardEntry.mcproj in McBasic™. Keep every declared source, layout, resource and migration file together.

To reconstruct it yourself, create a DesktopApp project and add the files declared below. In the complete project, follow these steps as a guided rebuild: replace each named source file once and Check after all its procedures are present.

Exact project configuration
KeyboardEntry.mcproj
{
  "schemaVersion": 1,
  "projectId": "c8cd8266-066f-4525-97d5-e3fac8535173",
  "name": "KeyboardEntry",
  "kind": "DesktopApp",
  "language": {
    "dialect": "McBasic1",
    "requireExplicit": true,
    "compare": "Binary",
    "defaultArrayBase": 0
  },
  "application": {
    "bundleIdentifier": "com.mcbasic.keyboardentry",
    "version": "0.1.0",
    "buildNumber": "1",
    "startupFormId": "form-main",
    "shutdownMode": "OnStartupFormClosed"
  },
  "forms": [
    {
      "id": "form-main",
      "typeName": "frmMain",
      "layout": "Forms/frmMain.form.json",
      "code": "Forms/frmMain.bas"
    },
    {
      "id": "5f8d8352-befb-4809-ad1f-906f8b106616",
      "typeName": "frmEntry",
      "layout": "Forms/frmEntry.form.json",
      "code": "Forms/frmEntry.bas"
    }
  ],
  "modules": [],
  "classes": [],
  "tests": [],
  "references": [],
  "resources": [],
  "targets": [
    "osx-arm64"
  ],
  "capabilities": []
}

Step 2Build the Form and set its properties#

frmMain

Select the Form first and set its properties. Add containers before their children. These are initial Designer values, not statements to paste into a procedure.

Form propertyValue
Caption"KeyboardEntry"
Height240
Width640
TypeNameParent
CommandButtoncmdOpenForm
LabellblLabel1Form
cmdOpen · exact Inspector values
PropertyValue
Caption"Open customer"
EnabledTrue
Height32
Left24
TabIndex0
Top32
VisibleTrue
Width180
Anchor edgesLeft, Top
lblLabel1 · exact Inspector values
PropertyValue
Caption"Enter saves a valid name. Escape closes the entry window."
EnabledTrue
Height56
Left24
TabIndex1
Top88
VisibleTrue
Width560
Anchor edgesLeft, Top

frmEntry

Select the Form first and set its properties. Add containers before their children. These are initial Designer values, not statements to paste into a procedure.

Form propertyValue
Caption"Keyboard customer"
Height360
Width640
TypeNameParent
TextBoxtxtNameForm
LabellblStatusForm
TextBoxtxtNotesForm
CommandButtoncmdSaveForm
CommandButtoncmdCancelForm
CheckBoxchkSaveEnabledForm
txtName · exact Inspector values
PropertyValue
EnabledTrue
Height32
Left24
MultiLineFalse
ReadOnlyFalse
TabIndex0
Text"New customer"
Top32
VisibleTrue
Width520
WordWrapFalse
Anchor edgesLeft, Top
lblStatus · exact Inspector values
PropertyValue
Caption"Enter a name, then press Return."
EnabledTrue
Height32
Left24
TabIndex1
Top80
VisibleTrue
Width560
Anchor edgesLeft, Top
txtNotes · exact Inspector values
PropertyValue
EnabledTrue
Height80
Left24
MultiLineTrue
ReadOnlyFalse
TabIndex1
Text""
Top128
VisibleTrue
Width520
WordWrapFalse
Anchor edgesLeft, Top
cmdSave · exact Inspector values
PropertyValue
Caption"Save customer"
EnabledTrue
Height32
Left24
TabIndex2
Top248
VisibleTrue
Width160
Anchor edgesLeft, Top
cmdCancel · exact Inspector values
PropertyValue
Caption"Cancel"
EnabledTrue
Height32
Left208
TabIndex3
Top248
VisibleTrue
Width160
Anchor edgesLeft, Top
chkSaveEnabled · exact Inspector values
PropertyValue
Caption"Enable Save shortcut"
CheckedTrue
EnabledTrue
Height32
Left24
TabIndex4
Top304
VisibleTrue
Width400
Anchor edgesLeft, Top

Step 3Connect the events explicitly#

frmMain

ObjectEventProcedureParameters
cmdOpenClickcmdOpen_ClickClick()

frmEntry

ObjectEventProcedureParameters
txtNameChangetxtName_ChangeChange()
cmdSaveClickcmdSave_ClickClick()
cmdCancelClickcmdCancel_ClickClick()
chkSaveEnabledChangechkSaveEnabled_ChangeChange()

Step 4Enter the complete behavior#

Open the named source file. Enter declarations first, then every procedure. Expand a procedure for its full body, or copy the complete file. Helpers are required: do not stop after entering the first Click handler.

Forms/frmMain.bas

Enter these declarations at file scope, before the procedures.

Forms/frmMain.bas · declarations
Option Explicit
Private Sub cmdOpen_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmMain.bas · cmdOpen_Click
Private Sub cmdOpen_Click()
    Dim entry As frmEntry
    Set entry = New frmEntry
    entry.Show
End Sub
Copy the complete Forms/frmMain.bas file

Replace the file once; do not append duplicate procedures.

Forms/frmMain.bas
Option Explicit

Private Sub cmdOpen_Click()
    Dim entry As frmEntry
    Set entry = New frmEntry
    entry.Show
End Sub

Forms/frmEntry.bas

Enter these declarations at file scope, before the procedures.

Forms/frmEntry.bas · declarations
Option Explicit
Private saveCount As Long
Private Sub cmdSave_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmEntry.bas · cmdSave_Click
Private Sub cmdSave_Click()
    saveCount = saveCount + 1
    Debug.Print "save:" & saveCount
    If Trim(txtName.Text) = "" Then
        lblStatus.Caption = "Enter a customer name."
        txtName.SetFocus
        txtName.SelectAll
        Exit Sub
    End If
    If txtName.Text = "New customer" Then
        lblStatus.Caption = "Replace the sample name."
        txtName.SetFocus
        txtName.SelectAll
        Exit Sub
    End If
    lblStatus.Caption = "Saved " & Trim(txtName.Text)
End Sub
Private Sub cmdCancel_Click()

Enter the complete action, including its input checks and calls to helper procedures.

Forms/frmEntry.bas · cmdCancel_Click
Private Sub cmdCancel_Click()
    Debug.Print "cancel"
    Unload Me
End Sub
Private Sub chkSaveEnabled_Change()

Read the current value or selection and update its dependent interface. The binding above determines which object calls this handler.

Forms/frmEntry.bas · chkSaveEnabled_Change
Private Sub chkSaveEnabled_Change()
    cmdSave.Enabled = chkSaveEnabled.Checked
End Sub
Private Sub txtName_Change()

Read the current value or selection and update its dependent interface. The binding above determines which object calls this handler.

Forms/frmEntry.bas · txtName_Change
Private Sub txtName_Change()
    lblStatus.Caption = "Ready to save."
End Sub
Copy the complete Forms/frmEntry.bas file

Replace the file once; do not append duplicate procedures.

Forms/frmEntry.bas
Option Explicit
Private saveCount As Long

Private Sub cmdSave_Click()
    saveCount = saveCount + 1
    Debug.Print "save:" & saveCount
    If Trim(txtName.Text) = "" Then
        lblStatus.Caption = "Enter a customer name."
        txtName.SetFocus
        txtName.SelectAll
        Exit Sub
    End If
    If txtName.Text = "New customer" Then
        lblStatus.Caption = "Replace the sample name."
        txtName.SetFocus
        txtName.SelectAll
        Exit Sub
    End If
    lblStatus.Caption = "Saved " & Trim(txtName.Text)
End Sub

Private Sub cmdCancel_Click()
    Debug.Print "cancel"
    Unload Me
End Sub

Private Sub chkSaveEnabled_Change()
    cmdSave.Enabled = chkSaveEnabled.Checked
End Sub

Private Sub txtName_Change()
    lblStatus.Caption = "Ready to save."
End Sub

Step 5Keep resources and storage reproducible#

No external resource or migration files are declared. Any inline migration, seed data or local JSON is present in the code above. Runtime application data and private preferences are not included in the archive.

Step 6Check, run and test the result#

Save all files and run Check. Resolve diagnostics before Run. Developer CLI users can work from the extracted folder:

Developer CLI
mcbasic check KeyboardEntry.mcproj
mcbasic run KeyboardEntry.mcproj

Acceptance checklist

  • Open the entry Form and navigate through fields using Tab.
  • Enter a name and press Return to exercise the default command.
  • Disable the save action, then press Return: it must not perform a disabled action.
  • Use Escape to exercise cancellation.

Step 7Make one change and repeat the tests#

Add a multiline note field and confirm Return can enter a new line there without unexpectedly submitting.

Save a working Git checkpoint first. Change one behavior, Check again and repeat the acceptance checklist. Compare with the downloaded solution if a regression appears.

Source baseline 20f4bfed6b · Build manifest