Kleidos API Search

In development. It changes without notice.

Topic

HID Profiles & Keyboard Tables

From text to key presses: the typer, the codes of the keys, and the accented letters each keyboard layout can type.

kleidos::ble::profile::HidTyper decodes UTF-8 and emits key reports through an interface, kleidos::ble::profile::IHidSink, so it knows no radio. A character the chosen layout cannot type is reported, never skipped.

The layout must be the one the computer uses: a keyboard sends keys, and the computer decides which letter each is.

See also
BLE Facade & HID Manager, BLE Types

Files

file src/ble/profile/battery_level.h
What the Battery Level characteristic serves, and when it notifies.
file src/ble/profile/hid_boot_report.h
Pure HID Boot Protocol report helpers (transport-agnostic).
file src/ble/profile/hid_burst_guard.h
A burst typed as the host will read it: its Caps Lock off, its HID protocol held, every key lifted when it ends early.
file src/ble/profile/hid_keystroke.h
One keystroke on the air: a press, its release, and what happens when the release cannot go out.
file src/ble/profile/hid_profile.h
Static definition of the BLE HID keyboard profile.
file src/ble/profile/hid_scancodes.h
Shared HID keyboard scancodes and modifier bits.
file src/ble/profile/hid_typer.h
Pure UTF-8 → HID report typer, independent of any transport.
file src/ble/profile/i_hid_sink.h
Abstract HID report sink decoupling HidTyper from any transport.
file src/ble/profile/latin1_tables.h
Per-layout Latin-1 codepoint → HID keystroke tables.

Classes

class kleidos::ble::profile::BatteryNotifyLatch
Whether a subscribed host is owed a Battery Level notification. More about kleidos::ble::profile::BatteryNotifyLatch
class kleidos::ble::profile::IHostKeyboardState
What the host last told the keyboard on the live link. More about kleidos::ble::profile::IHostKeyboardState
class kleidos::ble::profile::GuardedHidSink
An IHidSink that types through inner as the host will read it. More about kleidos::ble::profile::GuardedHidSink
The notify path a keystroke goes out on. More about kleidos::ble::profile::IReportLink
struct kleidos::ble::profile::KeystrokePacing
The timing and the bounds of one keystroke. More about kleidos::ble::profile::KeystrokePacing
class kleidos::ble::profile::HidProfile
Static facade over the HID report map and the active keyboard layout. More about kleidos::ble::profile::HidProfile
struct kleidos::ble::profile::TypingResult
Detailed result of HidTyper::typeUtf8(). More about kleidos::ble::profile::TypingResult
class kleidos::ble::profile::HidTyper
Stateless UTF-8 keyboard typer that drives an IHidSink. More about kleidos::ble::profile::HidTyper
class kleidos::ble::profile::IHidSink
Transport abstraction that turns HID reports into real keystrokes. More about kleidos::ble::profile::IHidSink
struct kleidos::ble::profile::Latin1Entry
One Latin-1 codepoint expressed as one or two HID keystrokes. More about kleidos::ble::profile::Latin1Entry
struct kleidos::ble::profile::Latin1Table
Lightweight view over a layout's sorted Latin1Entry array. More about kleidos::ble::profile::Latin1Table

Typedefs

usingkleidos::ble::profile::KeyboardReport
One 8-byte keyboard input report.

Enumerations

