Key remains pressed in keypad

I’m trying to use a membrane keypad, and its library, as input for a music synthesizer. I’m running into a problem because the program acts as if the keys stay pressed. In other words, keypad.isPressed() returns true after a key has been released. Here is the bit of code that I need help with:

void loop() {
 pressed_note = instrument.getKey();  //gets the key that is being pressed
 if (pressed_note && pressed_note != '8'){  //if available key in keypad is pressed
   while (instrument.isPressed(pressed_note)){
     digitalWrite(buzzPin, HIGH);  //next four lines play the note, delay based on freq of note
     delayMicroseconds(notes[pressed_note-'0']);
     digitalWrite(buzzPin, LOW);
     delayMicroseconds(notes[pressed_note-'0']);
   }
 }
}

I also tried changing the value within the while loop, but this leads to only one iteration happening, even if the key is still pressed:

void loop() {
  pressed_note = instrument.getKey();  //gets the key that is being pressed
  if (pressed_note && pressed_note != '8'){  //if available key in keypad is pressed
    while (instrument.isPressed(pressed_note)){
      pressed_note = instrument.getKey();  //NEW LINE
      digitalWrite(buzzPin, HIGH);  //next four lines play the note, delay based on freq of note
      delayMicroseconds(notes[pressed_note-'0']);
      digitalWrite(buzzPin, LOW);
      delayMicroseconds(notes[pressed_note-'0']);
    }
  }
}

Why is this happening? How can I fix it? Any input would help.

I have also put my complete code below in case anyone wants to look at it. Thank you in advance!

#include <Keypad.h>

const int ROWS = 4;
const int COLS = 4;
byte colPins[COLS] = {5,4,3,2}; //connect to the row pinouts of the keypad
byte rowPins[ROWS] = {9,8,7,6}; //connect to the column pinouts of the keypad

char keys[ROWS][COLS] = {
  {'0', '1', '2', '8'},
  {'3', '4', '5', '8'},
  {'6', '7', '8', '8'},
  {'8', '8', '8', '8'}
};

Keypad instrument = Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS);  //starts keypad

int buzzPin = 13;
long unsigned int notes[8] = {3817, 3405, 3034, 2863, 2551, 2273, 2025, 1908};  //based on frequencies of notes
char pressed_note;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(buzzPin, OUTPUT);
}

void loop() {
  pressed_note = instrument.getKey();  //gets the key that is being pressed
  if (pressed_note && pressed_note != '8'){  //if available key in keypad is pressed
    while (instrument.isPressed(pressed_note)){
      pressed_note = instrument.getKey();
      digitalWrite(buzzPin, HIGH);  //next four lines play the note, delay based on freq of note
      delayMicroseconds(notes[pressed_note-'0']);
      digitalWrite(buzzPin, LOW);
      delayMicroseconds(notes[pressed_note-'0']);
    }
  }
}

Instead of trying to play notes first get the keypad working properly
In a loop just read the pressed key and print it out the serial port first
and troubleshoot from there.

noweare:
Instead of trying to play notes first get the keypad working properly
In a loop just read the pressed key and print it out the serial port first
and troubleshoot from there.

I was already able to do that. I'm only having trouble finding a way to record that the key remains pressed. Do you have any input on how to do that?

If you press a key and release the statement below remains true ?

while (instrument.isPressed(pressed_note))

I think you have to call instrument.getKey() again to re-scan the keyboard.

You should use the tone() function to generate a note in hardware rather than trying to generate a note in software.

https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/

isPressed is a multi-keypress function, and is updated with getKeys().

If you only have to press one key at a time, use getKey(), which is updated every time the state changes (PRESSED, HOLD, RELEASED), and act according to the state.

Ref Arduino Playground - Keypad Library