Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Backspace Key
#10
Here it is the Typing Trainer 
Thanks to Marcus for the N7 code of input text with 'inkey()' 

Code:
'=============================================================
'
' Typing Trainer
'
' This program measures typing speed and backspace usage.
' The user must type the sentence 100% accurately;
' otherwise, it is marked as a failure.
'
' Control keys :
' - F5 to continue
' - Escape to quit the program
' - Backspace to erase any character before the cursor 
'
' Acknowledgement
' - input text with 'inkey' in N7 by Marcus
'   https://naalaa.com/forum/thread-227-post-1559.html#pid1559
'
'==============================================================

#win32
set window "Typing Trainer", 800,400,true
set redraw off
randomize time()

' color definition
black = [0,0,0]
white = [255,255,255]

' SoundFx Definition (duration, pitch, fadeOut, sampleRate)
visible type_sfx = CreateNoiseSfx(0.1, 10, 0.1, 14000)

' Pangrams
sentences = [
        "The quick brown fox jumps over the lazy dog.",               ' Classic pangram
        "Sphinx of black quartz, judge my vow.",                      ' Short pangram
        "Pack my box with five dozen liquor jugs.",                   ' Another classic pangram
        "How vexingly quick daft zebras jump!",                       ' Fun pangram
        "Bright vixens jump; dozy fowl quack.",                       ' Compact pangram
        "Jaded zombies acted quaintly but kept driving their oxen forward.", ' Longer pangram
        "Amazingly few discotheques provide jukeboxes.",              ' Modern pangram
        "Sixty zippers were quickly picked from the woven jute bag.", ' Complex pangram
        "Waltz, bad nymph, for quick jigs vex.",                      ' Short and snappy pangram
        "Glib jocks quiz nymph to vex dwarf.",                        ' Concise pangram
        "Quick zephyrs blow, vexing daft Jim.",                       ' Minimalist pangram
        "Brawny gods just flocked up to quiz and vex him.",           ' Extended pangram
        "Jackdaws love my big sphinx of quartz.",                     ' Classic pangram
        "The five boxing wizards jump quickly.",                     ' Short pangram
        "Heavy boxes perform quick waltzes and jigs.",               ' Medium pangram
        "A quick movement of the enemy will jeopardize five gunboats.", ' Complex pangram
        "Few quips galvanized the mock jury box.",                   ' Modern pangram
        "My faxed joke won a pager in the cable TV quiz show.",       ' Techie pangram
        "The jay, pig, fox, zebra, and my wolves quack!",             ' Animal-themed pangram
        "Blowzy night-frumps vex'd Jack Q.",                          ' Minimalist pangram
        "Jink cwm fog bravery, zap qux.",                             ' Compact pangram
        "Fickle jinx bog dwarves spy math quiz.",                     ' Fun pangram
        "Cozy sphinx waves quartz glyph job vex.",                    ' Short pangram
        "Vamp fox held quartz duck just by wing.",                    ' Creative pangram
        "Five quacking zephyrs jolt my wax bed.",                     ' Minimalist pangram
        "Quartz glyph job vex'd flummoxed backs.",                    ' Compact pangram
        "Crazy Fredrick bought many very exquisite opal jewels.",     ' Longer pangram
        "Six big devils from Japan quickly forgot how to waltz.",     ' Fun pangram
        "Grumpy wizards make toxic brew for the evil queen and jack.", ' Story-like pangram
        "The job requires extra pluck and zeal from every worker.",   ' Work-themed pangram
        "A wizard’s job is to vex chumps quickly in fog.",            ' Wizard-themed pangram
        "By Jove, my quick study of lexicography won a prize.",       ' Intellectual pangram
        "Exquisite farm waltz: djinn yobs pack quartz.",              ' Compact pangram
        "Zany vixens flock, braving quick mud paths.",                ' Adventure pangram
        "Jumped foxes, quick brown dogs, and lazy cats vie for fame.", ' Animal-themed pangram
        "Fix problem quickly with galvanized jets.",                 ' Engineering pangram
        "When zombies arrive, quickly fax Judge Pat.",                ' Emergency pangram
        "Big fjords vex quick waltz nymph.",                         ' Compact pangram
        "Jived fox nymph grabs quick waltz.",                        ' Short pangram
        "Glowing vixens zap quartz, jam by funk.",                   ' Funky pangram
        "The quick onyx goblin jumps over the lazy dwarf.",          ' Fantasy pangram
        "Five hexing wizard bots quietly jam.",                      ' Magical pangram
        "Zany quips vex bold frogs, chuckling mirth away.",          ' Frogs-themed pangram
        "Quick wafting zephyrs vex bold Jim.",                       ' Minimalist pangram
        "Prized waxy junk, vomit flecks, bad gnome.",                ' Junk-themed pangram
        "Quiz explained how jocks got fuzzy pants.",                 ' School-themed pangram
        "Brave kid juggles wax, phones, MQ.",                        ' Kid-themed pangram
        "Foxy diva brewed ginger ale, mixed vodka, sang jazz.",      ' Cocktail pangram
        "Jazz-loving vixens played quick waltz on saxophones.",      ' Music-themed pangram
        "Zesty hams quiz fox, vow to bake.",                         ' Food-themed pangram
        "Junk MTV quiz graced by fox whelps.",                       ' Media-themed pangram
        "Drab foxes quiz lynx, vow to jam.",                         ' Fox-themed pangram
        "Quizzical twins proved fox wrong.",                         ' Twins-themed pangram
        "Maxim quickly judged Byzantine glyphs.",                    ' Historical pangram
        "Faux kidney problems quickly vanished.",                    ' Medical pangram
        "Vexed funky DJs crammed box of liquor.",                    ' DJ-themed pangram
        "Jolly wizards fix broken quarts, make yummy pancakes.",     ' Cooking pangram
        "Zealous fox jumps, grabs quick waltz.",                     ' Dance-themed pangram
        "Brawny codfish squirm, jolt, vex lazy kelp.",               ' Sea-themed pangram
        "Vext cwm fly zing jabs Kurd qoph.",                         ' Minimalist pangram
        "Jinxed wizards quickly gave bogus prizes.",                 ' Trickster pangram
        "Fox nymphs grab quick waltzed jigs.",                       ' Dance-themed pangram
        "Zombies play quick waltz, vex frog king.",                  ' Zombie-themed pangram
        "Fuzzy waltz, bad nymphs, vex quick jig.",                   ' Dance-themed pangram
        "X-rayquiz: Vexed Goblins Flock Jack's Wry Hymn.",           ' X-ray-themed pangram
        "Jovial farm ducks quack, pigs oink, cows moo, zebras neigh.", ' Farm-themed pangram
        "Quick zephyrs vex bold farmers during winter.",             ' Weather-themed pangram
        "Hexapod robots quickly fixed jammed buzzer.",               ' Robotics pangram
        "Lazy bakers vow to fix jumbo quiches.",                     ' Bakery-themed pangram
        "Vexed jazz fans quickly improvise wild rhythms.",           ' Jazz-themed pangram
        "Quirky fox jams, vex bold glyphs, win chef's praise.",      ' Chef-themed pangram
        "Wizards fax quick jibes, growl, then vanish.",              ' Wizard-themed pangram
        "Zany foxes quickly jumped over the lazy dog.",              ' Classic variation
        "Vexed goblins quiz fjord nymphs, bark at wax.",             ' Goblin-themed pangram
        "Jazz-loving foxes quickly waltz, vexing dog.",              ' Jazz-themed pangram
        "Fuzzy jigs vex quick bad nymph, halt waltz.",               ' Dance-themed pangram
        "Xylophone wizards quickly jam bold fugues.",                ' Music-themed pangram
        "Kooky vixens jump, grab fizzy quarts, waltz.",              ' Kooky-themed pangram
        "Vexed gnomes quickly built jazzy fort.",                    ' Fort-themed pangram
        "Zany quips vex bold frogs, chuckling mirth away.",          ' Frogs-themed pangram
        "Jinxed wizards quickly gave bogus prizes.",                 ' Trickster pangram
        "Fox nymphs grab quick waltzed jigs.",                       ' Dance-themed pangram
        "Zombies play quick waltz, vex frog king."                  ' Zombie-themed pangram
]


