Input Object

class Input extends Object

Describes one keyboard or mouse reported by the current input source. Instances come from Input.Devices, A_EventInfo.Device or an OnDeviceChange callback.

This class is exported by the Ks module:

#Import Ks { Input }

Devices

Devices := Input.Devices

A read-only property returning a normal Array of Input objects for the current keyboards and mice. Each read returns a new list of new objects. A list already held by a script does not change when devices are connected or removed.

An empty Array means that no keyboard or mouse was listed. An unavailable inventory, denied access or enumeration failure throws an OSError. Array indexes are ordinal positions, unrelated to Id. Use Array.Filter to select devices by their properties; a selection can contain no devices, one device or several devices.

Device properties

All properties are read-only and contain the information completed when the descriptor was returned. Property reads do not query the device again. Unavailable metadata is an empty string in v2.0 mode and unset in v2.1 mode. A reported numeric zero or false remains a value. A descriptor keeps its values after the device disconnects.

PropertyDescription
IdAn opaque Integer identifying the device within its current input source. It is not an Array index or a permanent hardware identity. Values can change or be reused after reconnection, resume or a source restart. Identifiers from different sources need not be comparable.
Kind"Keyboard" or "Mouse".
NameThe name reported by the input source, when available. Names can repeat.
VendorIdThe numeric vendor identifier.
ProductIdThe numeric product identifier.
HardwareIdThe input source's hardware identity string. Its format and persistence depend on the source. It may identify a model or interface rather than one physical unit.
ManufacturerThe reported manufacturer text. Native Linux enumeration does not supply it.
ConnectionThe reported bus or transport, such as USB or Bluetooth. Native Windows enumeration does not supply it.
LocationAn opaque platform location string. Do not interpret it as a portable path.
IsInternalWhether the source reports the device as built in. Native Windows and Linux enumeration do not supply it.

A device can expose several interfaces or both kinds, so an entry is not necessarily one physical unit. Identical devices can share a name, vendor/product pair or hardware ID. No property is guaranteed to identify one unit permanently. Compare reported values when selecting a device; objects from separate reads are distinct descriptors.

The current event's device

Device := A_EventInfo.Device

For native Linux input and managed capture, keyboard and mouse hotkeys, their #HotIf expressions and InputHook callbacks expose A_EventInfo.Device as an Input descriptor when the event identifies a nonzero device. Generated input without a device identifier and native Windows/macOS hook events have no Device property.

The description is resolved when Device is read; reading Timestamp or IsAutoRepeat does not look up devices. Cached metadata from the event's original source remains available after device removal or source loss. If no metadata is known, the descriptor contains its captured Id and Kind, with other metadata unavailable.

GetKeyState

IsDown := Device.GetKeyState(KeyName)

Note: Per-device state is supplied through managed capture, or through native Linux keysharp-input with an existing InputMonitoring grant. Native Windows and macOS sources do not supply it. Third-party driver and hardware capture on macOS remain unverified.

KeyName is a keyboard key or mouse button from the key list. Returns true while that key or button is physically held on this device, otherwise false. This method takes no Mode parameter and does not read controller, wheel or toggle state. Keypad names follow the NumLock and logical Shift state shared by all keyboards, as hotkeys do.

An invalid name, including a controller or wheel name, raises a ValueError; a value which cannot be converted to a String raises a TypeError. An unsupported source, unavailable state, an unknown or removed device, or a descriptor from an earlier script or input-service connection raises an OSError. A source reset permanently invalidates that source's descriptors. There is no aggregate-state fallback. Use GetKeyState(KeyName, Mode) for aggregate logical, physical or toggle state.

Reading state does not request permission or install input hooks. A denied or expired grant raises OSError rather than reporting the key as up.

Managed capture tracks physical state from reported events; state is unavailable until the first physical event for the device. A detected HardwareId change clears that state, but reuse of an Id cannot be detected when HardwareId is unchanged or has not yet been read. Resetting, losing or removing the capture provider invalidates its descriptors. Changing whether keyboards or mice are captured clears the affected state without invalidating descriptors. An aggregate key-state provider does not supply per-device state.

OnDeviceChange

Hook := Input.OnDeviceChange(Callback)

Returns a running Input.DeviceHook which calls Callback for changes found in the keyboard and mouse inventory. Callback is a function object accepting three arguments:

Callback(Hook, Change, Device)

Hook is the subscription, Change is "Added", "Removed" or "Changed", and Device is an Input snapshot. A removed device retains its last-known metadata, but GetKeyState cannot read its state.

The current inventory is the starting point; devices already present do not produce initial Added callbacks. Notifications are coalesced and report differences between inventories, so a device connected and removed between inventories might not be reported.

On Windows and macOS, arrival and removal trigger inventory refreshes; a change only to a device's name or other metadata does not. Such changes can produce Changed callbacks when a later refresh discovers them.

With managed capture, native device notifications still trigger inventory refreshes and retain their platform and access requirements. Changes reported only by the provider do not trigger a refresh.

Subscribing raises an OSError when the native device-change source is unavailable or access is denied. On Linux, notifications require keysharp-input client ABI 1.1 or newer, a matching service and an existing InputMonitoring grant. Subscribing does not request permission or install keyboard or mouse input hooks.

Hooks

Hooks := Input.Hooks

A read-only property returning a snapshot Array of running Input.DeviceHook objects in the order they were started, across all real threads. Restarting a hook moves it to the end. Changing the returned Array does not start or stop subscriptions.

Input.DeviceHook

class Input.DeviceHook extends EventHook

Created by Input.OnDeviceChange, with no additional members beyond EventHook. Use Start() and Stop() to control the subscription, and read InProgress and EndReason for its state. Callbacks run on the real thread which started the subscription; EventHook's queued-callback rules apply.

Losing the device-change source or its permission, or failing to refresh the inventory, ends the subscription with EndReason "Failed" and stops callbacks. Call Start() to retry explicitly; a failed retry leaves the hook ended. It does not reconnect automatically.

The hook does not keep the script or its real thread running. A script which only waits for device changes calls Persistent.

Remarks

A managed capture provider can supply its own inventory and device IDs. Otherwise, Input.Devices lists native devices, whose descriptors cannot query the provider's state. An empty provider inventory does not fall back to native devices.

Windows

Native enumeration lists the keyboard and mouse interfaces known to Windows Raw Input. Id contains the Raw Input device handle's value. Names and hardware identifiers come from system device metadata. VendorId and ProductId use native HID attributes when available; an ACPI identifier does not supply numeric USB vendor or product identifiers.

Linux

Id contains the keysharp-input device ID. Device information requires an available keysharp-input service and an existing Input Monitoring grant. Listing devices does not request a grant or initialize input forwarding. Client ABI 1.1 and a matching service can check an existing grant directly. With an older client or service, listing succeeds only if another authorized input operation has already given the current connection access to device information; otherwise it throws an OSError.

macOS

Native enumeration reads keyboard and mouse descriptions from IOKit HID services, using a supplied serial number as HardwareId. When available, Id is the registry entry ID interpreted as a signed 64-bit Integer and is valid for that registry entry's lifetime.

Examples

Lists the reported keyboards.

#ErrorStdOut
#Warn All, StdOut
#Import Ks { Input }

for Device in Input.Devices.Filter((Device) => Device.Kind == "Keyboard")
    OutputDebug Device.Name ": " Device.HardwareId

Logs keyboard and mouse inventory changes until the script exits.

#ErrorStdOut
#Warn All, StdOut
#Import Ks { Input }

Persistent
Input.OnDeviceChange((Hook, Change, Device) => OutputDebug(Change ": " Device.Kind " " Device.Name))