You could always look into removing the default actions of the tab
key, there is a small tutorial on how to do this here.
However, that is for the enter
and tab
key, but if you also look here, you will see that the tab
key is key number 9. So if we alter the code to just remove the default actions of the tab
key your line would be something similar to
if ( key == 9 ) { }
rather than
if ( key == 3 || key == 9 || key == 13 ) { }
EDIT
Okay, since now you've stated you don't want to prevent the default action of the tab
key, but you still wish to stop that key from focusing on elements below the modal window.
The only thing I can think of now is, checking to see if the user is focused on your last field example 1 / example 2 (you would link these to your last input/textarea/checkbox/radio). If so, you could do 1 of 2 things:
1) Use the above code and prevent the default action, thus, stopping the using from focusing on elements below the modal.
OR
2) Focus back on the first field in your modal window, with first_field.focus();
As far as I can see, by sticking with your requirements the second choice is the best option for you.