enum class kleidos::ble::profile::ReportPath : uint8_t { kleidos::ble::profile::ReportPath::Report , kleidos::ble::profile::ReportPath::BootKeyboard }
Which HID characteristic a keystroke notification must target. More about kleidos::ble::profile::ReportPath
enum class kleidos::ble::profile::ProtocolModeWrite : uint8_t { kleidos::ble::profile::ProtocolModeWrite::Accepted , kleidos::ble::profile::ProtocolModeWrite::BadLength , kleidos::ble::profile::ProtocolModeWrite::BadValue }
How a host's write to Protocol Mode (0x2A4E) is answered. More about kleidos::ble::profile::ProtocolModeWrite
enum class kleidos::ble::profile::BurstStart : uint8_t { kleidos::ble::profile::BurstStart::Ready , kleidos::ble::profile::BurstStart::CapsLockStuck , kleidos::ble::profile::BurstStart::SendFailed }
How a burst's start ended. More about kleidos::ble::profile::BurstStart
enum class kleidos::ble::profile::BurstAbort : uint8_t { kleidos::ble::profile::BurstAbort::None , kleidos::ble::profile::BurstAbort::SendFailed , kleidos::ble::profile::BurstAbort::ProtocolChanged , kleidos::ble::profile::BurstAbort::CapsLockOn }
Why a guarded report was refused mid-burst. More about kleidos::ble::profile::BurstAbort
enum class kleidos::ble::profile::KeystrokeOutcome : uint8_t { kleidos::ble::profile::KeystrokeOutcome::Sent , kleidos::ble::profile::KeystrokeOutcome::NotSent , kleidos::ble::profile::KeystrokeOutcome::NotReserved , kleidos::ble::profile::KeystrokeOutcome::LinkDropped }
How a keystroke ended. More about kleidos::ble::profile::KeystrokeOutcome
enum class kleidos::ble::profile::PaceVerdict : uint8_t { kleidos::ble::profile::PaceVerdict::Proceed , kleidos::ble::profile::PaceVerdict::Wait , kleidos::ble::profile::PaceVerdict::GiveUp }
What a press does about the transmit buffers. More about kleidos::ble::profile::PaceVerdict
enum class kleidos::ble::profile::PacedStep : uint8_t { kleidos::ble::profile::PacedStep::Sent , kleidos::ble::profile::PacedStep::Wait , kleidos::ble::profile::PacedStep::GaveUp , kleidos::ble::profile::PacedStep::Failed }
How one try at a paced keystroke ended. More about kleidos::ble::profile::PacedStep
enum class kleidos::ble::profile::TypingOutcome : uint8_t { kleidos::ble::profile::TypingOutcome::Ok, kleidos::ble::profile::TypingOutcome::NotReady, kleidos::ble::profile::TypingOutcome::SendFailed, kleidos::ble::profile::TypingOutcome::InvalidUtf8, kleidos::ble::profile::TypingOutcome::UnsupportedCharacter }
Outcome of a typing pass. More about kleidos::ble::profile::TypingOutcome

Functions

constexpr uint8_tkleidos::ble::profile::servedBatteryLevel (bool known, int percent)
The byte the Battery Level characteristic serves for a sample.
constexpr boolkleidos::ble::profile::batteryNotifyAllowed (bool subscribed, bool connected, bool encrypted, bool authenticated, bool bonded, uint8_t keySize)
Whether the Battery Level may be notified on the host's link now.
constexpr std::array< uint8_t, kBootKeyboardReportSize >kleidos::ble::profile::buildBootKeyboardReport (uint8_t modifiers, uint8_t keycode)
Build the fixed 8-byte HID boot keyboard input report.
constexpr ReportPathkleidos::ble::profile::selectReportPath (uint8_t protocolMode)
Decide which characteristic a keystroke should be notified on.
constexpr ProtocolModeWritekleidos::ble::profile::checkProtocolModeWrite (const uint8_t *data, std::size_t len)
Judge a host's write to Protocol Mode before anything is stored.
constexpr boolkleidos::ble::profile::ledWriteValid (std::size_t len)
Whether a write to a keyboard Output Report carries the LED byte.
constexpr boolkleidos::ble::profile::capsLockOn (uint8_t leds)
Whether the host's LED byte says its Caps Lock is on.
boolkleidos::ble::profile::sendKey (IHidSink &sink, uint8_t modifiers, uint8_t scancode)
Send one key through sink, lifting every key if it fails.
PaceVerdictkleidos::ble::profile::paceVerdict (uint16_t freeBuffers, uint32_t waitedMs, const KeystrokePacing &pacing=kKeystrokePacing)
Whether a press may go out now.
PacedStepkleidos::ble::profile::keystrokeStep (IReportLink &link, uint8_t modifiers, uint8_t keycode, uint32_t waitedMs, const KeystrokePacing &pacing=kKeystrokePacing)
One try at a keystroke that waits for its transmit buffers.
template<typename Attempt, typename Wait>
PacedStepkleidos::ble::profile::runPacedKeystroke (Attempt &&attempt, Wait &&wait, const KeystrokePacing &pacing=kKeystrokePacing)
Try a keystroke until it goes out, fails, or gives up.
constexpr boolkleidos::ble::profile::liftOwed (uint16_t targetAttr, uint16_t lastReportAttr)
Whether a burst that ends early owes the host an all-zero report.
KeystrokeOutcomekleidos::ble::profile::sendKeystroke (IReportLink &link, uint8_t modifiers, uint8_t keycode, const KeystrokePacing &pacing=kKeystrokePacing)
Send one keystroke and leave no key held.
KeystrokeOutcomekleidos::ble::profile::liftAllKeys (IReportLink &link, const KeystrokePacing &pacing=kKeystrokePacing)
Send one all-zero report, as a release is sent: retried, else the link ends.
Latin1Tablekleidos::ble::profile::getLatin1Table (KbLayout layout)
Returns the Latin-1 table for the given layout, or {nullptr, 0} if no extra coverage is provided (US default — falls back to printable-ASCII only).

