Carlos Lombardii
Carlos Lombardii

Reputation: 253

Is there a "Get Keyboard Input" function in Lua? (Arrow keys)

I was wondering if you could receive keyboard input just like Java, C+, etc. Or if there isn't is there external libraries?

Here's some info on the 2D, Maze-Game: You use the arrow keys to navigate through the maze. You use the UP and DOWN arrow keys to select the objects in the menu, for example:

--> New Game

Load Game

Options

And so on... I appreciate any help. Good day.

Upvotes: 3

Views: 16849

Answers (3)

Foxie Flakey
Foxie Flakey

Reputation: 420

I have answer if you use linux this code will work. just try my code on other platform see if it work(if you curious). It doesn't echo the arrow key and doesn't wait until you press enter.

Modify some my code if this cause some problem.

This might be very useful for your 2D maze project

    function getch_unix() --This return key pressed instantly without waiting until user press enter key
      os.execute("stty -echo raw")
      os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
      local key = io.read(3) --must read three letter for getArrowKey to work
      os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
      return(key);
    end
    
    function getArrowKey()
      function split(str)
        local tmp = {}
        for let in string.gmatch(str, ".") do
          table.insert(tmp, let)
        end
        return tmp
      end
      --[[
      A is up arrow
      B is down arrow
      D is left arrow
      C is right arrow
      ]]
      local key = split(getch_unix())
      if string.lower(key[3]) == "a" then
        return "up"
      elseif string.lower(key[3]) == "b" then
        return "down"
      elseif string.lower(key[3]) == "d" then
        return "left"
      elseif string.lower(key[3]) == "c" then
        return "right"
      else
        error("Invalid arrow key!")
      end
    end

I get this way when i exprimenting with getch_unix() function that i found on internet searching to get a pressed key without echoing at all and not waiting for you press enter button.

EDIT: Im also found https://github.com/max1220/lua-getch it has several useful features for your use case more info at the git if my code still not fit with your use case it also shorthen your code

Upvotes: 2

Lucien Greathouse
Lucien Greathouse

Reputation: 593

Lua on its own does not provide any libraries that aren't part of ANSI C, which is part of the extensive portability of the language.

As such, you miss out on things like keyboard input and graphics, but also operations that might be considered "simple," like listing the files in a directory.

Most likely, there's a library for what you need, and if there isn't, then keep in mind that Lua is one of the friendliest languages to write C-side libraries for.

LOVE is a good framework that couples quite a few extensions to Lua (including a rather abstracted interface to SDL) with a distribution method, more or less. If you're developing games with Lua, this is a good place to start.

EDIT: If you're on Windows with LuaJIT and you're okay with global key hooks, then I developed a library recently (May 2015) that solves that problem: https://github.com/LPGhatguy/global-keys

Upvotes: 5

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

There are libraries like curses that may help; here is one tutorial one reading arrow keys with curses in Lua. There is also luaposix library, which includes curses.

Upvotes: 2

Related Questions