need help about start/stop pc program via arduino leonardo

Hi all

I’m a newbie .I have arduino leonardo project use 2 buttons. 1st button press run a program on pc. 2nd button press stop playing file in program then open new file to play. but when i press button 2. the program stop playing then show open file window but not fill the file name as i want to do. please help

case 2:
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('a');
      delay(200);
      Keyboard.releaseAll();
 break;
case 3:
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // control
      Keyboard.press('s'); //stop
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('f'); // file
      Keyboard.press('o'); // open
      Keyboard.println("2.lpgm");
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); //control
      Keyboard.press('p'); //play
      delay(200);
      Keyboard.releaseAll();
 break;

It's strange if i use 1 button more then re-coding as like as below. it's work ok. but i dont want to use 1 more button just to stop and show open dialog file. i just want only 1 button to do that.

 case 2:
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('a');
      delay(200);
      Keyboard.releaseAll();
 break;
 case 3:
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('s'); // Stop
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('f'); // File menu
      Keyboard.press('o'); // Open file
      delay(200);
      Keyboard.releaseAll();
 break;
 case 4:
      Keyboard.println("2.lpgm");
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('p'); // Play
      delay(100);
      Keyboard.releaseAll();
 break;

You probably need a delay after the ALT, f, o to allow the file requester time to open and the cursor focus to be set to the filename textbox before trying to inject the filename.

Riva:
You probably need a delay after the ALT, f, o to allow the file requester time to open and the cursor focus to be set to the filename textbox before trying to inject the filename.

Try adding delay(200) after ALT+F+O as you said but didn't work.

godofwar2020:
Try adding delay(200) after ALT+F+O as you said but didn't work.

Try a longer delay, One fifth of a second is probably not long enough. Maybe start with a much longer delay (say 2000) and if it works then reduce it a bit at a time.

Riva:
Try a longer delay, One fifth of a second is probably not long enough. Maybe start with a much longer delay (say 2000) and if it works then reduce it a bit at a time.

Didn't work too with delay 2000 or more. the program just show the open file dialog then stay here. nothing occur then after. it's weird.

When you perform an ALT-f-o and the file requester comes up, does the filename text input box have focus?

As a test add another long delay after the filename inject to confirm the filename is being injected where you expect.

If it works with using 3 buttons and the same injected keycodes then it really does sound like a timing issue that you just need to add delays until it works okay.

Riva:
When you perform an ALT-f-o and the file requester comes up, does the filename text input box have focus?

As a test add another long delay after the filename inject to confirm the filename is being injected where you expect.

If it works with using 3 buttons and the same injected keycodes then it really does sound like a timing issue that you just need to add delays until it works okay.

if i add long delay enough the text input had blink cursor look as the pic i posted above .it blink then stop. i also add another delay after file name injected but nothing too.

I just looked in more detail at your snippets and there is a big difference between your 2 button and 3 button examples. In the two button code you don't release the ALT-f-o keys before injecting the filename but the 3 button code you do.

Does this work?

case 3:
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('s'); // Stop
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('f'); // File menu
      Keyboard.press('o'); // Open file
      delay(200);
      Keyboard.releaseAll();
      delay(1000);
      Keyboard.println("2.lpgm");
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('p'); // Play
      delay(100);
      Keyboard.releaseAll();
 break;

if not then please post your latest code.

Riva:
I just looked in more detail at your snippets and there is a big difference between your 2 button and 3 button examples. In the two button code you don't release the ALT-f-o keys before injecting the filename but the 3 button code you do.

Does this work?

case 3:

Keyboard.press(KEY_LEFT_ALT);
     Keyboard.press('c'); // Control
     Keyboard.press('s'); // Stop
     Keyboard.press(KEY_LEFT_ALT);
     Keyboard.press('f'); // File menu
     Keyboard.press('o'); // Open file
     delay(200);
     Keyboard.releaseAll();
     delay(1000);
     Keyboard.println("2.lpgm");
     Keyboard.press(KEY_LEFT_ALT);
     Keyboard.press('c'); // Control
     Keyboard.press('p'); // Play
     delay(100);
     Keyboard.releaseAll();
break;



if not then please post your latest code.

