Sending " CTRL ; "

Hello everyone,

I'm playing with the boot HID keyboard code from Nico Hood. I've my Arduino Mega 2560 working now as a normal keyboard. When I sent text to the 16U2 it appears directly into notepad or where your cursor might be...

But now I want to play with Excel and want to enter a date. The Excel keyboard shortcut for this is " ctrl ; ".

As far as I know the control key subtracts 64 from the decimal ascii value. But the decimal value of the " ; " is 59 and therefore not in the ascii control character list... (This starts at @)

So can anyone explain to me how to sent the " ctrl ; " or other -non standard- control characters? I would expect an other range of hex codes...

In the time of my vintage Digital PDP8/e it was normal to have this smalls ascii set. But I think there are more possibilities these days :grinning:

Thanks, Roland

Instead of hitting CTRL, is there some library you could load in your program that finds the current date? Or even cooler yet, could you have your Arduino Mega interface with the Python DateTime module? Perhaps the CTRL shortcut isn't the best method.

I recall that PC keyboards send a code for every key combination (not ASCII). Does the library not give you access to those?

I recall that PC keyboards send a code for every key combination (not ASCII). Does the library not give you access to those?

Exactly what library are you using?

the_roboticist: Instead of hitting CTRL, is there some library you could load in your program that finds the current date? Or even cooler yet, could you have your Arduino Mega interface with the Python DateTime module? Perhaps the CTRL shortcut isn't the best method.

The arduino in this case doesn't know any date from itself. But the computer does. Just hit " ctrl ; " and there is your date in Excel. I don't have additional hardware. The Arduino mega (16u2 cpu) just acts like a USB HID keyboard with hoodloader2 as bootloader. Really nice, you can switch the mode from the 16u2 between HID device and serial programmer for the 2560. From the 2560 I just can sent characters over the serial line to the 16u2 which sends it as a keystroke on a keyboard. (recognized as standard keyboard by the PC)

the_roboticist: Exactly what library are you using?

https://github.com/NicoHood/HoodLoader2 (switchable bootloader for the 16u2) https://github.com/NicoHood/HID (HID keyboard library for the 16u2)

aarg: I recall that PC keyboards send a code for every key combination (not ASCII). Does the library not give you access to those?

I can only sent data by Serial.write to the 16u2. The scancodes are created in the 16U2.

In the meanwhile I found that it seems Serial.write('\x59'); is a valid value. Don't know if that works. I'm not at home now so I can't test it now...

Random stab in the dark here, but have you tried "^:" or is the carat figure just interpreted as another ASCII character?

From what I see on the site you posted, it has raw keycode support.

the_roboticist: Random stab in the dark here, but have you tried "^:" or is the carat figure just interpreted as another ASCII character?

Yes that was my first try :) But it seems Serial.write('\x59'); is valid as a extended one. Not sure, can't test it here right now.

aarg: From what I see on the site you posted, it has raw keycode support.

Hmmm maybe my misunderstanding. I thought that this was just a raw hid device and not really recognized as a keyboard. I have to figure that out.

Regards, Roland

a keyboard IS a HID device...

The actual USB keyboard protocol sends messages for every keypress and release, so a ctrl-; will look like CTRL-down, SEMICOL-down, SEMICOL-up, CTRL-up. Alas, the key values sent in the messages bare no resemblance to ascii - one of the purposes of the keyboard libraries is to let you specify ascii characters and convert them to the keycodes for you; to send a code that is NOT ascii (like ctrl-;), you'll have to bypass that conversion logic to a more primitive level...

It looks like you should be able to use a sequence of keyboard.press()/etc calls instead of keyboard.write: https://www.arduino.cc/en/Reference/KeyboardModifiers (I'm not quite sure of the interdependencies of the 16u2 code, the Hood HID library, and the Arduino library documentation.

westfw: It looks like you should be able to use a sequence of keyboard.press()/etc calls instead of keyboard.write: https://www.arduino.cc/en/Reference/KeyboardModifiers (I'm not quite sure of the interdependencies of the 16u2 code, the Hood HID library, and the Arduino library documentation.

Thanks, I've made a little nasty workaround. If the 16U2 catches a DEL acsii character from the 2560 it will sent the "ctrl ;" to Excel. This works quite well. It would be nicer if I could just sent this directly from the 2560. I have to figure out if there is a possibility for that.

Main loop for the 16U2

void loop() { char c = Serial1.read();

if(c==127) // alternative function for Ascii DEL character { BootKeyboard.press(KEY_LEFT_CTRL); // Excel date code BootKeyboard.write(';'); // Excel date code BootKeyboard.release(KEY_LEFT_CTRL); // Excel date code }

else { BootKeyboard.write(c); } }

Regards, Roland

Serial.write just sends bytes. It does not know anything about ASCII, or otherwise.

You can change your Serial.write() calls to send the keycodes, instead of trying to send "ASCII". Then, in the 16U2, pass them on as keycodes, or interpret them if you have to before passing them on.