module Selenium::WebDriver::KeyActions

def key_action(*args, action: nil, device: nil)

def key_action(*args, action: nil, device: nil)
  key_input = key_input(device)
  click(args.shift) if args.first.is_a? Element
  key_input.send(action, args.last)
  tick(key_input)
  self
end

def key_down(*args, device: nil)

def key_down(*args, device: nil)
  key_action(*args, action: :create_key_down, device: device)
end

def key_input(name = nil)

def key_input(name = nil)
  device(name: name, type: Interactions::KEY) || add_key_input('keyboard')
end

def key_up(*args, device: nil)

def key_up(*args, device: nil)
  key_action(*args, action: :create_key_up, device: device)
end

def send_keys(*args, device: nil)

def send_keys(*args, device: nil)
  click(args.shift) if args.first.is_a? Element
  args.map { |x| x.is_a?(String) ? x.chars : x }.flatten.each do |arg|
    key_down(arg, device: device)
    key_up(arg, device: device)
  end
  self
end