You need to test the keys and then store the state in a variable, You only change the variable when a key was pressed. You then only test the state of the variable.
#include <acknex.h>
#define STATE_1 1
#define STATE_2 2
// Variable initialisation
var current_state = STATE_1;
void main()
{
...
while(1)
{
if (key_...)
{
current_state = STATE_1 ;
}
else if ( key_... )
{
current_state = STATE_2 ;
}
...
switch ( current_state )
{
case STATE_1:
...
break;
case STATE_2:
...
break;
}
}
...
}
You can separate the codes in functions, eg handle_keys() ; do_stuff () ;