Keyboard.print() has trouble with combined keystrokes (includes fix)

rastersoft

New member
These days I've been doing some tests in a Teensy 2.0 with Keyboard.print() to add macro support to my keyboard, and found that characters that require pressing several modifiers usually aren't received correctly by my computer. Specifically, the '&' character is more than half of the times interpreted as a '6' (I use the spanish layout, where the ampersand requires Shift+6). The problem seems to be that both keystrokes are sent in the same USB packet.

I tried doing this change in usb_api.c and now it works like a charm:

Code:
// Step #4: do each keystroke
//
void usb_keyboard_class::write_key(KEYCODE_TYPE keycode)
{
	keyboard_report_data[0] = keycode_to_modifier(keycode);
	keyboard_report_data[1] = 0;
	keyboard_report_data[2] = 0;
	keyboard_report_data[3] = 0;
	keyboard_report_data[4] = 0;
	keyboard_report_data[5] = 0;
	keyboard_report_data[6] = 0;
	keyboard_report_data[7] = 0;
	if (keyboard_report_data[0] != 0)
		send_now();
	keyboard_report_data[2] = keycode_to_key(keycode);
	send_now();
	keyboard_report_data[2] = 0;
	send_now();
	if (keyboard_report_data[0] != 0) {
		keyboard_report_data[0] = 0;
		send_now();
	}
}

The idea is to send the modifier keys first as a single keystroke, and only after that send the "normal" keycode as another keystroke.
 
Back
Top