Thank you very much. Here is my code. It's working now. it's is very stricky about delay timing . if too short or too long it didn't work.

case 3:
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('s'); // Stop
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('f'); // File menu
      Keyboard.press('o'); // Open file
      delay(200);
      Keyboard.releaseAll();
      delay(200);
      Keyboard.println("2.lpgm");
      delay(200);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('p'); // Play
      delay(100);
      Keyboard.releaseAll();
 break;

As I said I think the problem is timing and you probably need more delays. A good starting point is a delay after the file name injection to allow time for the requester to close and possibly the project to load.
Is the file name your injecting, in the default folder the program is opening the file requester to?

Riva:
As I said I think the problem is timing and you probably need more delays. A good starting point is a delay after the file name injection to allow time for the requester to close and possibly the project to load.
Is the file name your injecting, in the default folder the program is opening the file requester to?

Yes, all files storage in same folder with the main program. This i my project for this Christmas at my church.

How can I disable the button click after 1 minute for example after first click ? then it auto enable again ? thank you very much

godofwar2020:
How can I disable the button click after 1 minute for example after first click ? then it auto enable again ? thank you very much

The sample code below will lock out a button for 60 seconds after it is pressed.

uint32_t releaseTime = 0; // millis value of button release time

void setup()
{
  Serial.begin(115200);
  pinMode(KEY_BUILTIN, INPUT_PULLUP);
}

void loop()
{
  if(digitalRead(KEY_BUILTIN) == LOW)
  {
    Serial.println("Pressed");
    if(millis() > releaseTime)
    {
      releaseTime = millis() + 60000; // Lock out for 60 seconds
      Serial.println("Locking");
    }
    else
    {
      Serial.println("Locked");
    }
    while(digitalRead(KEY_BUILTIN) == LOW){}; // Wait for button release
  }
  delay(100);
}

What is digitalRead(KEY_BUILTIN) ? My project had 4 button (2,3,4,5). Do i have to make a loop to read 4 key status ? How can I add these code to my main code project? sorry for the question.

godofwar2020:
What is digitalRead(KEY_BUILTIN) ? My project had 4 button (2,3,4,5). Do i have to make a loop to read 4 key status ? How can I add these code to my main code project? sorry for the question.

I don't have a spare Leonardo to hand so tested the code on a Heltec ESP32 board I have laying at my desk. It has a built-in user button that is defined as KEY_BUILTIN.
You will need to post your full sketch for someone to determine how to add the lockout code. You will also need to tell us if the lockout effects all the buttons or just some.

Here is full source code. I want it lockout effects for all the buttons.

#include <Keyboard.h>

int keys[] = {2,3,4,5};
//uint32_t releaseTime = 0; // millis value of button release time
void setup()
{
  // Serial.begin(115200);
  //pinMode(pin, INPUT_PULLUP);
  // put your setup code here, to run once:
 Keyboard.begin(); // setup keyboard
 for (int i = 2; i < 6; ++i)
 {
  // initilize pins
  pinMode(i, INPUT_PULLUP);
 }
}

void loop()
{
 /*  if(digitalRead(i) == LOW)
  {
    Serial.println("Pressed");
    if(millis() > releaseTime)
    {
      releaseTime = millis() + 60000; // Lock out for 60 seconds
      Serial.println("Locking");
    }
    else
    {
      Serial.println("Locked");
    }
    while(digitalRead(i) == LOW){}; // Wait for button release
  }
  delay(100);*/
  // put your main code here, to run repeatedly:
 for (int i = 2; i < 6; ++i)
 {
  // check buttons
  if(readButton(i))
  {
    doAction(i); 
  }
 }
}

boolean readButton(int pin)
{
 // check and debounce buttons
 if (digitalRead(pin) == HIGH)
 {
  delay(20);
  if (digitalRead(pin) == HIGH)
  {
    return true;
  }
 }
 return false;
}

