Help using keystrokes

I am currently doing my CSE project where the gist of it is when you hit a push button it is as if a key has been pressed. I am having problems with getting the key to be pressed and held as the button is being held down. Right now when I press it down it's as if it were being mashed rapidly on the keyboard. I was wondering if anybody had any suggestions on fixing my code.

Here's my code:

*/

const int buttonPin = 2;

int buttonState = 0;

void setup() {

pinMode(buttonPin, INPUT);

Keyboard.begin();

}

void loop(){

buttonState = digitalRead(buttonPin);

if(buttonState == HIGH) {

Keyboard.press('z');

}

else{

Keyboard.release('z');

}

}

This is only one part as the rest of my project will be the same thing (8 buttons doing different keys) Thanks to all that respond

You are seeing switch contact bounce. A very good blog on the subject with scope pictures and code.

It's usually better to detect changes in stable switch states than simply HIGH or LOW.

Thank you so much now everything works fine and I can finally get around to doing my actual building!

Wow, that was fast! You can use resources! You didn’t even ask 20 questions and then for examples!

Bookmark Nick’s blog page, go up a level and see a lot of info resource for future reference.
Down in my signature space are 3 addresses to his blogs that are keys to multitasking, the second one for the part on State Machines.

There is also the Arduino Playground as another info mine:
Playground General Code Library