Encoder knob push/release detection

I have a single encoder which has a push feature. Is there a way to detect when the encoder button is pushed in and then detect when it's released?

What I want to do is detect the push so I can emulate a mouse click...until I release the push, then release the mouse click.

Presently, when I push in, I am using a delay() to release the mouse click.

Below is my existing code.

  if ((!digitalRead(encoder_HDG_SingleKnob_PushSwitch)))
  {
    //Serial.println("hdg press");
    Mouse.press();
    delay(780);
    Mouse.release();
  }

Check out the state change detection tutorial. It shows how to detect when the switch becomes pressed (the transition) and unpressed.

Here is an example to illustrate the state change detection. The example differs from the tutorial in that the switch is connected to ground so the input is active LOW (LOW when the switch is pressed). Wired that way allows use of the internal pullup resistor.

// state change dtection demo.  C Goulding

// constants won't change:
const int  buttonPin = 4;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
// Variables will change:
boolean buttonState = HIGH;         // current state of the button
boolean lastButtonState = HIGH;     // previous state of the button

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}


void loop()
{
   // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);
   // compare the buttonState to its previous state
   if (buttonState != lastButtonState)
   {
      // if the state has changed, increment the counter
      if (buttonState == LOW)
      {
         // if the current state is LOW then the button
         // went from off to on:
         Serial.println("mouse down");
      }
      else
      {
         // if the current state is LOW then the button
         // went from on to off:
         Serial.println("mouse up");

      }
      delay(50);
   }
   // save the current state as the last state,
   //for next time through the loop
   lastButtonState = buttonState;
}

That worked great.

Thank you very much for the help!