Keyboard.write() won't write backspace

Status
Not open for further replies.

alexandros

Well-known member
I'm trying to use a Teensy LC as a keyboard and a serial device at the same time, choosing USB Type -> Serial + Keyboard + Mouse + Joystick from the Tools menu, but uploading the code below will just print 'a' and won't send backspace:
Code:
#include "Keyboard.h";

void setup() {
  // put your setup code here, to run once:
  Keyboard.begin();

  Serial.begin(57600);

  delay(5000);
  Keyboard.write('a');
  delay(1000);
  Keyboard.write(8);
}

void loop() {

}

Isn't 8 ASCII backspace? How can I send it?
 
Does this work? :: Keyboard.write( KEY_BACKSPACE );
Didn't know of these macros. Well, it doesn't work either. I checked the library's header file and tried all the macros after reading your post, but they're not doing what they're supposed to (they're printing different characters than the ones they claim they print).
I tried some loops to see the output and finally found backspace to be the value 127 ! Any idea why this is happening?
 
I don't believe either of those ways will work... As Keyboard.write() takes a uint8_t value... There are versions that take 16 bits but they simply cast back to uint8_t.

And: #define KEY_BACKSPACE ( 42 | 0xF000 )

And the write method calls through to usb_keyboard_write, which has:
Code:
	if (c < 0x80) {
		// single byte encoded, 0x00 to 0x7F
		utf8_state = 0;
		usb_keyboard_write_unicode(c);
	} else if (c < 0xC0) {
So this calls through to write_unicode which calls unicode_to_keycode which starts off with:
Code:
static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint)
{
	// Unicode code points beyond U+FFFF are not supported
	// technically this input should probably be called UCS-2
	if (cpoint < 32) {
		if (cpoint == 10) return KEY_ENTER & KEYCODE_MASK;
		if (cpoint == 11) return KEY_TAB & KEYCODE_MASK;
		return 0;
	}
So the only supported lower values are 10 and 11...

What I am not sure of is if you might be able to get away with using something like:
Code:
Keyboard.press(KEY_BACKSPACE);
Keyboard.release(KEY_BACKSPACE);
Again unsure if that will work or not
 
What I am not sure of is if you might be able to get away with using something like:
Code:
Keyboard.press(KEY_BACKSPACE);
Keyboard.release(KEY_BACKSPACE);
Again unsure if that will work or not

Even though I found backspace to be 127, your suggestion worked as well, thanks! It's more intuitive this way.
 
I looked half as far as KurtE - the web page noted the KEY_BACKSPACE - which I found in the code.

Indeed they page was for key.press() not .write()

The code I saw has it defined as both 42 and 127 in two ways - not sure of the difference ...
 
Status
Not open for further replies.
Back
Top