Thursday, 30 April 2026

Simulate Slow Typing with PowerShell: Type SQL Code Over Extended Periods

Introduction

Have you ever needed to simulate slow, character-by-character typing in Windows? Whether you're demonstrating code on a remote desktop, creating training videos, or just need to type slowly for testing purposes, this PowerShell script is perfect for the job.

This script uses the Windows SendKeys API to simulate keyboard input and can type either generated SQL comments or code from a file over a specified time period (default: 30 minutes).

Features

  • Type SQL code from a file or generate comment lines
  • Customizable typing duration (default: 30 minutes)
  • Automatic 10-second countdown before typing starts
  • Works with remote desktop sessions
  • Launches on-screen keyboard for visual reference
  • Real-time progress tracking
  • Handles special characters gracefully

Prerequisites

  • Windows PC with PowerShell
  • .NET Framework (included with Windows)
  • Target application (SSMS, Notepad, VS Code, etc.)

How It Works

  1. The script launches the Windows on-screen keyboard
  2. You have 10 seconds to click on your target window and position the cursor
  3. After the countdown, the script automatically starts typing
  4. Each character is sent with a 50ms delay
  5. Lines are separated by configurable delays to spread typing over the desired time period

Installation & Setup

Step 1: Create the Script File

  1. Open Notepad or your favorite text editor
  2. Copy the entire script code (see below)
  3. Save the file as SlowTypeScriptWithTargetWindow.ps1 in a folder (e.g., C:\Users\YourUsername\)

Step 2: Enable PowerShell Execution

Before running the script, you need to allow PowerShell to execute scripts. Open PowerShell as Administrator and run:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

Usage

Option 1: Generate 10,000 Comment Lines (Default)

Open PowerShell and navigate to the script folder, then run:

.\SlowTypeScriptWithTargetWindow.ps1

Option 2: Type SQL Code from a File

.\SlowTypeScriptWithTargetWindow.ps1 -FilePath "C:\path\to\your\file.sql"

Option 3: Customize Total Time

.\SlowTypeScriptWithTargetWindow.ps1 -TotalMinutes 60

Option 4: Combine Options

.\SlowTypeScriptWithTargetWindow.ps1 -FilePath "C:\path\to\your\file.sql" -TotalMinutes 45

Step-by-Step Execution Guide

  1. Open PowerShell in the directory where you saved the script
  2. Run the command with your desired parameters
  3. The script will display:
    • Configuration details (total lines, time, delay per line)
    • A 10-second countdown
  4. During the countdown:
    • Click on your target window (SSMS, Notepad, etc.)
    • Position your cursor where you want the text to appear
  5. After countdown completes:
    • Typing starts automatically
    • A progress bar shows real-time status
    • Do NOT click away from the target window
  6. When complete:
    • Summary statistics are displayed
    • Total lines typed and elapsed time

Troubleshooting

Issue: "Keyword delimiter is missing" or "Group delimiters are not balanced"

  • These are expected for certain special characters
  • The script handles them gracefully and continues typing
  • The text will still appear correctly in your target window

Issue: Script won't run

  • Ensure you've set the execution policy: Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
  • Run PowerShell as Administrator