void doAction(int pin)
{
 // perform tasks
 switch (pin)
 {
 case 2:
      // Sending a combo key CTRL+ALT+A to run program gate 1
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('a');
      delay(200);
      Keyboard.releaseAll();
      //keypadDisabled
      //keypadEnabled
 break;
case 3:
      // Play gate 2 program
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('s'); // Stop
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('f'); // File menu
      Keyboard.press('o'); // Open file
      delay(200);
      Keyboard.releaseAll();
      delay(200);
      Keyboard.println("2.lpgm");
      delay(200);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('p'); // Play
      delay(100);
      Keyboard.releaseAll();
 break;
case 4:
      // Play gate 4 program
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('s'); // Stop
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('f'); // File menu
      Keyboard.press('o'); // Open file
      delay(200);
      Keyboard.releaseAll();
      delay(200);
      Keyboard.println("4.lpgm");
      delay(200);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('p'); // Play
      delay(100);
      Keyboard.releaseAll();
 break;
case 5:
      // Play gate 5 program
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('s'); // Stop
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('f'); // File menu
      Keyboard.press('o'); // Open file
      delay(200);
      Keyboard.releaseAll();
      delay(200);
      Keyboard.println("5.lpgm");
      delay(200);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press('c'); // Control
      Keyboard.press('p'); // Play
      delay(100);
      Keyboard.releaseAll();
 break;
 }
}

One more thing is how to control the shortcut key always focus to the program i want. because if the running program not selected the shortcut key may do another task. thank for any help.

Below is the code to hopefully lock out key presses for 60 seconds once one has been presses.
I just put the lockout code in the readButton function so once a button has been pressed other calls to the function just return false until the timeout has expired and then it will start checking keys again.

Setting the focus to your program is a lot more difficult and the only way to consistently doing it is to use some program running on the PC that maybe responds to a keyboard sequence to fire off a macro to load/focus on your program. I don’t know of such a program but someone here will hopefully jump in and offer suggestions.

#include <Keyboard.h>

uint32_t releaseTime = 0; // millis value of button release time

void setup()
{
  // put your setup code here, to run once:
  // Serial.begin(115200);
  Keyboard.begin(); // setup keyboard
  for (int i = 2; i < 6; ++i)
  {
    // initilize pins
    pinMode(i, INPUT_PULLUP);
  }
}

void loop()
{
  // put your main code here, to run repeatedly:
  for (int i = 2; i < 6; ++i)
  {
    // check buttons
    if(readButton(i))
    {
      doAction(i);
    }
  }
}

boolean readButton(int pin)
{
  if(millis() > releaseTime)                // Only check button if we are not locked out
  {
    // check and debounce buttons
    if (digitalRead(pin) == HIGH)
    {
      delay(20);
      if (digitalRead(pin) == HIGH)
      {
        releaseTime = millis() + 60000;     // Lock out for 60 seconds
        return true;
      }
    }
  }
  return false;
}
void doAction(int pin)
{
  // perform tasks
  switch (pin)
  {
  case 2:
    // Sending a combo key CTRL+ALT+A to run program gate 1
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('a');
    delay(200);
    Keyboard.releaseAll();
    //keypadDisabled
    //keypadEnabled
    break;
  case 3:
    // Play gate 2 program
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('c'); // Control
    Keyboard.press('s'); // Stop
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('f'); // File menu
    Keyboard.press('o'); // Open file
    delay(200);
    Keyboard.releaseAll();
    delay(200);
    Keyboard.println("2.lpgm");
    delay(200);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('c'); // Control
    Keyboard.press('p'); // Play
    delay(100);
    Keyboard.releaseAll();
    break;
  case 4:
    // Play gate 4 program
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('c'); // Control
    Keyboard.press('s'); // Stop
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('f'); // File menu
    Keyboard.press('o'); // Open file
    delay(200);
    Keyboard.releaseAll();
    delay(200);
    Keyboard.println("4.lpgm");
    delay(200);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('c'); // Control
    Keyboard.press('p'); // Play
    delay(100);
    Keyboard.releaseAll();
    break;
  case 5:
    // Play gate 5 program
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('c'); // Control
    Keyboard.press('s'); // Stop
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('f'); // File menu
    Keyboard.press('o'); // Open file
    delay(200);
    Keyboard.releaseAll();
    delay(200);
    Keyboard.println("5.lpgm");
    delay(200);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('c'); // Control
    Keyboard.press('p'); // Play
    delay(100);
    Keyboard.releaseAll();
    break;
  }
}

You're a genius. Thank you very much. Your code working flawlessly .Have a nice day.