Sadly I don't think you can invoke this behavior and this is not the solution you are asking for, though as I imagine you are using a keyboard (apple?) you could always use the hotkeys to lock your laptop and screen.
- Option–Command–Power buttonOption–Command–Power / Option–Command–Media EjectOption–Command–Media Eject: Put Mac to sleep
- Control–Shift–Power buttonControl–Shift–Power / Control–Shift–Media EjectControl–Shift–Media Eject : Put display(s) to sleep