Backspace Key - 1micha.elok - 04-23-2025
Hi
I am working with some keydown event codes, but I can't seem to find the key code for the backspace key.
Could you kindly advise me? Thank you!
Code: #win32
set window "KEYDOWN",200,100,false,4
set redraw off
while not keydown(KEY_ESCAPE)
set color 0,0,0
cls
set color 255,255,255
set caret 10,10
if keydown(KEY_INSERT) wln "Insert"
if keydown(KEY_DELETE) wln "Delete"
if keydown(KEY_HOME) wln "Home"
if keydown(KEY_END, true) wln "End"
if keydown(KEY_PAGE_UP) wln "Page Up"
if keydown(KEY_PAGE_DOWN) wln "Page Down"
'if keydown(KEY_BACKSPACE) wln "Backspace"
redraw
fwait 10
wend
RE: Backspace Key - johnno56 - 04-23-2025
To tell you the truth, I have never needed to code a program that required backspace...
As a work-a-round... The ASCII value for backspace is 8. I suppose you could probably use:
if inkey() = 8 then wln "Backspace"
The "timing" of the key press and or release may not be the same as keydown but it should work... kind of...
RE: Backspace Key - Marcus - 04-23-2025
I've just not defined a named constant for it. You can use 8, as johnno suggested, with the regular keydown function:
Code: if keydown(8)
...
endif
RE: Backspace Key - Marcus - 04-24-2025
... But if you're planning on doing text input, the inkey function is the way to go - that's what ned/ngui uses.
RE: Backspace Key - 1micha.elok - 04-25-2025
Thanks, Johnno and Marcus, for the tips on handling the backspace key in Naalaa—whether it’s using `keydown(8)` or checking if `inkey() = 8`. I’m working on a text-based game, kind of like a fantasy computer or a typing tutor, where I need to catch the backspace key. The game’s not done yet, but I’m hoping to drop an alpha version in the next couple of weeks.
RE: Backspace Key - johnno56 - 04-25-2025
Well... there two things that you can do that are far better than I can do... 1. Imagine; plan and create a game... and 2. Set yourself a deadline and stick to it... Super impressed!! You must tell me your secret... shhhh... I will hardly tell anyone... Moo Ha Ha Ha....
RE: Backspace Key - 1micha.elok - 04-25-2025
(04-25-2025, 08:58 AM)johnno56 Wrote: Well... there two things that you can do that are far better than I can do... 1. Imagine; plan and create a game... and 2. Set yourself a deadline and stick to it... Super impressed!! You must tell me your secret... shhhh... I will hardly tell anyone... Moo Ha Ha Ha....
Honestly, I don’t have any juicy secrets—I just like keeping my brain in good shape. You know that old saying, “an apple a day keeps the doctor away”? Well, here’s my secret twist (oops, guess I *do* have a secret!): “Write a game in a month and keep the brain fog away!”
RE: Backspace Key - Marcus - 04-25-2025
Is the inkey example useful? I don't remember Here's an example that makes sense.
Code: set window "test", 640, 480
set redraw off
' input text with 'inkey'.
' allowed characters.
charFilter = []
for i = 32 to 126 charFilter[i] = true
inputString = ""
blinkTimer = 0
while not keydown(KEY_RETURN)
blinkTimer = (blinkTimer + 1)%60
c = inkey()
while c
if key(charFilter, c) inputString = inputString + chr(c)
elseif c = 8 inputString = left(inputString, len(inputString) - 1)
c = inkey()
wend
set color 0, 0, 0
cls
set color 255, 255, 255
set caret 0, 0
write "Enter something: " + inputString
if blinkTimer%60 < 30 write "_"
redraw
fwait 60
wend
' or just use 'rln'
wln
write "Enter something else: "
otherInputString = rln()
RE: Backspace Key - 1micha.elok - 04-25-2025
(04-25-2025, 02:01 PM)Marcus Wrote: ...
Code: ...
while not keydown(KEY_RETURN)
blinkTimer = (blinkTimer + 1)%60
c = inkey()
while c
if key(charFilter, c) inputString = inputString + chr(c)
elseif c = 8 inputString = left(inputString, len(inputString) - 1)
c = inkey()
wend
...
if blinkTimer%60 < 30 write "_"
...
wend
...
Thank you for the example on how to use `inkey()`. It’s been a huge help on my journey to building a typing tutor—or maybe something even bigger, like a fantasy computer written in Naalaa. I especially appreciate the part with the blinking cursor using `if blinkTimer%60 < 30 then write "_"`. That’s exactly what I needed to simulate an old-school computer cursor.
Next, I’m working on detecting how many times the user presses the backspace key, so I can accurately track their typing. The goal is to count how correctly they type a given sentence and then provide feedback at the end of the session.
RE: Backspace Key - 1micha.elok - 05-06-2025
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
|