Variables

constexpr uint8_tkleidos::ble::profile::kBatteryLevelFull
Highest value the Battery Level characteristic carries, in percent.
constexpr uint8_tkleidos::ble::profile::kBatteryLevelUnknown
constexpr uint8_tkleidos::ble::profile::kProtocolModeBoot
HOGP Protocol Mode value for Boot Protocol (host wrote 0x00 to 0x2A4E).
constexpr uint8_tkleidos::ble::profile::kProtocolModeReport
HOGP Protocol Mode value for Report Protocol (default; host wrote 0x01).
constexpr std::size_tkleidos::ble::profile::kBootKeyboardReportSize
Fixed size of a boot keyboard report: [modifier][reserved][keycode×6].
constexpr std::size_tkleidos::ble::profile::kKeyboardLedReportSize
Size of the keyboard LED output report the host writes: one byte of LED bits.
constexpr uint8_tkleidos::ble::profile::kHidLedCapsLock
constexpr uint32_tkleidos::ble::profile::kCapsLockConfirmMs
How long the host has to confirm, in its LED byte, a Caps Lock keystroke.
constexpr uint32_tkleidos::ble::profile::kCapsLockPollMs
How often the LED byte is looked at while waiting for that confirmation.
constexpr uint32_tkleidos::ble::profile::kKeyReleaseDelayMs
How long a key is held, from its press to its release.
constexpr KeystrokePacingkleidos::ble::profile::kKeystrokePacing
constexpr uint8_tkleidos::ble::profile::kHidKeyA
'a' — base of the letter range.
constexpr uint8_tkleidos::ble::profile::kHidKey1
'1' — base of the digit range.
constexpr uint8_tkleidos::ble::profile::kHidKey0
'0'.
constexpr uint8_tkleidos::ble::profile::kHidKeyReturn
Return / Enter.
constexpr uint8_tkleidos::ble::profile::kHidKeyEscape
Escape.
constexpr uint8_tkleidos::ble::profile::kHidKeyBackspc
Backspace.
constexpr uint8_tkleidos::ble::profile::kHidKeyTab
Tab.
constexpr uint8_tkleidos::ble::profile::kHidKeySpace
Space.
constexpr uint8_tkleidos::ble::profile::kHidKeyCapsLock
Caps Lock (toggles the host's lock).
constexpr uint8_tkleidos::ble::profile::kHidModLCtrl
Left Ctrl.
constexpr uint8_tkleidos::ble::profile::kHidModLShift
Left Shift.
constexpr uint8_tkleidos::ble::profile::kHidModLAlt
Left Alt.
constexpr uint8_tkleidos::ble::profile::kHidModLGui
Left GUI (Win/Cmd).
constexpr uint8_tkleidos::ble::profile::kHidModRCtrl
Right Ctrl.
constexpr uint8_tkleidos::ble::profile::kHidModRShift
Right Shift.
constexpr uint8_tkleidos::ble::profile::kHidModRAlt
Right Alt (AltGr).
constexpr uint8_tkleidos::ble::profile::kHidModRGui
Right GUI (Win/Cmd).

Back to the top

Enumeration Type Documentation

BurstAbort #

enum class kleidos::ble::profile::BurstAbort : uint8_t

Why a guarded report was refused mid-burst.

Enumerator
None 

No report refused.

SendFailed 

The transport refused the report.

ProtocolChanged 

The host switched Protocol Mode since the burst began.

CapsLockOn 

The host turned Caps Lock on since the burst began.

BurstStart #

enum class kleidos::ble::profile::BurstStart : uint8_t

How a burst's start ended.

Enumerator
Ready 

Caps Lock is off: type.

CapsLockStuck 

Caps Lock is on and the host did not confirm it off: type nothing.

SendFailed 

The Caps Lock keystroke did not go out, or the link went while the burst waited for the host's answer.

KeystrokeOutcome #

enum class kleidos::ble::profile::KeystrokeOutcome : uint8_t

How a keystroke ended.

Enumerator
Sent 

The press and its release went out.

NotSent 

No key is held: the release followed a refused press.

NotReserved 

Both buffers could not be taken: nothing went out, try again later.

LinkDropped 

A release could not go out: the link was ended so the host lifts the key.

PacedStep #

enum class kleidos::ble::profile::PacedStep : uint8_t

How one try at a paced keystroke ended.

Enumerator
Sent 

The press and its release went out.

Wait 

Buffers are short: nothing went out; wait one step and try again.

GaveUp 

Buffers stayed short past the pace limit: nothing went out, no key is held.

Failed 

The press was refused or the link ended; no key is held.

PaceVerdict #

enum class kleidos::ble::profile::PaceVerdict : uint8_t

What a press does about the transmit buffers.

Enumerator
Proceed 

Enough are free: send.

Wait 

Too few: wait one step and look again.

GiveUp 

Too few for too long: the burst ends with nothing sent.

ProtocolModeWrite #

enum class kleidos::ble::profile::ProtocolModeWrite : uint8_t

How a host's write to Protocol Mode (0x2A4E) is answered.

Enumerator
Accepted 

One byte, Boot (0x00) or Report (0x01): the mode is stored.

BadLength 

Not exactly one byte: nothing is stored.

BadValue 

One byte, neither mode: nothing is stored.

ReportPath #

enum class kleidos::ble::profile::ReportPath : uint8_t

Which HID characteristic a keystroke notification must target.

Enumerator
Report 

Notify on the Report characteristic (0x2A4D) — report protocol.

BootKeyboard 

Notify on the Boot Keyboard Input Report characteristic (0x2A22).

TypingOutcome #

enum class kleidos::ble::profile::TypingOutcome : uint8_t

Outcome of a typing pass.

Enumerator
Ok 

Whole buffer typed successfully.

NotReady 

Sink reports !isReady (no BLE link).

SendFailed 

sendReport returned false mid-stream.

InvalidUtf8 

Malformed UTF-8 sequence.

UnsupportedCharacter 

Codepoint has no entry on the active layout.

Back to the top

Function Documentation

batteryNotifyAllowed() #

bool kleidos::ble::profile::batteryNotifyAllowed(bool subscribed, bool connected, bool encrypted, bool authenticated, bool bonded, uint8_t keySize)
nodiscardconstexpr

Whether the Battery Level may be notified on the host's link now.

Only the connected host, subscribed, on the link its pairing secured: the same bar a keystroke meets (security::reportLinkSecure: encrypted, MITM-authenticated, bonded, 16-byte key). A link short of it is sent nothing, whatever its subscription says.

Parameters
NameDirectionMeaning
subscribedinThe host enabled notifications on the CCCD.
connectedinA host's link is up.
encryptedinThe link is encrypted.
authenticatedinThe pairing was MITM-authenticated.
bondedinThe host is bonded.
keySizeinThe link's encryption key length, in bytes.
Return values
ValueMeaning
trueThe level may go out on this link.
falseSend nothing.

buildBootKeyboardReport() #

std::array< uint8_t, kBootKeyboardReportSize > kleidos::ble::profile::buildBootKeyboardReport(uint8_t modifiers, uint8_t keycode)
constexpr

Build the fixed 8-byte HID boot keyboard input report.

The boot keyboard report is [modifier][reserved=0][keycode][0×5]: a single key plus modifiers, with the reserved byte fixed at 0 and the remaining five key slots empty (we type one keycode at a time, matching the report-protocol path). A zero keycode with zero modifiers yields the all-zero "release" report.

Parameters
NameDirectionMeaning
modifiersinModifier bitmap (LeftCtrl … RightGUI).
keycodeinHID usage ID for the pressed key (0 = none / release).
Returns
The 8-byte boot keyboard report.

capsLockOn() #

bool kleidos::ble::profile::capsLockOn(uint8_t leds)
constexpr

Whether the host's LED byte says its Caps Lock is on.

Parameters
NameDirectionMeaning
ledsinThe LED byte the host last wrote on this link (0 when it wrote none: a HID keyboard's LEDs start off, and a host writes them when it has one on).
Return values
ValueMeaning
trueCaps Lock is on.

checkProtocolModeWrite() #

ProtocolModeWrite kleidos::ble::profile::checkProtocolModeWrite(const uint8_t * data, std::size_t len)
constexpr

Judge a host's write to Protocol Mode before anything is stored.

HOGP defines the value as one byte, 0x00 (Boot) or 0x01 (Report); anything else is refused and leaves the mode the host last chose (finding C32).

Parameters
NameDirectionMeaning
datainThe written bytes (may be null when len is 0).
leninHow many bytes the host wrote.
Returns
Whether the write is stored, and why not.

getLatin1Table() #

Latin1Table kleidos::ble::profile::getLatin1Table(KbLayout layout)

Returns the Latin-1 table for the given layout, or {nullptr, 0} if no extra coverage is provided (US default — falls back to printable-ASCII only).

Parameters
NameMeaning
layoutKeyboard layout identifier.
Returns
Lightweight view over the sorted Latin1Entry array for layout.

keystrokeStep() #

PacedStep kleidos::ble::profile::keystrokeStep(IReportLink & link, uint8_t modifiers, uint8_t keycode, uint32_t waitedMs, const KeystrokePacing & pacing = kKeystrokePacing)

One try at a keystroke that waits for its transmit buffers.

Looks at the free buffers (paceVerdict), then sends the keystroke; a press whose two buffers cannot be taken counts as a wait, never as a failure, since nothing went out and no key is held.

Parameters
NameDirectionMeaning
linkin, outThe notify path.
modifiersinModifier byte of the press.
keycodeinHID usage of the press.
waitedMsinHow long this keystroke has waited already.
pacinginTiming and bounds.
Returns
How the try ended.

ledWriteValid() #

bool kleidos::ble::profile::ledWriteValid(std::size_t len)
constexpr

Whether a write to a keyboard Output Report carries the LED byte.

The Output Report (report protocol) and the Boot Keyboard Output Report both carry one byte of LED bits; a write of any other length is refused and the LED state the host last wrote is kept.

Parameters
NameDirectionMeaning
leninHow many bytes the host wrote.
Return values
ValueMeaning
trueThe write is the LED byte.
falseIt is not; nothing is stored.

liftAllKeys() #

KeystrokeOutcome kleidos::ble::profile::liftAllKeys(IReportLink & link, const KeystrokePacing & pacing = kKeystrokePacing)

Send one all-zero report, as a release is sent: retried, else the link ends.

What a burst that ends early sends while its link is up, so the last report the host saw is all-zero whatever the burst was doing.

Parameters
NameDirectionMeaning
linkin, outThe notify path.
pacinginThe release bounds.
Returns
Sent, or LinkDropped.

liftOwed() #

bool kleidos::ble::profile::liftOwed(uint16_t targetAttr, uint16_t lastReportAttr)
constexpr

Whether a burst that ends early owes the host an all-zero report.

Every keystroke ends with its release queued, or with the link ended, on the characteristic it went to; so a key can be held only on a characteristic other than the one the last report went to, that is when the host switched Protocol Mode between a press and its release. Anything else is lifted already, and an all-zero report would only cost a buffer that may be short.

Parameters
NameDirectionMeaning
targetAttrinThe input report a keystroke would go to now.
lastReportAttrinThe input report the last report on this link went to; 0 when none went out.
Return values
ValueMeaning
trueSend the all-zero report on targetAttr.
falseNo key can be held.

paceVerdict() #

PaceVerdict kleidos::ble::profile::paceVerdict(uint16_t freeBuffers, uint32_t waitedMs, const KeystrokePacing & pacing = kKeystrokePacing)

Whether a press may go out now.

Parameters
NameDirectionMeaning
freeBuffersinThe stack's free transmit buffers.
waitedMsinHow long this press has waited already.
pacinginThe bounds.
Returns
The verdict.

runPacedKeystroke() #

template<typename Attempt, typename Wait>
PacedStep kleidos::ble::profile::runPacedKeystroke(Attempt && attempt, Wait && wait, const KeystrokePacing & pacing = kKeystrokePacing)

Try a keystroke until it goes out, fails, or gives up.

The adapter runs each try under the host lock and waits between them outside it, so a teardown never waits on a paced keystroke.

Template Parameters
NameMeaning
AttemptCallable (uint32_t waitedMs) -> PacedStep: one try.
WaitCallable (uint32_t ms): the wait between two tries.
Parameters
NameDirectionMeaning
attemptinOne try.
waitinThe wait.
pacinginTiming and bounds.
Returns
The step that ended it: never Wait.

selectReportPath() #

ReportPath kleidos::ble::profile::selectReportPath(uint8_t protocolMode)
constexpr

Decide which characteristic a keystroke should be notified on.

Report Protocol (the default and the common phone/PC case) routes to the Report characteristic; Boot Protocol routes to the Boot Keyboard Input Report characteristic. Any unexpected mode value is treated as Report Protocol so a malformed Protocol Mode write can never silently divert typing away from the default path.

Parameters
NameDirectionMeaning
protocolModeinThe last value the host wrote to Protocol Mode (0x2A4E).
Returns
The report path to use for this keystroke.

sendKey() #

bool kleidos::ble::profile::sendKey(IHidSink & sink, uint8_t modifiers, uint8_t scancode)

Send one key through sink, lifting every key if it fails.

For the keys a burst sends outside the typer (Tab between the fields, Return after them).

Parameters
NameDirectionMeaning
sinkin, outThe sink.
modifiersinModifier byte.
scancodeinHID usage.
Return values
ValueMeaning
trueIt went out.
falseIt did not; releaseAll() was sent.

sendKeystroke() #

KeystrokeOutcome kleidos::ble::profile::sendKeystroke(IReportLink & link, uint8_t modifiers, uint8_t keycode, const KeystrokePacing & pacing = kKeystrokePacing)

Send one keystroke and leave no key held.

Parameters
NameDirectionMeaning
linkin, outThe notify path.
modifiersinModifier byte of the press.
keycodeinHID usage of the press.
pacinginTiming and bounds.
Returns
How it ended.

servedBatteryLevel() #

uint8_t kleidos::ble::profile::servedBatteryLevel(bool known, int percent)
nodiscardconstexpr

The byte the Battery Level characteristic serves for a sample.

Parameters
NameDirectionMeaning
knowninThe battery sampler holds a good reading.
percentinThat reading's charge estimate, in percent.
Returns
percent clamped to 0..100, or kBatteryLevelUnknown when no good reading exists.

Back to the top

Variable Documentation

kBatteryLevelUnknown #

uint8_t kleidos::ble::profile::kBatteryLevelUnknown
inlineconstexpr

What the characteristic serves while no good reading exists: full, so a host raises no low-battery warning from a reading that was never taken.

kHidLedCapsLock #

uint8_t kleidos::ble::profile::kHidLedCapsLock
inlineconstexpr

Bit of the LED output byte (HID usage page 0x08, usage 0x02) the host sets while its Caps Lock is on.

kKeystrokePacing #

KeystrokePacing kleidos::ble::profile::kKeystrokePacing
inlineconstexpr

The device's keystroke: an 8 ms hold; a press waits for six free buffers (two notifications of two buffers each, and room for what the host sends meanwhile) for up to 5 s, past one connection event at the longest interval a central sets in practice (4 s); four tries at a release 20 ms apart.

Back to the top