HID Keyboard

260 views
Skip to first unread message

Carlos Fonseca

unread,
Mar 16, 2023, 3:46:07 AM3/16/23
to btstack-dev
Sorry for bothering, but I have been stuck for a few days trying to make a macropad using a raspberry pi pico w.

What I am trying to do is send a key combination when a button is pressed, but I am stuck trying to send a simple char on command, is there any event I need to call to send a specific key on a button press?

Sorry if this question is out of place, and if so could you guide me to the right place?

Thanks in advance

Matthias Ringwald

unread,
Mar 16, 2023, 4:59:45 AM3/16/23
to btsta...@googlegroups.com
Hi Carlos

With most computers and smartphones, you can either use HID over Classic or HID over GATT (LE). Please have a look at hid_keyboard_demo (classic) or hog_keyboard_demo (LE).
Both have conditional code on HAVE_BTSTACK_STDIN. If it's defined, it allows to send keys for input received over the UART. If it is not defined, both demos will send an fixed test after getting connected. So, please give it a try.

That should get you pretty close to your use case. The example also shows, how to queue key events, as most BTstack functions are non-blocking.

Cheers
Matthias
> --
> You received this message because you are subscribed to the Google Groups "btstack-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to btstack-dev...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/5dbc153f-6415-4efe-8eed-e7ed32f79e65n%40googlegroups.com.

MatSOBDev

unread,
Mar 19, 2023, 1:02:45 PM3/19/23
to btstack-dev
Hi to everyone.

Carlos, 'hog_keyboard_demo' has 'send_report' function, that is an essence of 'typing' as BLE keyboard. First byte is a modifier key, second always 0x00 (in standard 'mode' I guess), then next 6 bytes are keycodes (there to find as well as modifiers keys: shift, ctrl... that combination of those is logical OR of ones used, eg. 0x01 | 0x02 for left control and shift). Multiple keycodes of those 6 available for key combinations (with or without modifiers). First you call 'send_repor' with your key(s), then with zeroes to release pressed key(s).

Carlos Fonseca

unread,
Mar 21, 2023, 6:24:57 PM3/21/23
to btstack-dev
Thank you, I managed to make it send the keys I wanted using the 'send_report' function.

Now I am just wondering, I wanted to receive a message from the computer in the hid keyboard on the pico, how could I do it? I cannot find any information about it.

Matthias Ringwald

unread,
Mar 22, 2023, 9:11:10 AM3/22/23
to btsta...@googlegroups.com
Hi Carlos

HID allows to also send data to a keyboard. However you would need to figure out how to tell the operating system (assuming you connect your pico w to a mac, windows, linux system) to send it.

Alternatively, you can setup a classic L2CAP or RFCOMM service to which you can connect from the desktop. In that case, you could as well send the HID data over this.
For LE, you can setup an additional GATT Service, to which you can connect from the desktop. It all depends on what you want to achieve :)

Best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/e6b1a69d-6f06-4931-b86a-49aa665cbda3n%40googlegroups.com.


Carlos Fonseca

unread,
Mar 22, 2023, 10:51:32 AM3/22/23
to btstack-dev
Hi Matthias, 

Are there any examples for that? what I wanted to do was a simple program that would send the mappings to use in the keyboard, what option to send information to the keyboard would you recommend? 

Thank you,
Carlos

Matthias Ringwald

unread,
Mar 22, 2023, 11:05:33 AM3/22/23
to btsta...@googlegroups.com
Hi Carlos

On which platform would you like to write the keyboard configuration mapping?

Best
Matthias

Carlos Fonseca

unread,
Mar 22, 2023, 11:19:13 AM3/22/23
to btstack-dev
Hi Matthias,

If possible from linux to the keyboard, but if it is easier it could also be from windows to the keyboard.

Thank you,
Carlos

MatSOBDev

unread,
Mar 22, 2023, 4:26:10 PM3/22/23
to btstack-dev
You might mix hog_keyboard_demo with nordic_spp_le_counter (with common *.gatt file importing both sets of services at one name).

Matthias Ringwald

unread,
Mar 24, 2023, 10:10:34 AM3/24/23
to btsta...@googlegroups.com
HI Carlos

There are different options to communicate with the Pico.

If you're on linux and have Bluetooth/blueZ setup properly, you could use bluez to create an RFCOMM connection to the pico. For that, you'd need to provide an RFCOMM service on the pico, see e.g. spp_counter_example. Please use your favorite search engine to find a tutorial on how to use bluez to create an RFCOMM connection - it's quite similar to creating an TCP/IP socket.

