Thankyou for your answer. I tried using the above function, but the results were not as expected.
I tried to debug it but I found out the following comment on lighthouse3D
Note: This doesn’t work exactly as it says in Microsoft Windows, this is because in Microsoft’s OS the focus is changed with a mouse click. Although you can change this is your own system using some tools from Microsoft, others are likely to have the standard setting so its probably better if you don’t use this feature in Microsoft Windows to detect when the mouse enters/leaves the window.