turgu1 / bt-keyboard

Other
68 stars 8 forks source link

Crashing for certain keys #9

Open jthiem opened 1 week ago

jthiem commented 1 week ago

My Mac keyboard was crashing the ESP32 for certain keys (fn and eject). I traced the problem to the BTKeyboard::push_key(uint8_t * keys, uint8_t size) function, since for those keys the size parameter is 1; then 'max' becomes -1 (well, 255 since it's uint8) and the for loop messes up random parts of the heap.

A simple but lazy fix is to wrap the 5 lines from 'uint8_t...' to the end of the for loop in an if (size>2).

I'll make it into a pull request once I have thought about it a bit more and perhaps come up with something cleaner.

jthiem commented 1 week ago

Fix in pull request #8

turgu1 commented 4 days ago

Thanks @jthiem , Which Mac keyboard are-you using? The one from Apple or the one from Logitech?

jthiem commented 4 days ago

My pleasure! I'm using an Apple A1255 keyboard, the one with 3 AA batteries. It is very finicky to pair which makes it unpleasant to move between devices. My idea is basically create a USB dongle that is permanently paired to the keyboard.

In my experimenting I learned that the Classic BT HID protocol is designed to be just a "wire replacement" for USB, so the HID Keyboard protocol is the same as in USB. That is probably why the "fn" and "eject" key don't emit the same type of message (8 bytes) as all other keys as they are handled in a special way by the Mac.

The perma-keyboard-donlge could catch and handle those codes and modify its behaviour (eg rewrite codes to emit keys that are not present, like accents!)