Can't programmatically click and input after "focus out"

Discussions related to using VirtualBox on Windows hosts.
Post Reply
gvvice
Posts: 18
Joined: 9. Jun 2022, 18:14

Can't programmatically click and input after "focus out"

Post by gvvice »

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:

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)
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

Re: Can't programmatically click and input after "focus out"

Post by scottgus1 »

It appears that your script is working with the mouse. I would suggest to turn off Mouse Integration in the VM, see the Status bar, mouse icon; and possibly use the PS/2 mouse instead of a USB Tablet, see the VM's System Motherboard settings.
gvvice
Posts: 18
Joined: 9. Jun 2022, 18:14

Re: Can't programmatically click and input after "focus out"

Post by gvvice »

scottgus1 wrote:It appears that your script is working with the mouse. I would suggest to turn off Mouse Integration in the VM, see the Status bar, mouse icon; and possibly use the PS/2 mouse instead of a USB Tablet, see the VM's System Motherboard settings.
Tired both PS/2 and USB Tablet, still can't solve this, also tried to enable/disable drag and drop, mouse integration, auto capture keyboard, still.

Keyboard will also not be able to use, any key events will also just go to the black hole...

Mouse status: "MI is off, pointer is not captured".

But for this code example, it will end with an error:

Code: Select all

import win32api, win32con
def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(500,80)

=> error: (0, 'SetCursorPos', 'No error message is available')
Also tried this, goes to the hole:

Code: Select all

ctypes.windll.user32.SetCursorPos(x, y)
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

Re: Can't programmatically click and input after "focus out"

Post by scottgus1 »

One other idea I had last night, set up a Remote Desktop session into the VM, either through Virtualbox settings (see Display, Extension Pack is required) or through direct-into-the-VM-OS RDP or VNC. Then when the session is live, Alt-Tab out of the remote client.

Alternatively, use a USB Filter to pass a secondary USB mouse into the VM, with Mouse Integration remaining off.

Virtualbox doesn't have many settings to control the ability of a mouse to split into two so the VM can run a script on the VM's mouse while the host mouse is used elsewhere. You could scan the manual, the PDF version on the Virtualbox Downloads website is searchable
gvvice
Posts: 18
Joined: 9. Jun 2022, 18:14

Re: Can't programmatically click and input after "focus out"

Post by gvvice »

scottgus1 wrote:One other idea I had last night, set up a Remote Desktop session into the VM, either through Virtualbox settings (see Display, Extension Pack is required) or through direct-into-the-VM-OS RDP or VNC. Then when the session is live, Alt-Tab out of the remote client.

Alternatively, use a USB Filter to pass a secondary USB mouse into the VM, with Mouse Integration remaining off.

Virtualbox doesn't have many settings to control the ability of a mouse to split into two so the VM can run a script on the VM's mouse while the host mouse is used elsewhere. You could scan the manual, the PDF version on the Virtualbox Downloads website is searchable
Solved, thanks
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

Re: Can't programmatically click and input after "focus out"

Post by scottgus1 »

Glad you're up and running! Out of curiosity, what fixed it?
Post Reply