lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wednesday 22 December 2010 06:11:45 Luiz Henrique de Figueiredo wrote:
> > I want to ask the user to type a keystroke, and get that keystroke
> > without the user needing to press Enter.
> 
> See http://lua-users.org/lists/lua-l/2006-06/msg00314.html

Confirmed!

I used the bash commands from my old perl code instead of the code in your 
URL, but it's basically the same thing:

========================================
#!/usr/bin/lua

function getch_unix()
	os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
	local key = io.read(1)
	os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
	return(key);       
end

for i=1 , 5 do
	io.write("Hit key==>")
	local key = getch_unix()
	io.write("\nYou pressed ")
	io.write(key)
	io.write("\n")
end
========================================

It works perfectly on Ubuntu Linux, although I'd still have to try it on BSD, 
OpenSolaris and the like. And like you said, it's valid only for printing 
characters -- it fails on cursor keys and function keys because they return 
more than one character's worth of data per keystroke.

Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt