I've been playing with a small matrix keypad with diodes and the Teensy 4 and keypad library. A small test application I wrote that works fine on an Arduino Pro Micro was behaving differently on the Teensy 4.

On the Teensy 4, the code (basic usage of Keypad library) works fine without any diodes in the matrix of switches. If I added the diodes back into the matrix, I saw strange ghosting of characters and some keys not being detected at all. I started wondering if the (much) higher speed of the Teensy 4 might be causing some row/column input detection from being seen.

I began making some changes to the Keypad library by adding some delays at various points. With some trial and error, I was able to narrow down the problem a bit. Perhaps you can shed some light on what I have found....

In the Keyboard library source code (the Keypad.cpp file in particular) there is a point in a loop where pin_mode() is called repetitively. If I insert a delay, even a delay as small as one microsecond between the pin_mode() calls, the code works fine and all keys are detected properly. Without the delay, the issue still exists.

Does it seem as though switching the pin_mode() takes a bit of time to accomplish, hence the need for the delay? I have include a section of the Keypad.cpp code, with my delayMicroseconds() remarked for comparison.


Code:
void Keypad::scanKeys()
{
	// Re-intialize the row pins. Allows sharing these pins with other hardware.
	for (byte r = 0; r < sizeKpd.rows; r++)
	{
		pin_mode(rowPins[r], INPUT_PULLUP);
	}

	// bitMap stores ALL the keys that are being pressed.
	for (byte c = 0; c < sizeKpd.columns; c++)
	{
		pin_mode(columnPins[c], OUTPUT);
		pin_write(columnPins[c], LOW); // Begin column pulse output.

		for (byte r = 0; r < sizeKpd.rows; r++)
		{
			bitWrite(bitMap[r], c, !pin_read(rowPins[r])); // keypress is active low so invert to high.
		}
		// Set pin to high impedance input. Effectively ends column pulse.
		pin_write(columnPins[c], HIGH);
		pin_mode(columnPins[c], INPUT);
		delayMicroseconds(1);  Without this delay, Teensy keypad has ghosting problems and 'misses' key detection.
	}
}