sethmlarson/virtualbox-python

How to hold down keys?

Karol-G opened this issue · 1 comments

Hi,

I managed to send keystrokes to the VM with:

vbox = virtualbox.VirtualBox()
machine = vbox.find_machine("Ubuntu 20")  # Find a running VM named "Ubuntu 20"
session = machine.create_session()  # Connect to the VM
session.console.keyboard.put_keys("Hello, world!")  # Send the keys "Hello, world!" to the VM

However I also need to keep them pressed / hold them down and then release them later. I could not find anything in https://buildmedia.readthedocs.org/media/pdf/pyvbox/latest/pyvbox.pdf

Something like this would be best:

vbox = virtualbox.VirtualBox()
machine = vbox.find_machine("Ubuntu 20")  # Find a running VM named "Ubuntu 20"
session = machine.create_session()  # Connect to the VM
session.console.keyboard.key_down("p")  # Hold down the key 'p'
# Do some other stuff
session.console.keyboard.key_up("p")  # Release key 'p'

Best
Karol

Ok. I managed to solve it myself:

def key_down(key):
    put, _ = session.console.keyboard.SCANCODES[key]
    session.console.keyboard.put_scancodes(list(put))

def key_up(key):
    _, release = session.console.keyboard.SCANCODES[key]
    session.console.keyboard.put_scancodes(list(release))

Best
Karol