'-----------
' Main Loop
'-----------
do

    ' Initialize values               
    randomSentence = ""
    userInput = ""
    startTime = 0
    endTime = 0
    timeTaken = 0
    backspaceCount = 0
   
    set color black
    cls
    set color white
    set caret 0,0
                     
    ' Randomly select a sentence from the list
    randomSentence = sentences[rnd(sizeof(sentences))]
   
    ' Display instructions and the sentence to type
    wln "------------------------------"
    wln "Welcome to the Typing Trainer!"
    wln "------------------------------"
    wln "Type the following sentence EXACTLY as shown:"
    wln
    type(randomSentence)
   
    ' Start the time
    startTime = time()
      
    ' input text with 'inkey'.
    ' allowed characters.
    charFilter = []
    for i = 32 to 126  charFilter[i] = true
    blinkTimer = 0
    while not keydown(KEY_RETURN)
        blinkTimer = (blinkTimer + 1)%60
        c = inkey()
        while c
            if key(charFilter, c)  then
                userInput = userInput + chr(c)
                play sound type_sfx
            elseif c = 8  then
                userInput = left(userInput, len(userInput) - 1)
                backspaceCount = backspaceCount + 1
            endif
            c = inkey()
        wend
       
        'clear
        set color black
        draw rect 0, 180, width(),100, true
       
        set color white
        set caret 0, 200
        wln "Type the sentence ! "
        write userInput
       
        if blinkTimer%60 < 30  write "_"
        if keydown(KEY_ESCAPE,true) then end
       
        redraw
        fwait 60
    wend
       
    ' Stop the time
    endTime = time()
   
    ' Calculate time taken in seconds
    timeTaken = (endTime - startTime)
   
    ' Check if the user input matches the random sentence exactly
    if userInput = randomSentence
        wln
        wln "Time Taken: " + timeTaken + " seconds"
        wln "Backspace Usage: " + backspaceCount + " times"
    else
        wln
        wln "Oops! Your input does not match the sentence exactly."
    endif
   
    ' Pause  
    wln
    wln "Press F5 to continue..."
    redraw
    do;wait 1;until keydown(KEY_F5,true)

