I don't understand why it is so different for Leonardo, why the Mega firmware couldn't act as transparently as Leonardo's, to allow both HID functions and loading programs from IDE simultaneously. Since this is terra incognita for me, I don't want to screw things up - to brick the board or lose the ability to flash new programs using the standard Arduino IDE (as one guy warns here: arduino mega 2560 as HID USB keybaord - Programming Questions - Arduino Forum). Keyboard.begin() starts emulating a keyboard connected to a computer. So, I read that I need to rewrite the board's firmware to make it appear to PC as a keyboard. The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords. My code that uses Keyboard.begin() just doesn't compile for Mega 2560.
I found this lib but it is only for Leonardo, Micro, or Due. IC an cheap USB IC which support USB Serial, USB HID and so on, what ever you need in the moment. last but not least, you can use instead CP. I'd like to use the onboard USB connection for this, so that I can avoid buying any additional hardware like USB shield. you could connect to esp32 pins USB HID controller. Solder the socket to an Arduino proto shield.
Use a multimeter to find the pinout of the socket. Desolder the socket that the PS/2 cable is connected to. I have an Arduino Mega 2560 board for this, as it has enough inputs to handle the ~30 keys. Numeric keypad Escape and function keys The library can simulate function keys up to F24. The following is an easy way to get started: Take a PS/2 mouse apart without cutting any cables. I have a project where I have to create a custom keyboard with limited set of keys.