lua-users home
lua-l archive

[ANN] lua-evdev: interact with input devices on Linux

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


Hello, all;
I'm happy to announce a simple Lua 5.2 module I put together for using
the Linux evdev (EVent DEVice) and uinput (User-mode Input) APIs.
This can be useful for reading data from drawing tablets, game controllers,
as well as emulating keyboards, mice, tablets, and controllers.
The "Device" object is suitable for polling by cqueues if you like that
event loop,
but there are no special dependencies.
You can get it by installing the "evdev" rock,
or find the source and fuller documentation at
https://github.com/Tangent128/lua-evdev/tree/evdev-1.0
Here's hoping somebody else can have fun with it!
Example: Reading events from /dev/input/event* nodes:
---
local evdev = require "evdev"
-- (assuming event0 is the keyboard today)
local keyboard = evdev.Device "/dev/input/event0"
while true do
 local timestamp, eventType, eventCode, value = keyboard:read()
 if eventType == evdev.EV_KEY then
 if eventCode == evdev.KEY_ESC then
 break
 end
 if value == 0 then
 print("Key Released:", eventCode)
 else
 print("Key Pressed:", eventCode)
 end
 end
end
Example: Creating a fake mouse:
---
local e = require "evdev"
local fakeMouse = e.Uinput "/dev/uinput"
-- register supported events
fakeMouse:useEvent(e.EV_KEY)
fakeMouse:useEvent(e.EV_REL)
fakeMouse:useKey(e.BTN_0)
fakeMouse:useKey(e.BTN_1)
fakeMouse:useRelAxis(e.REL_X,-100,100)
fakeMouse:useRelAxis(e.REL_Y,-100,100)
fakeMouse:init()
-- inject a few events; mouse move, button press, button release
fakeMouse:write(e.EV_REL, e.REL_X, -50)
fakeMouse:write(e.EV_REL, e.REL_Y, 0)
fakeMouse:write(e.EV_SYN, e.SYN_REPORT, 0)
fakeMouse:write(e.EV_KEY, e.BTN_0, 1)
fakeMouse:write(e.EV_SYN, e.SYN_REPORT, 0)
fakeMouse:write(e.EV_KEY, e.BTN_0, 0)
fakeMouse:write(e.EV_SYN, e.SYN_REPORT, 0)
-- dispose
fakeMouse:close()
---
"Tangent"
Joseph Wallace

AltStyle によって変換されたページ (->オリジナル) /