-- get_key_ usage example -- made by antibrain -- define list of all supported key names keys={"1","2","3","4","5","6","7","8","9","0", "q","w","e","r","t","y","u","i","o","p", "a","s","d","f","g","h","j","k","l", "z","x","c","v","b","n","m", "[","]","shift","space","backspace"} --define vars currentkeyname="" keystr="" keyoffset=0 kpressed=true ikd=false -- get_key usage: -- get_key_pressed(key) checks if key "key" is pressed and returns "true" for the frame that key "key" is pressed. -- get_key_state(key) returns 1 if key "key" is held. if key "key" isnt held, returns "nil" -- "q", "1", "space", "backspace" are all valid key names. -- "Q", "!", "^", "`" are not valid key names. -- lowercase letters, numbers, and control keys (space, alt, backspace, etc) are all valid key names -- uppercase letters, symbols, F keys and hotkeys are NOT valid key names. -- if you want to use symbols (eg. shift+4=$), you can check if shift is held, then change output chars while it is. -- an easy way to check for all keys is to make a table of all supported keynames, then iterate over it. -- main update loop (30fps) function _update() -- clear screen and reset drawing vars cls() kstrx=6 kstry=20 --for every key in keys, run key checking script for i=1,#keys do -- check if key "keys[i]" is held and set it to currentkey currentkey=get_key_state(keys[i]) -- if current key isnt held, set it to 0 so future calculations dont die if currentkey==nil then currentkey=0 end -- if current key is held, then set its name to currentkeyname for later if currentkey==1 then currentkeyname=keys[i] end -- if key string length in pixels is longer than screen length, move it back by 476 ish pixels if (kstrx-(keyoffset*5))+(#keystr*5)>480 then kstrx=6 kstry+=8 j=#keystr-keyoffset keyoffset=#keystr end if (#keystr<(keyoffset)) then keyoffset-=j end -- display all keys and change its color to red if key is held print(keys[i],6+(i*6),6,currentkey+7) -- draw line at next char in keystr to show where next char will go line(((#keystr+1)*5)-keyoffset*5,kstry+8,(((#keystr+1)*5)+4)-keyoffset*5,kstry+8,currentkey+7) -- draw key string at kstrx,kstry in hyperlink blue print(keystr,kstrx-(keyoffset*5),kstry,28) -- if a key hasnt been pressed this frame if ikd==false then -- set ikd to true if one is ikd=get_key_pressed(keys[i]) end -- if a key has been pressed this frame if ikd==true then -- set space to " " so that it doesnt just add the word "space" to keystr if currentkeyname=="space" then currentkeyname=" " end -- if backspace has been pressed, set keyname to "" so it doesnt print anything, then remove one char from end of keystr by setting keystr to substring of keystr from char 0 to char #keystr minus 1 if currentkeyname=="backspace" then currentkeyname="" keystr=sub(keystr,0,#keystr-1) end keystr=keystr..currentkeyname --reset key vars currentkeyname="" ikd=false end end end