Can't programmatically click and input after "focus out"
Posted: 9. Jun 2022, 18:31
I installed Windows 10 home edition on my windows host's VirtualBox, and I want to do some programmatically jobs with VirtualBox.
After installed python3.8, copy/paste the script files into VM, and ran the script with python, It works fine from the beginning as if it was running on my host, but if I want it to keep running after "focus out" which released my mouse and keyboard back to work with my host, problems will happen...
From VBox.logs, I noticed such relevant things:
01:47:44.109807 GUI: Machine-window #0 deactivated
01:47:44.109830 GUI: Releasing mouse on focus out
01:47:44.109877 GUI: Releasing keyboard on focus out
01:47:44.116301 GUI: Machine-view #0 unfocused, reason=3
After this "releasing" log printed and wait a little bit more time(can't be sure how long so far), the scripts will stop working correctly, all mouse and keyboard events will not be able to work, no effects at all, and the scripts won't have any exceptions, looks like it just keep running in an black hole:(
To let it continue to work again, I have to click to activate the VM window, then have to click anywhere in VM screen, after this two actions, everything for my little script will work again...
(Only activate the window without that "one click anywhere" action won't be enough, even I can move the mouse in VM, and scripts will still not be able to click or type)
Simple Code here:
After installed python3.8, copy/paste the script files into VM, and ran the script with python, It works fine from the beginning as if it was running on my host, but if I want it to keep running after "focus out" which released my mouse and keyboard back to work with my host, problems will happen...
From VBox.logs, I noticed such relevant things:
01:47:44.109807 GUI: Machine-window #0 deactivated
01:47:44.109830 GUI: Releasing mouse on focus out
01:47:44.109877 GUI: Releasing keyboard on focus out
01:47:44.116301 GUI: Machine-view #0 unfocused, reason=3
After this "releasing" log printed and wait a little bit more time(can't be sure how long so far), the scripts will stop working correctly, all mouse and keyboard events will not be able to work, no effects at all, and the scripts won't have any exceptions, looks like it just keep running in an black hole:(
To let it continue to work again, I have to click to activate the VM window, then have to click anywhere in VM screen, after this two actions, everything for my little script will work again...
(Only activate the window without that "one click anywhere" action won't be enough, even I can move the mouse in VM, and scripts will still not be able to click or type)
Simple Code here:
Code: Select all
import win32api, win32con, time
def leftClick():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 100,100)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 100,100)
print('Left Click')
while True:
leftClick()
time.sleep(1)