PDA

View Full Version : T3: generating special symbols with USB keyboard



el_supremo
01-18-2013, 03:51 PM
On a US keyboard the copyright symbol can be typed by holding down an ALT key, typing 0169 on the numeric keypad and then releasing the ALT key. I've been playing with the USB keyboard code and can't figure out how to get this to work. The ALT, SHIFT etc. keys are only modifiers of a typed key and there doesn't seem to be a way to say "hold down the ALT key".
How can the copyright and other special symbols be typed with the Teensy 3 USB keyboard?

Thanks
Pete

el_supremo
01-18-2013, 06:35 PM
Here's the code for my most recent attempt to generate the copyright symbol:

Pete



// Play with Teensy 3 USB keyboard
// Type AbC on first line (works)
// Type the copyright symbol on the second line (fails)

int led = 13;

int keypress(uint8_t key, uint8_t modifier)
{
int r;
keyboard_modifier_keys = modifier;
keyboard_keys[0] = key;
keyboard_keys[1] = 0;
keyboard_keys[2] = 0;
keyboard_keys[3] = 0;
keyboard_keys[4] = 0;
keyboard_keys[5] = 0;
return usb_keyboard_send();
}


int key_release_all(void)
{
delay(1);
keyboard_modifier_keys = 0;
keyboard_keys[0] = 0;
keyboard_keys[1] = 0;
keyboard_keys[2] = 0;
keyboard_keys[3] = 0;
keyboard_keys[4] = 0;
keyboard_keys[5] = 0;
return usb_keyboard_send();
}

// the setup routine runs once when you press reset:
void setup() {
pinMode(led, OUTPUT);

// flash led for 10 seconds
for (int i=0; i < 10; i++) {
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}

usb_keyboard_press(KEY_A,MODIFIERKEY_SHIFT);
usb_keyboard_press(KEY_B,0);
usb_keyboard_press(KEY_C,MODIFIERKEY_SHIFT);
usb_keyboard_press(KEY_ENTER,0);

// Use my modified key press code to try to generate
// copyright symbol
// light up the LED if this first one fails
if(keypress(0,MODIFIERKEY_LEFT_ALT) < 0)digitalWrite(led, HIGH);
keypress(KEYPAD_0,MODIFIERKEY_LEFT_ALT);
keypress(KEYPAD_1,MODIFIERKEY_LEFT_ALT);
keypress(KEYPAD_6,MODIFIERKEY_LEFT_ALT);
keypress(KEYPAD_9,MODIFIERKEY_LEFT_ALT);
key_release_all();
keypress(KEY_ENTER,0);
key_release_all();

}

// the loop routine runs over and over again forever:
void loop() {
}

el_supremo
01-18-2013, 10:12 PM
Aha. Figured it out.

keyboard_modifier_keys = MODIFIERKEY_LEFT_ALT;
keyboard_keys[0] = KEYPAD_0;
keyboard_keys[1] = KEYPAD_1;
keyboard_keys[2] = KEYPAD_6;
keyboard_keys[3] = KEYPAD_9;
keyboard_keys[4] = 0;
keyboard_keys[5] = 0;
usb_keyboard_send();
usb_keyboard_release_all();

Pete

Nantonos
01-19-2013, 10:33 PM
Thanks for documenting your workaround. (I would not have expected it to work, since it seems to be saying that all four numeral keys are pressed simultaneously).

An alternative approach would be to use the US International keyboard layout (http://en.wikipedia.org/wiki/Keyboard_layout#US-International) which, besides the more common accented Latin letters, also gives you (Right Alt, c).

165

el_supremo
01-19-2013, 11:24 PM
it seems to be saying that all four numeral keys are pressed simultaneously
This seems to be its way of allowing you to simulate multiple keypresses while one or more modifier keys (shift, alt ,etc) are also held down. In which case you can simulate a sequence of up to six keys in a row!
I haven't found any other way of doing it.

Pete