How to control the return key's event?

I have a modal in my app for log in and some settings. The first time user open this app, the modal show. After log in, the homepage shows. If user open this app later, the modal hide because of the have logged.
The question is how can I control the event of the return key for running in the first time? I must close the app when the modal shows and user not log in while user press the return key. How can I do this?