Drive motor Using Accelstepper and pushbutton

Im trying to write a program that will move a motor when a button is pressed using the accelstepper function. Right now the program I have only runs when the the button is pressed down but stops when it is released. I am wondering how I would program this so that when the button is pressed and released it runs through the if loop and moves the motor.

#include <AccelStepper.h>

AccelStepper stepper(1, 7, 6);
int buttonPin=8; // define button input pin

void setup()
{

  pinMode(buttonPin, INPUT_PULLUP);

}

void loop()
{
  //check button pressed, if so enter program condition (inside if statement)
  if(digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW
  {
  stepper.setMaxSpeed(1000);//1100
  stepper.setAcceleration(1100);
  stepper.moveTo(2000);

    stepper.run();
  }
}

For how long should the motor move after the button is pressed?

You need to check when the button changes from not-pressed to pressed and then set a variable (call it motorRun = true;) and then keep the motor going if that variable is true.

...R

You need to remember the state of the button and then run your if statement per what the last state of the button is. You also need to use millis instead of delay when debouncing the button so it doesn't interfere with the motor movement.
You might have to play with the LOW's and HIGH's of the button code to get it to run the way you want because I haven't tested it to make sure it works, this is just a quick guide to get you in the right direction.

const int buttonPin = 8;            // pin button is on
int val = HIGH;                     // current button state
int old_val = HIGH;
int buttonstate = HIGH;
unsigned long previousMillis;

void setup() {
  previousMillis = millis();
}

void loop() {

  if ( (millis() - previousMillis) >= 50) {    //state & button debounce
    val = digitalRead(buttonPin);
    if ((val == HIGH) && (old_val == LOW)) {
      buttonstate = LOW;
    }
    previousMillis = millis();
    old_val = val;
  }
  if(digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW
  {
  stepper.setMaxSpeed(1000);//1100
  stepper.setAcceleration(1100);
  stepper.moveTo(2000);

    stepper.run();
  }