My first thoughts are in case you will use the keypad for various functions or different scenes you will have global listeners for these key-pressed events. In this case I'd include in the scene a script listening to key-pressed events and save the code status in a variable.
Hard to put in words. Well imagine you have a variable called level and a code "1 2 3 4 5" stored in an Array keycode[].
now we start off in the scene with level = 1.
if key "1" is pressed it will check the current level (1) and check keycode[level]. In this case keycode[1]=1 so the script would advance to level = 2. When the next numkey event is sent to the script it will check again. For example numkey 3 is pressed it will see keycode[2]=2 -->NOT 3 so the script will reset level back to 1 and all starts over again.
is that understandable?
Let's try pseudo-code
variables:
keycode[1]=1; -->five digit code 1 - 5
keycode[2]=2;
keycode[3]=3;
keycode[4]=4;
keycode[5]=5;
level = 0; -->the level of code entered (it's successful number advances in level, wrong number resets to one)
numkey; -->the key that has been pressed.
Event: when a numkey has been pressed
{
if (numkey == keycode[level])
{
level++;
correct number entered so level advances one more step if (level > 5) trigger event / unlock door;
if level has advanced over 5 it means the code has been entered successfull and can trigger the event (e.g. to unlock a door) }
else
{
level=1;
if a wrong number is entered level is reset to 1 and you need to type all over again }
}