You need to start by setting up two input bindings in your .input_binding file, one for the R key and you can bind that to the action restart and the other for the ESC key and bound to main_menu.
Next you check for these in on_input():
function on_input(self, action_id, action)
if action_id == hash("restart") and action.pressed then
-- run code to restart level
elseif action_id == hash("main_menu") and action.pressed then
-- run code to load and show main menu
end
end