Count button Pressed

Hello,

I have been trying to make a menu, I use the enter button for turn on a Led which is the corresponded menu level. This I would like to do is that, if the user press the menu button over a second, then the enter button will operate as an exit buttton rather than enter button.
Can anyone help me for this task? Below I could illustrate my code which doesn't work.

Thank you in advance!

My first effort

int PressEnterButton (void)
{
unsigned long StartCount,EndCount,ResultCount,idleTime;
int EnterButton ,LastEnterButton ;
int state,ConfirmState = 0 ;

while(true){
EnterButton = digitalRead(9);
delay(100);

if(EnterButton != LastEnterButton)
{
if(!EnterButton)
{
StartCount = millis();
ConfirmState = 1;
idleTime = StartCount - EndCount;
}
else{
EndCount = millis();
ResultCount = EndCount - StartCount;
if((idleTime< 1000) && (ConfirmState == 1))
{
return state = 2;
}
else if((idleTime > 1000) && (ConfirmState == 1))
{
return state = 1;
}

}

My second effort

int PressEnterButton (void)
{
unsigned long StartCount,CurrentCount,ResultCount;
int EnterButton,state,ConfirmCount = 0;

EnterButton = digitalRead(9);
delay(100);
StartCount = millis();
if(EnterButton == 0)
{
while(!EnterButton)
{
CurrentCount = millis();
ConfirmCount = 1;
}
ResultCount = CurrentCount - StartCount;

if(ResultCount >= 1000)
{
return state = 2;
}
else
{
return state = 1;
}

}
else
{
return state = 0;
}}

There is a button library that has long press detection.

Or you can see this button long press and short press tutorial

Thank you a lot, I'm going to read this very carefully.
I appreciate your help!

Record the value of millis() when the switch closes.

closedTime = millis();

When the switch opens, subtract what you saved from the current value of millis(), this is the time the switch was pressed/closed.

timePressed =millis() - closedTime;

Use this value to see if the closure was long enough.

if(timePressed >= 1000)
{
// do exit stuff
...
}

Thank you Larryd, I will try your advice!