loop


'-----------
' Functions
'-----------
function type(x)
    for t = 0 to len(x)
        write mid(x,t)
        play sound type_sfx
        wait 100
        redraw
    next
endfunc


function CreateNoiseSfx(duration, pitch, fadeOut, sampleRate)
    assert sampleRate >= 8000, "CreateBoomSfx: invalid sample rate"
    assert pitch > 0, "CreateBoomSfx: invalid pitch"
  
    ' Mix four different noise frequencies weighted, in a weird way, by the pitch value.
    freqs = [
            [v: 0, p: sampleRate/500, d: 0, t: 0, w: pitch],
            [v: 0, p: sampleRate/1000, d: 0, t: 0, w: pitch^2],
            [v: 0, p: sampleRate/2000, d: 0, t: 0, w: pitch^3],
            [v: 0, p: sampleRate/8000, d: 0, t: 0, w: pitch^4]]
   
    s = sizeof(freqs)
    data = []
    vol = 1
    fadeOut = fadeOut*duration*sampleRate
    fadeOutDelta = 1/(duration*sampleRate - fadeOut)
    for i = 0 to duration*sampleRate - 1
        v = 0
        w = 0
        for j = 0 to s - 1; f = freqs[j]
            f.t = f.t - 1
            if f.t <= 0
                f.t = f.p
                f.d = ((rnd()*2 - 1) - f.v)/f.p
            endif
            f.v = f.v + f.d
            v = v + f.v*f.w
            w = w + f.w
        next
        data[i] = vol*v/w
        if i > fadeOut  vol = vol - fadeOutDelta
    next
   
    return createsound(data, data, sampleRate)
endfunc
Reply


Messages In This Thread
Backspace Key - by 1micha.elok - 04-23-2025, 03:22 AM
RE: Backspace Key - by johnno56 - 04-23-2025, 06:32 AM
RE: Backspace Key - by Marcus - 04-23-2025, 07:56 PM
RE: Backspace Key - by Marcus - 04-24-2025, 01:37 PM
RE: Backspace Key - by 1micha.elok - 04-25-2025, 12:57 AM
RE: Backspace Key - by johnno56 - 04-25-2025, 08:58 AM
RE: Backspace Key - by 1micha.elok - 04-25-2025, 11:37 AM
RE: Backspace Key - by Marcus - 04-25-2025, 02:01 PM
RE: Backspace Key - by 1micha.elok - 04-25-2025, 10:53 PM
RE: Backspace Key - by 1micha.elok - 05-06-2025, 02:53 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)