Created
December 10, 2023 08:42
-
-
Save amigojapan/3e70af7b2c9ac9719e06e8ac1d35a1da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local posix = require("posix") | |
-- Function to set non-blocking mode for a file descriptor | |
local function setNonBlocking(fd) | |
local flags = posix.fcntl(fd, posix.F_GETFL, 0) | |
posix.fcntl(fd, posix.F_SETFL, flags + posix.O_NONBLOCK) | |
end | |
-- Open stdin in non-blocking mode | |
setNonBlocking(0) -- 0 is the file descriptor for stdin | |
-- Read input with non-blocking | |
while true do | |
posix.sleep(1) | |
local input, err = io.read(1) -- Read one character | |
if input then | |
print("You entered:", input) | |
elseif err == "timeout" then | |
-- No input yet, do something else or continue waiting | |
--posix.sleep(5) -- Sleep for 1 second (adjust as needed) | |
else | |
print("no input") | |
--break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment