Send keyboard keys

USB Library ›› Device ›› Use HID ››
… visits
Parent Previous Next

First try one key in an empty text editor. Every key press needs a matching release; start with that simple test before typing a whole line.

Understand the keyboard report

Select Boot keyboard. Its report describes the keys currently held down:

Byte

Contents

0

Modifier bits 0–7: left Ctrl, Shift, Alt, GUI; right Ctrl, Shift, Alt, GUI

1

Reserved; zero

2–7

Up to six key usage codes; zero means unused

These numbers identify keys, not text characters or ASCII codes. Key usage 4 is A, 40 is Enter and 44 is Space. Left Shift is bit 1 of byte 0, which gives a byte value of 2. The keyboard layout selected on the PC decides which character appears.


After the generated keyboard startup, call this procedure from your main program to send uppercase A on a matching keyboard layout. Check USB_Result after calling it; that variable contains the result of the last USB operation.

Proc KeyboardSendA()

    Clear TXReport

    TXReport[0] = 2                  ' Left Shift

    TXReport[2] = 4                  ' A key

    USB_WriteWait(8, 20)

    If USB_Result <> USB_OK Then ExitProc

    USB_Flush(20)

    If USB_Result <> USB_OK Then ExitProc


    Clear TXReport                  ' Release every key and modifier

    USB_WriteWait(8, 20)

    If USB_Result <> USB_OK Then ExitProc

    USB_Flush(20)

EndProc

Always send the release. Check the result after calling the procedure. If the press reached the PC but the release failed, retry the all-zero release while the connection is usable. The library does not release a pressed key automatically; the PC may keep repeating it.

USB_WriteWait() waits for room to put a report in the send queue. With ping-pong enabled, the queue can hold both a press and a release before the PC has received either one.


USB_Flush() waits until the PC has collected queued input reports. Call it before starting a delay that should hold a key down. These calls tell you about USB delivery, not whether a particular PC program has used the key.

Try a complete typing demonstration

For a typing demonstration, use this sequence:

  1. Wait until configured and not suspended, then wait 10 seconds so the user can focus a text editor.
  2. Convert each character of Positron8 HID Keyboard to a key/modifier pair.
  3. Send the press, flush, wait about 10 ms, send the release, flush, then wait about 90 ms. This gives approximately 100 ms between keystrokes; USB and program overhead add to it.
  4. Send Enter once at the end. If the source text ends with CR and LF, translate CR into Enter and skip LF to avoid two blank-line actions.
  5. Wait 2 seconds after the completed line, then repeat. Restart the initial wait after a new connection.

Use the wait pattern that keeps USB running. A long plain DelayMS stops USB servicing in polling mode. In interrupt mode, USB servicing continues, but your wait must still notice a disconnect or suspend. A helper named KeyboardWait can be useful in your own program; it is not a required library command.

Read keyboard LED states

The PC may send LED states. After USB_Read() returns USB_OK with a nonzero length, RXReport[0] bits 0–4 mean Num Lock, Caps Lock, Scroll Lock, Compose and Kana. Use those bits to update your LEDs if wanted; they are not incoming text.

See also

Created with the Personal Edition of HelpNDoc: Make Documentation a Breeze with a Help Authoring Tool