Project

evdev

0.0
No commit activity in last 3 years
No release in over 3 years
There's a lot of open issues
A ruby object wrapper around libevdev bindings.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Evdev

A ruby wrapper around Libevdev for a nice and easy access to linux's event devices like keyboards and mice.

Installation

Add this line to your application's Gemfile:

gem 'evdev'

And then execute:

$ bundle

Or install it yourself as:

$ gem install evdev

Usage

Let's have a look at my keyboard:

Initializing a device

require 'evdev'
keyboard = Evdev.new('/dev/input/event0')

Querying the device

keyboard.name                       # => "Logitech USB Receiver"
keyboard.phys                       # => "usb-0000:00:1d.2-1/input0"
keyboard.uniq                       # => ""
keyboard.vendor_id                  # => 1133
keyboard.product_id                 # => 50473
keyboard.bustype                    # => 3 (LinuxInput::BUS_USB)
keyboard.version                    # => 273
keyboard.driver_version             # => 65537
keyboard.has_property? :pointer     # => false

Grabbing a device

keyboard.grab                       # => true if successful, else false
keyboard.ungrab                     # => true if successful, else false

Event handling

Evdev lumps event type and code together. You just need to give it the name of the code and it derives the implied type from it internally.

keyboard.supports_event? :KEY_A     # => true
keyboard.supports_event? :ABS_X     # => false

key_handler = keyboard.on(:KEY_A, :KEY_S, :KEY_D, :KEY_F) do |state, key|
  puts "#{%w(released pressed repeated)[state]} #{key}"
end

loop do
  begin
    keyboard.handle_event
  rescue Evdev::AwaitEvent
    Kernel.select([keyboard.event_channel])
    retry
  end
end

Removing the handler:

key_handler.cancel

Evdev includes the CallbacksAttachable mixin to attach and detach callbacks. Have a look at its API in its own documentation.