I want to detect if 2 keys are pressed within a certain time frame, thus detecting if they are pressed simultaneously or not.

For this, I store the millis() when a key is pressed in uint32_t values key1millis and key2millis.

Then I calculate the absolute difference of these 2 unsigned values.

It turned out a little tricky to get the correct results, and I am wondering if it could be done better.

Here is the function code I am using:

```
const uint32_t MAXDIFF = 0xFFFFFFFF / 2 ;
const uint16_t MAXTIMEDIFF = 100 ; // max 100ms delay between key1 and key2
uint32_t getABSdiff( uint32_t X, uint32_t Y )
{ uint32_t Diff = X - Y ;
if( Diff > MAXDIFF ) Diff = Y - X ;
else
{ if( X < Y ) Diff = Y - X ;
if( Diff > MAXDIFF ) Diff = X - Y ;
}
return( Diff ) ;
}
if( getABSdiff( key1millis, key2millis ) < MAXTIMEDIFF )
Serial.println( "simultaneous" ) ;
```