Alternatively, you could plug an USB Bluetooth dongle into your linux system and create a command line tool similar to the existing BTstack examples. Here, spp_streamer_client might be a good start: it does an inquiry and connects to the first device with CoD 1234. With the HID keyboard, either providing the Bluetooth address as a parameter or look for a device with a fixed name would work, too. See port/libusb for this.

If you're adventurous, you could also try the pan_http_lwip_demo, which implements a PAN service with BTstack and then allows to use Bluetooth tethering to connect to it, then use a web browser. You could run a web app on the Pico...

Good luck
Matthias

> On 22 Mar 2023, at 16:19, Carlos Fonseca <cmce...@gmail.com> wrote:
>
> Hi Matthias,
>
> If possible from linux to the keyboard, but if it is easier it could also be from windows to the keyboard.
>
> Thank you,
> Carlos
>
> On Wednesday, March 22, 2023 at 3:05:33 PM UTC Matthias Ringwald wrote:
> Hi Carlos
>
> On which platform would you like to write the keyboard configuration mapping?
>
> Best
> Matthias
>
> > On 22 Mar 2023, at 15:51, Carlos Fonseca <cmce...@gmail.com> wrote:
> >
> > Hi Matthias,
> >
> > Are there any examples for that? what I wanted to do was a simple program that would send the mappings to use in the keyboard, what option to send information to the keyboard would you recommend?
> >
> > Thank you,
> > Carlos
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "btstack-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to btstack-dev...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/85c828dd-463b-4266-9b19-60955f3c82e2n%40googlegroups.com.

Carlos Fonseca

unread,
Mar 24, 2023, 10:48:04 AM3/24/23
to btstack-dev
Hi MatSOBDev and Matthias,

I was looking into the option that  MatSOBDev sent with the gatt services for that how would I send the information to the keyboard? 
Is it like a serial connection, or would I need to use something like the port/libusb? 

Many Thanks,
Carlos Fonseca

MatSOBDev

unread,
Mar 26, 2023, 2:52:53 AM3/26/23
to btstack-dev
It is full BT LE and it would be serial like. Classic serial will make COM port in system, accessible like standard wired serial port. For Windows and or Linux it might require special terminal app, or just custom app all together using GATT comms to talk to Pico W. For Android there is Serial Bluetooth Terminal app supporting both Classic and BLE. Dual - Classic for rfcomm and BLE for keyboard might be an easier approach with standard terminal app like Putty, CoolTerm etc but I guess it might be more power hungry ir Classic and BLE will be up at the same time.

Carlos Fonseca

unread,
Mar 28, 2023, 10:10:46 AM3/28/23
to btstack-dev
I can run the hog_keyboard_demo and the nordic_spp_le_counter in separate but I cannot (and also am not super sure on how to ) join them both, how can I mix gatt services? 

Matthias Ringwald

unread,
Mar 29, 2023, 2:43:31 PM3/29/23
to btsta...@googlegroups.com
Hi Carlos

From a high level view, you can just combine the corresponding functions and the GATT file.
If you don't need Nordic's SPP emulation, I would suggest to use gatt_counter instead as it's just about sending (sending notifications) and receiving (write with/without response).

It would be good to first try to understand the gatt_counter demo. It's reasonably documented (we hope), Then have a look at similar parts in the hog_keyboard_demo.

Going back to the merge:
- I'd start with the hog_keyboard_demo and try to add the demo GATT service from gatt_counter.
- as the hog_keyboard_demo doesn't register a read/write callback in att_server_init, you can just use the line from the gatt counter and bring along the att_read_callback and att_write_callback.
that should cover most of it.

I would recommend to try this on a desktop computer where you also use a debugger. If not, you can still add logging to figure out what's called.

best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/73bacf1b-facb-4c81-8b35-aa34846d7af9n%40googlegroups.com.

MatSOBDev

unread,
Apr 4, 2023, 5:25:17 AM4/4/23
to btstack-dev
Just an idea, but could be possible to use HID input capabilities to transfer data into microcontroller. Just app or script that toggles NumLock, CapsLock, ScrollLock to send data. NumLock for the clock, rest for the data.

Carlos Fonseca

unread,
Apr 4, 2023, 6:35:30 AM4/4/23
to btstack-dev
And how would you do that? I have been trying to do that but have not been able to do so yet

MatSOBDev

unread,
Apr 4, 2023, 8:04:34 AM4/4/23
to btstack-dev
To be honest never wrapped my head around this, but I guess there is a special service/characteristic for input. I would start by reading that input data from given characteristic.

MatSOBDev

unread,
Apr 4, 2023, 8:36:34 AM4/4/23
to btstack-dev
'hids.gatt' file inside BTstack library dir.
Reply all
Reply to author
Forward
0 new messages