0

Documentation on HIDKeyboard
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Documentation on HIDKeyboard

by willwade on Mon Dec 17, 2018 10:20 am

I have a feather M0 with BLE and writing a small python app that detects all keypress' on the host computer and over serial sends the keystrokes to the other. To do this I need to send the key_down and key_release of each key character. I can see from the example here: https://learn.adafruit.com/adafruit-feather-m0-bluefruit-le/hidkeyboard that there is:

Code: Select all | TOGGLE FULL SIZE
ble.print("AT+BleKeyboard=");
 ble.println(keys);


But how would I emulate a keydown and keyrelease of keys. For example lets say I wanted to send Ctrl+H ?

Any ideas? I cant find any docs and am a bit of a newbie to this

Thanks

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Re: Documentation on HIDKeyboard

by ktownsend on Mon Dec 17, 2018 11:26 am

AT+BLEKEYBOARDCODE is probably what you're looking for? See here: https://learn.adafruit.com/introducing- ... code-14-25

ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: Documentation on HIDKeyboard

by willwade on Mon Dec 17, 2018 12:13 pm

Great. Thanks. So anyone know of any nice C code to convert a ASCII code to the codeset seen in the below table? (i.e. https://learn.adafruit.com/introducing- ... odes-14-26)

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Re: Documentation on HIDKeyboard

by grantbt on Mon Dec 17, 2018 8:18 pm

You "just" have to make a big lookup table. It's all you can do. There probably are some out there if you search plausible names or terms.

If you can get by with mostly printable ASCII characters you can use your original technique. In that case each character is sent one at a time so you don't need to use the HID key report format (that's what the "CODE" implementation does). They have support for a couple of lower order codes (and if memory serves, Ctrl-H is backspace and they support via \b maybe?).

grantbt
 
Posts: 32
Joined: Mon Dec 10, 2018 7:45 pm

Re: Documentation on HIDKeyboard

by willwade on Mon Dec 17, 2018 9:15 pm

I'm developing a system for people using custom on-screen keyboards which can only be used on windows (by people with disabilities) to send all keystrokes over to another computer hence needing all the raw codes - not just chars.

I think I've largely done it with this lookup: https://gist.github.com/willwade/30895e766273f606f821568dadebcc1c#file-keyboardhook-py-L42

There is a problem though - I cant figure out what on earth this means in the adafruit documentation from here https://learn.adafruit.com/introducing-adafruit-ble-bluetooth-low-energy-friend/ble-services#modifier-values-14-25 (I'm a newbie to the world of C and HEX). I can see a ATCommand looks something like this:

AT+BLEKEYBOARDCODE=00-00-HEX-00-00-00-00

Where HEX is the hexcode for say a character. But what if I'm holding down shift and control. Which one of those 00's gets the modifier? Would it be something like

AT+BLEKEYBOARDCODE=0x010x02-00-HEX-00-00-00-00

I'm reading that its 7 Bits for each modifier but where in the string are they going? I'm clearly being dim here/showing my ignorance to proper computing!

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Re: Documentation on HIDKeyboard

by grantbt on Tue Dec 18, 2018 11:45 am

Everything about this command is based on the standard USB HID key report. The first byte is a combination of all the modifiers held down. Then the second byte is always zero then six more bytes indicating which keys (generally other than modifiers) are down.

So if shift and control are down then (assuming your bits are correct, I did not verify), then the first byte (hex) is 0x01 + 0x02 = 0x03

Now assume the user is doing shift-ctrl-A. The USB HID report looks like (all values hex): 03,00,04,00,00,00,00,00.
Assumes 0x04 is the code for the Aa key.

So first you must understand the USB HID tables and the report, as above. There are tons of examples of this online.

Now for the Bluefruit it is converted to an ASCII (human readable) command (most of the competitor modules do something similar):

"AT+BLEKEYBOARDCODE=03-00-04-00-00-00-00-00\n"

So first get up to speed on the HID packet, then it's not too hard to convert it to the Bluefruit ASCII command. To confuse matters and to speed things up (the result of all this is SLOW), the Bluefruit command has shorter variations.

However, I am having huge problems with Windows and this module and I don't know the solution yet, so I'm not saying your end product is do-able until I can guarantee that mine, which is something similar, is.

grantbt
 
Posts: 32
Joined: Mon Dec 10, 2018 7:45 pm

Re: Documentation on HIDKeyboard

by willwade on Tue Dec 18, 2018 10:04 pm

