Processes inputs read from /dev/input device streams

What's New



Fix various bugs in the InputDevice.ActiveStreamSequence implementation.

Full Changelog: 6.1.1...6.2.0


GitHub release Tests Codacy Badge codecov Docs

Processes inputs read from /dev/input device streams.


Add the following dependency to your Package.swift:

.package(url: "", from: "6.0.0"),


  • For Swift as of version 5.3, use DeviceInput version 4.x.y.
  • For Swift as of version 5.5, use DeviceInput version 5.x.y.
  • For Swift as of version 5.6, use DeviceInput version 6.x.y.



An InputDevice is the entry point for streaming input events. Create an input device by passing it the path to the input file. By default, an input device "grabs" its input file when it begins streaming for events. By doing so, no other process (e.g. the default system input handler) will receive the events of the input device. However, this only works on Linux. You can manually pass false to the grabDevice parameter in the initializer if you explicitly don't want to grab the device.

You then call startReceivingEvents(informing:) on the input device. The parameter you pass to this method is an InputDevice.EventConsumer. It takes a DispatchQueue and a handler. The latter is called on the queue and is passed the device as well as an array of events received from the device. After calling startReceivingEvents, the input device will start streaming for events and notify the passed event consumer. You can register more event consumers by calling addEventConsumer. You can as well deregister event consumers using removeEventConsumer. Note that when the last event consumer is removed, the input device automatically stops streaming events. Finally, if you're done with the input device, call stopReceivingEvents to deregister all event consumers and stop streaming events.


Registered event consumers are passed an array of InputEvent structs. It represents an input_event from the linux source, but uses native swift types instead of C types. Constants help dealing with events. However, it is currently optimized for handling key state change events. Axis events and other types might need additional work (see the section below about possible features).

Possible Features

While not yet integrated, the following features might provide added value and could make it into DeviceInput in the future:

  • Improved InputEvent that is optimized for the various kinds of events sent by the input subsystem.


The API is documented using header doc. If you prefer to view the documentation as a webpage, there is an online version available for you.


If you find a bug / like to see a new feature in DeviceInput there are a few ways of helping out:

  • If you can fix the bug / implement the feature yourself please do and open a PR.
  • If you know how to code (which you probably do), please add a (failing) test and open a PR. We'll try to get your test green ASAP.
  • If you can do neither, then open an issue. While this might be the easiest way, it will likely take the longest for the bug to be fixed / feature to be implemented.


See LICENSE file.


  • Swift Tools 5.6.0
View More Packages from this Author


Last updated: Thu Mar 16 2023 20:56:17 GMT-0500 (GMT-05:00)