05-03-26

Typing Analytics Logger Script

This Lua script for hardware input tracks your typing speed, frequency, and patterns. It logs key presses, calculates WPM, and saves data to JSON files for personal analysis.

rebindlua scriptkey loggertyping analyticsWPMhardware inputscript

Question

Key logger for personal typing analytics

Rebind AI

luau
-- rebind: name=Typing Analytics Logger
-- rebind: description=Track personal typing speed, frequency, and patterns

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable Logging" }),
    auto_save = UI.Toggle(true, { label = "Auto-save Every 100 Keys" }),
})

local stats = {
    total_keys = 0,
    total_time = 0,
    session_start = System.Time(),
    key_counts = {},
    wpm_samples = {},
    last_key_time = System.Time(),
    current_word_chars = 0,
    current_word_start = System.Time(),
}

local function saveStats()
    local filename = "typing_session_" .. os.date("%Y%m%d_%H%M%S") .. ".json"
    local data = {
        total_keys = stats.total_keys,
        total_time_ms = stats.total_time,
        session_duration = System.Time() - stats.session_start,
        key_frequency = stats.key_counts,
        average_wpm = #stats.wpm_samples > 0 and (stats.total_keys / 5) / (#stats.wpm_samples / 60) or 0,
        most_pressed = (function()
            local max_key, max_count = nil, 0
            for key, count in pairs(stats.key_counts) do
                if count > max_count then max_key, max_count = key, count end
            end
            return max_key
        end)(),
        timestamp = os.date("%Y-%m-%d %H:%M:%S"),
    }
    File.WriteJSON(filename, data)
    UI.Notify("Session saved: " .. filename, "success")
    Log.Info("Typing analytics saved")
end

function OnDown(key)
    if not cfg.enabled then return true end

    -- track only printable characters, not modifiers or system keys
    if #key == 1 or key == "Space" or key == "Enter" then
        stats.total_keys = stats.total_keys + 1
        stats.key_counts[key] = (stats.key_counts[key] or 0) + 1

        local now = System.Time()
        local time_since_last = now - stats.last_key_time
        stats.last_key_time = now

        -- WPM calculation: assume word break on space/enter (5 chars = 1 word)
        if key == "Space" or key == "Enter" then
            if stats.current_word_chars > 0 then
                local word_time = now - stats.current_word_start
                table.insert(stats.wpm_samples, word_time)
            end
            stats.current_word_chars = 0
            stats.current_word_start = now
        else
            stats.current_word_chars = stats.current_word_chars + 1
        end

        stats.total_time = stats.total_time + time_since_last

        -- auto-save every 100 keys
        if cfg.auto_save and stats.total_keys % 100 == 0 then
            After(500, function()
                Run(function()
                    saveStats()
                end)
            end)
        end
    end

    return true
end

function OnStop()
    if stats.total_keys > 0 then
        saveStats()
    end
end

function OnBlur()
    if stats.total_keys > 0 and cfg.auto_save then
        Run(function()
            saveStats()
        end)
    end
    stats.session_start = System.Time()
    stats.total_keys = 0
    stats.total_time = 0
    stats.key_counts = {}
    stats.wpm_samples = {}
end

How it works

This script logs your personal typing analytics:

  • Key tracking: Counts how many times each key is pressed during your session
  • Session timing: Records total typing time and calculates session duration
  • WPM estimation: Estimates words-per-minute based on character count and timing (5 characters = 1 word)
  • Most used key: Identifies your most frequently pressed key
  • Auto-save: Optionally saves stats every 100 keys typed
  • Session files: Creates timestamped JSON files with complete statistics when you stop typing or close the script

The logger only counts printable characters (letters, numbers, space, enter), not modifiers or system keys. Data is saved locally in your script directory as JSON files you can analyze or import into spreadsheets.

Learn more