Interesting to see your response. Now I finally have something half working (my codes are all wrong..) I'm finding sending AT+BLEKEYBOARDCODE much slower compared with the easier method of AT+BleKeyboard but could easily be my code..

Interesting you are trying to do something similar. I'm really surprised more people haven't done this already.

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Re: Documentation on HIDKeyboard

by grantbt on Wed Dec 19, 2018 11:39 am

There might not be more people doing this because of bugs in the module. I have another thread going where today I hope to post a series of steps that ACTUALLY WORK*, at least on module firmware 0.8.0. Fingers crossed as I have to verify it still.

Yes AT+BLEKEYBOARDCODE is slow, the packets are huge mainly because they are human readable ASCII. And at 9600 baud it's not a speed demon either. Adding to that is the USB HID packet format that is limited (you can't put two of the same char in one packet as far as I know ... at least it's asking for trouble). And finally you have to wait for an acknowledgment from the module before proceeding. I don't recall at the moment but it's something like 50ms per packet. So best case scenario is 6 chars (and modifiers) every 50ms, but realistically it's half that since you more than likely are sending an "empty" packet indicating all character keys up. So I'd say about 60 chars a second, but in reality I think it's closer to 6.

* Bug = not able to reconnect after one end or the other powers off or goes away for whatever reason. You have to re-pair/bind/bond.
This is the thread I hope to post a solution to today:
viewtopic.php?f=53&t=145111

grantbt
 
Posts: 32
Joined: Mon Dec 10, 2018 7:45 pm

Re: Documentation on HIDKeyboard

by willwade on Thu Dec 20, 2018 8:07 am

Gah. Ok - thanks for that. I'll keep an eye on how that goes - but for now I think I'm going to change tack. The method of sending text characters is ok in speed - just about for my methods. It limits the usefulness of what I'm trying to build but hey.. its a good starting point till other things come along. Thanks.

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Re: Documentation on HIDKeyboard

by grantbt on Thu Dec 20, 2018 11:50 am

Near as I can tell that bug was reported a year ago. This is a show stopper for me. I also noticed that the Adafruit EZ Key has been discontinued and it makes you wonder. If anyone has made a successful bluefruit BLE HID that works with a Win PC I'd sure like to know about it and pick their brain. Maybe I'm just doing something in the wrong order. Who knows?

There are other issues too, such as not being able to identify who you are connected to. One of the quirks with Bluetooth is that after the initial pairing, it is the PC side that (re)establishes connection with the device. That means, on a new day, any previously paired PC that is in range could reconnect with the bluefruit. So you have to be super careful to always unpair and/or turn off bluetooth on previously paired PC's. I've been stung by the bluefruit reconnecting to something in a different room and wondering why I'm not transferring data.

Anyway I'm going to try Bluegiga (SI Labs) and/or Roving Networks (Microchip) in the meantime.

grantbt
 
Posts: 32
Joined: Mon Dec 10, 2018 7:45 pm

Re: Documentation on HIDKeyboard

by willwade on Fri Dec 21, 2018 9:18 pm

Interesting - but disappointing. I
Thank you though for getting this far - it’s saved me time. Seen this? https://haroldpimentel.wordpress.com/20 ... h-arduino/

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Re: Documentation on HIDKeyboard

by grantbt on Mon Dec 24, 2018 6:10 pm

Thanks, I haven't seen that one before. I just started playing with the RN42, so we'll see how that goes. If that fails to impress then the BT121 is next in line. :)

grantbt
 
Posts: 32
Joined: Mon Dec 10, 2018 7:45 pm

Re: Documentation on HIDKeyboard

by willwade on Thu Dec 27, 2018 8:06 am

Cool. Just incase this is useful as well check out https://github.com/juancgarcia/HID-Relay

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Re: Documentation on HIDKeyboard

by grantbt on Fri Dec 28, 2018 4:13 pm

So far the RN42 is solid on Android, will test on Windows. You can tell this unit has a hardware UART, 115kbaud, never a missed command so not needing any retries. Also, the command language is very succinct. The connect pin (LED) is fast so that you get instant notification of connected or not. This particular model is not BLE though.

grantbt
 
Posts: 32
Joined: Mon Dec 10, 2018 7:45 pm

Re: Documentation on HIDKeyboard

by willwade on Fri Dec 28, 2018 4:27 pm

Great. I've got one on its way. I look forward to trying it. BTLe Not that important. Thanks for sharing your progress. If you can would love to see any write up of how your project works.

willwade
 
Posts: 24
Joined: Sat Dec 01, 2018 3:04 pm

Please be positive and constructive with your questions and comments.