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

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

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

Tangent 128

Announcing version 2.0.0 of my lua-evdev module, for those interested.

Lua-evdev is a module for interacting with the Linux evdev (EVent
DEVice) and uinput (User-mode Input) APIs.

This can be used to read input from drawing tablets, game controllers,
keyboards, mice, and other HID devices, as well as to create virtual
input devices to emulate or remap such devices.


Features:
---------
* Lua 5.2 and 5.3 support
* read input device events from an evdev.Device() object
* write LED control events to an evdev.Device() object
* evdev.Device() object is pollable by cqueues
* control virtual input device events with an evdev.Uinput() object
* no dependencies besides Lua, libc, and Linux


Where To Get:
-------------
Lua-evdev is available via LuaRocks; install "evdev"

Source and documentation can be browsed on Github:
https://github.com/Tangent128/lua-evdev/tree/evdev-2.0/

====

Example 1: Reading keyboard events:
-----------------------------------
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 2: Turning on a keyboard LED:
-------------------------------------
local e = require "evdev"
-- (again assuming event0 is the keyboard here)
local keyboard = evdev.Device("/dev/input/event0", true)
keyboard:write(e.EV_LED, e.LED_CAPSL, 1)
keyboard:write(e.EV_SYN, e.SYN_REPORT, 0)


Example 3: Creating a fake mouse via uinput:
--------------------------------------------
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()


Happy New Year!
====
Joseph "Tangent" Wallace