Function Keys Question.

I’m using a Teensy 2 to assist a friend with a physical impairment.
The program i need is very easy; press a button and send 2 keystrokes at the same time (in this case SHIFT and CTRL).
I wrote a little program to output “This is a test” when the button is pressed, which works fine.
To test the function key required, I then modified the program to use the Keyboard.press function to send CTRL and “n” to open a new document in Notepad (as listed in the Arduino examples page). Unfortunately only ‘n’ gets output, and it does so repeatedly. The CTRL seems to be ignored (at least its not sending CTRL+n, which is what I want). Been banging my head against the wall for about 6 hours now. At this point I’m sure I’m missing something very easy, but I can’t find a similar example online that doesn’t site the Arduino examples page, which doesn’t work for me. ANYTHING that can point me in the general direction is greatly appreciated. (code below)

****CODE STARTS****
char ctrlKey = KEY_LEFT_CTRL;  

void setup(){
  pinMode(0, INPUT_PULLUP); // saves us from needing to use a resistor in the circuit
 Keyboard.begin();
 delay(5000);
}
// ------------------------------------------------------------

void loop() {
  while (digitalRead(0) == HIGH) {
    // do nothing until pin 0 goes low
    delay(500);
  }
  delay(1000);
  // Press CTRL+N to open a new document:
  Keyboard.press(ctrlKey);
  Keyboard.press('n');
  delay(100);
  Keyboard.releaseAll();
  // wait for new window to open:
  delay(1000);
}
****CODE ENDS****

Moderatore: code tags are the #button above the smileys.

Your code will send an 'n', every 1.5 seconds, whether you pressed any buttons, or not. Thats one of your problems.

The other part of the problem, I don't understand. Are you trying to read this keyboard, or write to one ?

You may be trying to write the keys to the keyboard, and then expect the keyboard to pass the keystrokes on to a computer. I don't quite see how this can work, the keyboard would be controlled either by the computer or the arduino, but not both.

It may be ok on a teensy, I don't know, but I'd also check whether pin 0 is a good pin to use.

The example I found looks a bit different

void loop() {
  while (digitalRead(2) == HIGH) {
    // do nothing until pin 2 goes low
    delay(500);
  }
  delay(1000);
  // new document:
  Keyboard.press(ctrlKey);
  Keyboard.press('n');
  delay(100);
  Keyboard.release(ctrlKey);
  Keyboard.release('n');
  // wait for new window to open:
  delay(1000);
}

Does it matter which key you release first ?

What I would do next, is figure out how to observe the actual character/bytes being received at the other end. It is likely that the ctrl character is there, but you just are not seeing it.

Thanks for the very quick reply. Sorry If I haven't explained myself well enough. Essentially the button is an external USB button which when pressed, needs to output 2 keys to a program. with Keyboard.print, I can send a text string to the program no problem ("Hello World"). Unfortunately, Keyboard.print doesn't seem to work with the function keys I need (CTRL+SHIFT) and can't send 2 keys simultaneously anyway, which led me to the Keyboard.press function. The external USB button is essentially acting like a very fast person typing 2 keys at once, which my friend is no longer dexterous enough to do. Hope this helps.

Finally figured it out after some really deep googling.
The easiest was to achieve what I was trying to do was to combine the Bounce feature to test for changes in the button state, and using the Keyboard.set_modifier() function to send CTRL and SHIFT at the same time:
Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_SHIFT);
For those interested, code is below and thanks for the help.
dav./

*** Start CODE ***

#include <Bounce.h>

// Create Bounce objects for the button
Bounce button0 = Bounce(0, 10);

void setup() {
pinMode(0, INPUT_PULLUP);
}

void loop() {
button0.update(); // Update the button
//Check falling edge for the button
if (button0.fallingEdge()) {
// press SHIFT while still holding CTRL
Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_SHIFT);
Keyboard.send_now();
}
//Check rising edge for the button
if (button0.risingEdge()) {
// If button is released, release all the keys at the same instant
Keyboard.set_modifier(0);
Keyboard.set_key1(0);
Keyboard.send_now();
}
}
*** END CODE ***