Log In  


Hey everyone!

I've been messing around with picotron, and some people keep saying (including myself) that they hope for more buttons to work with or more input methods, but i've discovered something interesting that anyone can use! (until api changes)

I've made a keyboard tester that is fully commented out, and explains how it works and how get_key works and how to use it. to run the cart, click "show" below and copypaste code into picotron, then run it.

-- get_key_<state/pressed> 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
        if sub(tostr(flr(time())/2),-1)=="5" then
        line(((#keystr+1)*5)-keyoffset*5,kstry+8,(((#keystr+1)*5)+4)-keyoffset*5,kstry+8,currentkey+7)
        end
        -- 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

I'll explain some things about get_key here.

There are two forms of get_key. get_key_pressed and get_key_state. they are very different and are usefull in their own ways.

get_key_pressed(key) returns true or false, depending on if key key is pressed. it returns true for one frame when key key is pressed.

get_key_state(key) returns nil or 1, depending on if key key is HELD. holding the key will make it return 1, and if key is not held, it returns NIL.

as for what to put in key, there are rules on what a valid keyname is.

"a","space" and "ctrl" are all valid keynames.
"A","`" and "!" are NOT valid keynames.

Valid keynames include: lowercase letters; numbers; control keys (space,ctrl,backspace,shift,etc)
INVALID keynames include: uppercase letters; symbols; upper key chars; anything you have to press shift for; F keys; programmable hotkeys.

Interestingly enough, "[" and "]" are also valid keynames, even though no other symbols seem to work.

If you want to use uppercase letters/symbols, you actually arent out of luck! you can write a script to check if shift is held using get_key_state, then change what keys do what.

I hope this guide/tutorial/cart was useful to you all! just remember that the API still might change before 0.1, so dont expect this to work later down the line. ill try to update this if it stops working, but dont count on it.

2



[Please log in to post a comment]