Code Monkey home page Code Monkey logo

ruby-uinput's Introduction

Uinput

FFI Structs and Constants of to use uinput with ruby. Depends on the linux_input gem.

This is pretty low level. For a higher level and more user friendly abstraction have a look at the uinput-device gem.

Installation

Add this line to your application's Gemfile:

gem 'uinput'

And then execute:

$ bundle

Or install it yourself as:

$ gem install uinput

Usage

require 'uinput'

Creating a new virtual keyboard having an A key:

file = File.open('/dev/uinput', Fcntl::O_WRONLY | Fcntl::O_NDELAY)
device = Uinput::UinputUserDev.new

device[:name] = "Virtual Ruby Device"
device[:id][:type] = LinuxInput::BUS_VIRTUAL
device[:id][:vendor] = 0
device[:id][:product] = 0
device[:id][:version] = 0

file.ioctl(Uinput::UI_SET_KEYBIT, LinuxInput::KEY_A)
file.ioctl(Uinput::UI_SET_EVBIT, LinuxInput::EV_KEY)
file.ioctl(Uinput::UI_SET_EVBIT, LinuxInput::EV_SYN)

file.syswrite(device.pointer.read_bytes(device.size))
file.ioctl(Uinput::UI_DEV_CREATE)

Typing an 'a' on our keyboard:

# key down event
event = LinuxInput::InputEvent.new
event[:time] = LinuxInput::Timeval.new
event[:time][:tv_sec] = Time.now.to_i
event[:type] = LinuxInput::EV_KEY
event[:code] = LinuxInput::KEY_A
event[:value] = 1
file.syswrite(event.pointer.read_bytes(event.size))

# sync event for key down
event = LinuxInput::InputEvent.new
event[:time] = LinuxInput::Timeval.new
event[:time][:tv_sec] = Time.now.to_i
event[:type] = LinuxInput::EV_SYN
event[:code] = LinuxInput::SYN_REPORT
event[:value] = 0
file.syswrite(event.pointer.read_bytes(event.size))

# key up event
event = LinuxInput::InputEvent.new
event[:time] = LinuxInput::Timeval.new
event[:time][:tv_sec] = Time.now.to_i
event[:type] = LinuxInput::EV_KEY
event[:code] = LinuxInput::KEY_A
event[:value] = 0
file.syswrite(event.pointer.read_bytes(event.size))

# sync event for key up
event = LinuxInput::InputEvent.new
event[:time] = LinuxInput::Timeval.new
event[:time][:tv_sec] = Time.now.to_i
event[:type] = LinuxInput::EV_SYN
event[:code] = LinuxInput::SYN_REPORT
event[:value] = 0
file.syswrite(event.pointer.read_bytes(event.size))

Destroying the device:

file.ioctl(Uinput::UI_DEV_DESTROY, nil)

(Re-)Compiling the interface on a linux machine

Reads your local linux/uinput.h

$ rake ffi:generate

ruby-uinput's People

Contributors

christopheraue avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.