Press and release the 'x' key for 4 seconds with a single button

Hi there. I want to run a hotkey project using 'arduino micro'.

The problem I encountered while working on this project is that

1 button with a resistor
When the button is pressed, I want to repeat "press X for 4 seconds, release for 1 second".

int Button6 = 6;
#include "Keyboard.h"

void setup() 
 {
  pinMode(Button6, INPUT);
  Serial.begin(9600);
 }

void loop() 
 {
if (digitalRead(Button6) == HIGH) 
    {
      //delay(500);
    }
   
   else if (digitalRead(Button6) == LOW) 
    {
        Keyboard.press('x');
        delay(4000);
        Keyboard.releaseAll();
        delay(1000);
    }
 }

I've tried this far, but I don't see the X key being pressed over and over again, with a one-second pause.

Hi,
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

How have you got your button wired?
If between pin 6 and 5V, have you got a 10K pulldown resistor from pin 6 to gnd?
If between pin 6 and gnd, have you got a 10K pullup resistor from pin 6 to 5V?

Have you looked at the keyboard library example?
There should be a

 Keyboard.begin();

and a

Keyboard.write

statement, view the example and run it to find out how it works.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

ok! wait plz!

Is this enough for a pinmap?
Do we need any other information?

Hi,
Can you measure the voltage at pin 6 to see if it changes when you press the button.

Make sure you look at the example code that came with your library.

Tom.. :smiley: :+1: :coffee: :australia:

For now, i know that button 6 works.
This is because when I run the code I wrote above, button 6 keeps pressing x even though it's only pressed once. :rofl::joy:

Keyboard.write only presses x once, so we've used a press that keeps on pressing. What we want to do is press x for 4 seconds, release for 1 second, press x for 4 seconds, and so on and so forth.

Hi,
You don't need the second if statement, its logical that if button is not HIGH it is LOW.

Like this

void loop() 
 {
if (digitalRead(Button6) == LOW) 
    {
      //delay(500);
    }
   
   else
    {
        Keyboard.press('x');
        delay(4000);
        Keyboard.releaseAll();
        delay(1000);
    }
 }

Note with those button modules I assume, you press the button, pin 6 will go HIGH so your logic needs to be reversed as I have edited in the above code.

Measure it with a meter.

Tom... :smiley: :+1: :coffee: :australia:

It is not clear from your description how long you want this [press x for 4 seconds, release for 1 second] cycle to continue for, is it:

  • a certain number of cycles?

  • for a minute?

  • until friday?

  • forever?

  • until the button is pressed again?

Can you draw a graphical representation of what you actually want to happen?

If I press button 6,

[A]
X for 4 seconds and release for 1 second to make it permanent.

Because I've already completed the code to disable all of these functions.

I just want it to run as [A] once I press button 6.

I don't know if you know.
If you do it the way you told me,

My computer will be plastered with bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb until I press the button.

All I need is a button to work when it's pressed.
but the way you told me is to keep pressing b.

Hi,
Have you tried my edited code?
Do you understand the button logic?

Your original code says if the button is HIGH do nothing.

if (digitalRead(Button6) == HIGH) 
    {
      //delay(500);
    }

Tom.. :smiley: :+1: :coffee: :australia:

you mean this code?

void loop() 
 {
if (digitalRead(Button6) == LOW) 
    {
      //delay(500);
    }
   
   else
    {
        Keyboard.press('x');
        delay(4000);
        Keyboard.releaseAll();
        delay(1000);
    }
 }

yeah i try too this code
then my computer text page all press the x

Measure the volts on Pin 6 when you push the button and release it.

It sounds like your button may be LOW output when you press, so change the LOW in my code to HIGH.

Tom.. :smiley: :+1: :coffee: :australia:
PS. Can you please post some images of your project so we can see your component layout?

how
like this

Hi,
Explain button pressed ONE TIME?
Do you mean just momentarily press it and the x's will be sent for 4 seconds JUST once and if the button is held down will repeat and repeat with 1 second gaps?

Tom.. :smiley: :+1: :coffee: :australia:

Thank you again for your hard work, it is greatly appreciated. Thank you.

To be exact,

Press button 6 once ();
{
Press X for 4 seconds
Release the button for a second.
Press X for 4 seconds
Release the button for a second.
Press X for 4 seconds
...
...
...
}

I want this kind of movement. :joy:

Hi,
So you only want one cycle per press, if held down for more than 5 seconds it doesn't repeat, but waits for the button to be released.

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.