Lua keypress help

  • FM22
    24th Feb 2014 Member 0 Permalink

    I know there is a function in Lua for registering keypresses but I can't seem to find it anywhere on the forums or in the wiki. Can anyone please tell me it?

  • boxmein
    24th Feb 2014 Former Staff 1 Permalink
    I just made a thing for that!
    http://boxmein.x10.mx/tptelements/search.html
    nil tpt.register_keypress (function onKey(keychar, keycode, modifiers, eventobj))


    ...so basically
    function onkey(char, code, mod, evt)
    if char == 'f' then
    -- eat pudding
    -- then disable the key. pudding ran out, or something
    tpt.unregister_keypress(onkey)
    else
    -- don't eat pudding
    end
    end
    tpt.register_keypress(onkey)



    ... Speaking of which, does anyone want to chip in and help me write the descriptions/detail boxes for these? Right now they're all mostly generations and splices from the Lua reference. PM :o i've basically asked people to pm me in my latest three-four posts now?
    Edited 3 times by boxmein. Last: 24th Feb 2014
  • FM22
    25th Feb 2014 Member 0 Permalink

    It didn't work for me. When I put the following code into autorun.lua, it didn't work (it's supposed to run a file when you press m):

    function onkey(char, code, mod, evt)
    if char == 'm' then
    dofile('mod.lua')
    end
    end

    But when I did this, it said something about an unexpected character:

    nil tpt.register_keypress (function onKey)

    function onkey(char, code, mod, evt)
    if char == 'm' then
    dofile('mod.lua')
    end
    end

    What am I doing wrong?

    Edited once by FM22. Last: 25th Feb 2014
  • boxmein
    25th Feb 2014 Former Staff 0 Permalink
    @FM22 (View Post)
    The thing the search page shows in blue is a sort of short description of how the function looks.
    nil tpt.register_keypress (function onKey)

    means that the function returns a
    nil
    value or nothing interesting.
    Then, after that comes the function name
    tpt.register_keypress
    , and then which kind of arguments it wants from you.

    This time, it wants one argument, a
    function
    which itself takes the arguments
    (keychar, keycode, modifiers, eventobj)
    .

    You learn a lot from just that one line, and the format is very widespread in programming languages, so I picked that and adapted it for Lua.

    Now for how you can apply it.
    First off, remember, you need to pass the register_keypress function an actual function, which means you need to make a function to pass one! So let's make one.

    function onkey(char, code, mod, evt)
    if char == 'm' then
    dofile ('mod.lua')
    end
    end


    Now we have a function. Wee!
    After that we need to tell TPT to keep track of this function and call it with the right arguments every time a key is pressed.

    tpt.register_keypress(onkey)

    ..and that's about it! The entire code is inside the next textbox. Usually you'll only need one keypress handling function, because you can handle every possibility inside that one.

    function onkey(char, code, mod, evt)
    if char == 'm' then
    dofile ('mod.lua')
    end
    end
    tpt.register_keypress(onkey)
    Edited 2 times by boxmein. Last: 25th Feb 2014
  • FM22
    26th Feb 2014 Member 0 Permalink

    Thanks! It works perfectly now!