Console Input/Output

BambooBasic provides simple functions for console-based input and output. These functions are perfect for text-based programs, debugging, and learning.



UseConsole Directive

To use console I/O functions, add UseConsole at the top of your program:

UseConsole

Function Main()
    Print "Hello, World!"
    Return False
EndFunction

This directive ensures a console window is available for your program.

Important Notes:

1. Closing Console Window with [X]

When running a console application with UseConsole, if you close the window by clicking the [X] button on the title bar instead of letting the program exit normally, you may see an error message like:

ERROR: YourProgram.exe returned error code -1073741510

This is normal behavior and not a bug in your program. The error code -1073741510 is a standard Windows error code that indicates the process was terminated externally (by clicking [X]) rather than exiting cleanly with a return value.

To avoid this error, allow your program to exit normally by:

2. Using Input Commands Without UseConsole

If you try to use console input commands like Input or GetKey() without the UseConsole directive at the top of your program, the editor will hang and become unresponsive.

This happens because:

Solution: Always add UseConsole at the top of any program that uses Input or GetKey().

If your program hangs:



Print - Display Output

The Print statement outputs text to the console.

Syntax:

Print expression

Examples:

Function Main()
    ; Print string literals
    Print "Hello, World!"

    ; Print variables
    Local name:String = "Alice"
    Print name

    ; Print with concatenation
    Local age:Int = 25
    Print "Age: " & ToString(age)

    ; Print multiple lines
    Print "Line 1"
    Print "Line 2"
    Print "Line 3"

    ; Print empty line
    Print ""

    Return False
EndFunction

Output:

Hello, World!
Alice
Age: 25
Line 1
Line 2
Line 3

Notes:



Input - Get User Input

The Input statement reads user input from the console.

Syntax:

Input prompt, variable

Examples:

UseConsole

Function Main()
    ; String input
    Local name:String
    Input "Enter your name: ", name
    Print "Hello, " & name & "!"

    ; Integer input
    Local age:Int
    Input "Enter your age: ", age
    Print "You are " & ToString(age) & " years old"

    ; Double input
    Local height:Double
    Input "Enter your height in meters: ", height
    Print "Your height is " & ToString(height) & " meters"

    Return False
EndFunction

Example Session:

Enter your name: Alice
Hello, Alice!
Enter your age: 25
You are 25 years old
Enter your height in meters: 1.75
Your height is 1.75 meters

Notes:



GetKey() - Non-Blocking Input

The GetKey() function reads a single keypress without blocking program execution.

Syntax:

GetKey:Int()

Returns:

Example:

UseConsole

Function Main()
    Print "Press any key (ESC to exit)..."
    Print ""

    Local running:Int = 1

    While running = 1
        Local key:Int = GetKey()

        If key > 0 Then
            If key = 27 Then
                ; ESC key
                Print "ESC pressed. Exiting..."
                running = 0
            ElseIf key = 13 Then
                ; Enter key
                Print "[ENTER]"
            ElseIf key = 32 Then
                ; Space
                Print "[SPACE]"
            ElseIf key >= 32 And key <= 126 Then
                ; Printable character
                Local ch:String = Chr(key)
                Print "Key: '" & ch & "' (code: " & ToString(key) & ")"
            Else
                ; Other control character
                Print "Control code: " & ToString(key)
            EndIf
        EndIf
    Wend

    Return False
EndFunction

Common Key Codes:

Key Code Description
ESC 27 Escape key
Enter 13 Enter/Return key
Backspace 8 Backspace key
Tab 9 Tab key
Space 32 Space bar
A-Z 65-90 Uppercase letters
a-z 97-122 Lowercase letters
0-9 48-57 Number keys


Input vs GetKey()
Feature Input GetKey()
Blocking Yes (waits for Enter) No (returns immediately)
Input Type Full line of text Single key press
Return Value Stores in variable Returns key code
Use Case Forms, data entry Games, interactive programs


Complete Example: Simple Menu
UseConsole

Function ShowMenu()
    Print ""
    Print "=== Main Menu ==="
    Print "1. New Game"
    Print "2. Load Game"
    Print "3. Options"
    Print "4. Exit"
    Print ""
EndFunction

Function Main()
    Local running:Int = 1

    While running = 1
        ShowMenu()

        Local choice:String
        Input "Enter your choice (1-4): ", choice

        Local choiceNum:Int = ToInt(choice)

        If choiceNum = 1 Then
            Print "Starting new game..."
        ElseIf choiceNum = 2 Then
            Print "Loading game..."
        ElseIf choiceNum = 3 Then
            Print "Opening options..."
        ElseIf choiceNum = 4 Then
            Print "Exiting..."
            running = 0
        Else
            Print "Invalid choice! Please enter 1-4."
        EndIf
    Wend

    Return False
EndFunction


Complete Example: Number Guessing Game
UseConsole

Function Main()
    Print "=== Number Guessing Game ==="
    Print "I'm thinking of a number between 1 and 100"
    Print ""

    ; Secret number (in real game, would be random)
    Local secret:Int = 42
    Local guesses:Int = 0
    Local found:Int = 0

    While found = 0
        Local guessStr:String
        Input "Enter your guess: ", guessStr

        Local guess:Int = ToInt(guessStr)
        guesses = guesses + 1

        If guess = secret Then
            Print "Correct! You found it in " & ToString(guesses) & " guesses!"
            found = 1
        ElseIf guess < secret Then
            Print "Too low! Try again."
        Else
            Print "Too high! Try again."
        EndIf
    Wend

    Return False
EndFunction


Complete Example: Real-Time Key Detection
UseConsole

Function Main()
    Print "=== Arrow Key Demo ==="
    Print "Use arrow keys to move (ESC to exit)"
    Print ""

    Local x:Int = 5
    Local y:Int = 5
    Local running:Int = 1

    While running = 1
        Local key:Int = GetKey()

        If key <> 0 Then
            ; Arrow keys return negative codes
            If key = -72 Then
                ; Up arrow
                y = y - 1
                Print "UP    - Position: (" & ToString(x) & ", " & ToString(y) & ")"
            ElseIf key = -80 Then
                ; Down arrow
                y = y + 1
                Print "DOWN  - Position: (" & ToString(x) & ", " & ToString(y) & ")"
            ElseIf key = -75 Then
                ; Left arrow
                x = x - 1
                Print "LEFT  - Position: (" & ToString(x) & ", " & ToString(y) & ")"
            ElseIf key = -77 Then
                ; Right arrow
                x = x + 1
                Print "RIGHT - Position: (" & ToString(x) & ", " & ToString(y) & ")"
            ElseIf key = 27 Then
                ; ESC
                Print "Exiting..."
                running = 0
            EndIf
        EndIf
    Wend

    Return False
EndFunction


Input Validation
Function GetValidAge:Int()
    Local valid:Int = 0
    Local age:Int = 0

    While valid = 0
        Local ageStr:String
        Input "Enter your age (1-150): ", ageStr

        age = ToInt(ageStr)

        If age >= 1 And age <= 150 Then
            valid = 1
        Else
            Print "Invalid age! Please enter a number between 1 and 150."
        EndIf
    Wend

    Return age
EndFunction

Function Main()
    Print "Age Validator"
    Print ""

    Local age:Int = GetValidAge()
    Print "Valid age entered: " & ToString(age)

    Return False
EndFunction


Key Points

See Also

Examples

See the Input Examples for complete demonstrations.


BambooBasic © 2026 Michael Denathorn