sethmlarson / virtualbox-python

Complete implementation of VirtualBox's COM API with a Pythonic interface.
https://pypi.org/project/virtualbox
Apache License 2.0
354 stars 75 forks source link

How to hold down keys? #144

Closed Karol-G closed 4 years ago

Karol-G commented 4 years ago

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

Karol-G commented 4 years ago

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