# `Kuroneko.Gadget.Input`
[🔗](https://github.com/cocoa-xu/kuroneko_gadget/blob/v0.5.0/lib/kuroneko/gadget/input.ex#L1)

The KVM's keyboard, mouse and touch input to the target computer, on the
`:keyboard`, `:mouse` and `:relative_mouse` functions of
`Kuroneko.Gadget`.

The process keeps the keys and buttons held and sends a report on each
change, so callers only send changes:

    Kuroneko.Gadget.Input.key("ShiftLeft", :down)
    Kuroneko.Gadget.Input.key("KeyA", :down)
    Kuroneko.Gadget.Input.key("KeyA", :up)
    Kuroneko.Gadget.Input.key("ShiftLeft", :up)

    Kuroneko.Gadget.Input.pointer(x: 0.5, y: 0.5)
    Kuroneko.Gadget.Input.pointer(buttons: [:left])
    Kuroneko.Gadget.Input.pointer(buttons: [])
    Kuroneko.Gadget.Input.pointer(wheel: -1)

    Kuroneko.Gadget.Input.type("Hello!\n")

    Kuroneko.Gadget.Input.relative(dx: 10, dy: -3)
    Kuroneko.Gadget.Input.touch([{0, 0.2, 0.3}, {1, 0.6, 0.3}])

Keys are `KeyboardEvent.code` values (see `Kuroneko.Gadget.HID.Keymap`)
or HID usages; keys the keyboard has no usage for are ignored.
`pointer/2` and `touch/2` place the pointer from 0.0 (left, top) to 1.0
(right, bottom) across the target's screen, with the `:mouse` function in
its mode: an absolute mouse, a pen (where the left button puts the tip
down) or a touch screen (where it touches). `relative/2` moves the
`:relative_mouse`, which BIOS setups and games take.

Input wakes a target that sleeps, where it lets its USB devices wake it.
Without a device (the function is missing, or went away while the gadget
changed) reports are dropped, and the device is opened again a little
later. While the target does not take reports (unplugged, asleep), only
the newest report is kept for when it does: no key stays held.

## Options

  * `:name` - the name to register, `Kuroneko.Gadget.Input` by default
  * `:keyboard`, `:mouse`, `:relative_mouse` - device paths; by default
    the devices of the functions of those names. Or `{module, arg}`, for
    input that goes elsewhere, such as over Bluetooth: a process that
    `module.start_link(arg)` starts and links, which takes each report
    as `{:report, report}` and does not wake the target
  * `:pointer_mode` - the mode of the absolute pointer, by default the
    one of the gadget's `:mouse` function
  * `:gadget` - the gadget, `Kuroneko.Gadget.default_gadget/0` by default
  * `:udc_class` - where the device controllers are, `/sys/class/udc` by
    default

# `button`

```elixir
@type button() :: :left | :right | :middle | :back | :forward
```

# `contact`

```elixir
@type contact() :: {0..9, number(), number()}
```

A touch: an id from 0 to 9 and where, from 0.0 to 1.0.

# `device_stats`

```elixir
@type device_stats() :: %{
  device: Path.t() | nil,
  sent: non_neg_integer(),
  dropped: non_neg_integer()
}
```

# `key`

```elixir
@type key() :: Kuroneko.Gadget.HID.Keymap.code() | 4..221 | 224..231
```

A `KeyboardEvent.code` value or a HID usage.

# `option`

```elixir
@type option() ::
  {:name, GenServer.name() | nil}
  | {:keyboard, writer()}
  | {:mouse, writer()}
  | {:relative_mouse, writer()}
  | {:pointer_mode, pointer_mode()}
  | {:gadget, Path.t()}
  | {:udc_class, Path.t()}
```

# `pointer_change`

```elixir
@type pointer_change() ::
  {:x, number()}
  | {:y, number()}
  | {:buttons, [button()]}
  | {:wheel, integer()}
  | {:pan, integer()}
```

# `pointer_mode`

```elixir
@type pointer_mode() :: :absolute | :stylus | :touch
```

How the absolute pointer reports: as a mouse, a pen or a touch screen.

# `relative_change`

```elixir
@type relative_change() ::
  {:dx, integer()}
  | {:dy, integer()}
  | {:buttons, [button()]}
  | {:wheel, integer()}
  | {:pan, integer()}
```

# `stats`

```elixir
@type stats() :: %{
  keyboard: device_stats(),
  mouse: device_stats(),
  relative_mouse: device_stats()
}
```

# `writer`

```elixir
@type writer() :: Path.t() | {module(), term()}
```

Where the reports of a device go: its path, or a process to start.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `key`

```elixir
@spec key(GenServer.server(), key(), :down | :up) :: :ok
```

Presses or releases a key.

# `pointer`

```elixir
@spec pointer(GenServer.server(), [pointer_change()]) :: :ok
```

Moves the absolute pointer, presses or releases buttons, turns the wheels
(`:wheel` up, `:pan` right). What is not given stays as it is; the wheels
turn only once.

# `pointer_mode`

```elixir
@spec pointer_mode(GenServer.server()) :: pointer_mode()
```

The mode of the absolute pointer: `:absolute`, `:stylus` or `:touch`.

# `relative`

```elixir
@spec relative(GenServer.server(), [relative_change()]) :: :ok
```

Moves the relative mouse by `:dx` and `:dy` (right, down), presses or
releases its buttons, turns its wheels.

# `release_all`

```elixir
@spec release_all(GenServer.server()) :: :ok
```

Releases all keys, buttons and touches and stops any typing, for example
when the user goes away. It does not wake the target.

# `start_link`

```elixir
@spec start_link([option()]) :: GenServer.on_start()
```

Starts the process. See the module documentation for the options.

# `stats`

```elixir
@spec stats(GenServer.server()) :: stats()
```

The devices in use and the reports sent and dropped for each.

# `touch`

```elixir
@spec touch(GenServer.server(), [contact()]) :: :ok
```

The contacts touching the screen now, up to ten: on a touch screen each,
else the first as the pointer with the left button.

# `type`

```elixir
@spec type(GenServer.server(), String.t()) :: :ok
```

Types text on the target as a US keyboard would, a key at a time, some 60
characters a second, after any text typed before. Characters that
keyboard has no key for are left out. `release_all/1` stops the typing.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
