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.
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.
For a typing demonstration, use this sequence:
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.
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.
Created with the Personal Edition of HelpNDoc: Make Documentation a Breeze with a Help Authoring Tool