Issue: Text not appearing in target window

  • Make sure the target window is in focus when typing starts
  • The on-screen keyboard should be visible (it's just for reference)
  • Check that you're using a compatible application (SSMS, Notepad, VS Code, etc.)

Complete Script Code

Copy the code below and save it as SlowTypeScriptWithTargetWindow.ps1


# PowerShell Script to type slowly using on-screen keyboard
# Can type SQL code from a file or generate comment lines
# User has 10 seconds to position cursor before typing starts

param(
    [string]$FilePath = "",
    [int]$TotalMinutes = 30,
    [int]$LineCount = 10000
)

# Add required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Calculate delay per line in milliseconds
$totalMilliseconds = $TotalMinutes * 60 * 1000
$delayPerLine = $totalMilliseconds / $LineCount

# Initialize code lines array
$codeLines = @()

# Check if file path is provided
if ($FilePath -and (Test-Path $FilePath)) {
    Write-Host "Reading SQL code from file: $FilePath" -ForegroundColor Cyan
    $codeLines = @(Get-Content -Path $FilePath)
    $LineCount = $codeLines.Count
    Write-Host "Loaded $LineCount lines from file" -ForegroundColor Green
}
else {
    # Generate SQL comment lines
    Write-Host "Generating $LineCount SQL comment lines..." -ForegroundColor Cyan
    for ($i = 1; $i -le $LineCount; $i++) {
        $codeLines += "-- This is a SQL comment line $i"
    }
    Write-Host "Generated $LineCount comment lines" -ForegroundColor Green
}

# Recalculate delay per line based on actual line count
$delayPerLine = $totalMilliseconds / $codeLines.Count

# Launch on-screen keyboard
Write-Host "Launching on-screen keyboard..." -ForegroundColor Cyan
Start-Process "osk.exe"
Start-Sleep -Seconds 2

# Show instructions
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "SLOW TYPE SCRIPT - READY TO START" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "Configuration:" -ForegroundColor Yellow
Write-Host "  Total lines to type: $($codeLines.Count)" -ForegroundColor Yellow
Write-Host "  Total time: $TotalMinutes minutes" -ForegroundColor Yellow
Write-Host "  Delay per line: $([math]::Round($delayPerLine / 1000, 2)) seconds" -ForegroundColor Yellow
if ($FilePath -and (Test-Path $FilePath)) {
    Write-Host "  Source: File ($FilePath)" -ForegroundColor Yellow
}
else {
    Write-Host "  Source: Generated SQL comments" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "Instructions:" -ForegroundColor Cyan
Write-Host "  1. Click on your target window (SSMS, Notepad, etc.)" -ForegroundColor Cyan
Write-Host "  2. Position your cursor where you want to type" -ForegroundColor Cyan
Write-Host "  3. Typing will start automatically in 10 seconds" -ForegroundColor Cyan
Write-Host ""
Write-Host "Typing will start in 10 seconds..." -ForegroundColor Green

# Wait 10 seconds before starting
for ($i = 10; $i -gt 0; $i--) {
    Write-Host "Starting in $i seconds..." -ForegroundColor Yellow
    Start-Sleep -Seconds 1
}

Write-Host ""
Write-Host "Starting typing now..." -ForegroundColor Green
Write-Host "Do NOT click away from the target window!" -ForegroundColor Yellow
Write-Host ""

# Type each line with delay
$lineNumber = 0
$startTime = Get-Date

foreach ($line in $codeLines) {
    $lineNumber++
    $elapsedSeconds = ((Get-Date) - $startTime).TotalSeconds
    $estimatedTotalSeconds = $TotalMinutes * 60
    $percentComplete = ($elapsedSeconds / $estimatedTotalSeconds) * 100
    
    Write-Progress -Activity "Typing Code" `
                   -Status "Line $lineNumber of $($codeLines.Count) | Elapsed: $([math]::Round($elapsedSeconds, 0))s / $estimatedTotalSeconds`s" `
                   -PercentComplete $percentComplete
    
    # Type the line character by character
    foreach ($char in $line.ToCharArray()) {
        try {
            # Escape special characters for SendKeys
            $charToSend = $char
            switch ($char) {
                '{' { $charToSend = '{{}}' }
                '}' { $charToSend = '{{}}' }
                '+' { $charToSend = '{+}' }
                '^' { $charToSend = '{^}' }
                '%' { $charToSend = '{%}' }
                '~' { $charToSend = '{~}' }
                '(' { $charToSend = '('; [System.Windows.Forms.SendKeys]::SendWait($charToSend); Start-Sleep -Milliseconds 50; continue }
                ')' { $charToSend = ')'; [System.Windows.Forms.SendKeys]::SendWait($charToSend); Start-Sleep -Milliseconds 50; continue }
                '[' { $charToSend = '['; [System.Windows.Forms.SendKeys]::SendWait($charToSend); Start-Sleep -Milliseconds 50; continue }
                ']' { $charToSend = ']'; [System.Windows.Forms.SendKeys]::SendWait($charToSend); Start-Sleep -Milliseconds 50; continue }
            }
            [System.Windows.Forms.SendKeys]::SendWait($charToSend)
        }
        catch {
            # Skip problematic characters and continue
        }
        Start-Sleep -Milliseconds 50
    }
    
    # Press Enter to go to next line
    [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
    
    # Wait before next line
    Start-Sleep -Milliseconds $delayPerLine
}

$totalElapsedSeconds = ((Get-Date) - $startTime).TotalSeconds

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "TYPING COMPLETED!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "Summary:" -ForegroundColor Yellow
Write-Host "  Total lines typed: $lineNumber" -ForegroundColor Yellow
Write-Host "  Total time elapsed: $([math]::Round($totalElapsedSeconds / 60, 2)) minutes" -ForegroundColor Yellow
Write-Host "  Expected time: $TotalMinutes minutes" -ForegroundColor Yellow
Write-Host ""

Conclusion

This PowerShell script is a powerful tool for simulating slow typing in Windows. Whether you're creating training content, demonstrating code on remote desktops, or testing applications, it provides a flexible and easy-to-use solution.

No comments:

Post a Comment