Let’s say you have a Windows VM on Linux and you want to use the same keyboard and mouse inside the VM.

What if I told you this was a very easy process thanks to the evdev input device type on libvirt.

If you go to edit the XML definition of your VM you will find a <device> ... </device> block.

All you have to do to make this work is add this snippet into the this block.


<devices>
    ...
    <input type='evdev'>
      <source dev='/dev/input/by-id/MOUSE_ID'/>
    </input>
    <input type='evdev'>
      <source dev='/dev/input/by-id/KEYBOARD_ID' grab='all' repeat='on'/>
    </input>
    ...
</devices>

You will just need to replace MOUSE_ID and KEYBOARD_ID with the corresponding id for your device.

On my system running the following command gives this output.


% ls -l /dev/input/by-id                                                                                                                                                       !1243
total 0
lrwxrwxrwx 1 root root  9 Dec 11 13:52 usb-Corsair_Corsair_Gaming_K95_RGB_PLATINUM_Keyboard_1401700AAF37A1065B5FE70DF5001BC3-event-if00 -> ../event5
lrwxrwxrwx 1 root root  9 Dec 11 13:52 usb-Corsair_Corsair_Gaming_K95_RGB_PLATINUM_Keyboard_1401700AAF37A1065B5FE70DF5001BC3-event-kbd -> ../event2
lrwxrwxrwx 1 root root  9 Dec 11 13:52 usb-Corsair_Corsair_Gaming_K95_RGB_PLATINUM_Keyboard_1401700AAF37A1065B5FE70DF5001BC3-event-mouse -> ../event6
lrwxrwxrwx 1 root root  6 Dec 11 13:52 usb-Corsair_Corsair_Gaming_K95_RGB_PLATINUM_Keyboard_1401700AAF37A1065B5FE70DF5001BC3-mouse -> ../js0
lrwxrwxrwx 1 root root 10 Dec 11 13:52 usb-Corsair_CORSAIR_HS70_Pro_Wireless_Gaming_Headset-event-if03 -> ../event24
lrwxrwxrwx 1 root root  9 Dec 11 13:52 usb-Logitech_Gaming_Mouse_G402_6D8022AA4948-event-mouse -> ../event7
lrwxrwxrwx 1 root root  9 Dec 11 13:52 usb-Logitech_Gaming_Mouse_G402_6D8022AA4948-if01-event-kbd -> ../event8
lrwxrwxrwx 1 root root  9 Dec 11 13:52 usb-Logitech_Gaming_Mouse_G402_6D8022AA4948-mouse -> ../mouse1
lrwxrwxrwx 1 root root 10 Dec 11 13:52 usb-PC-LM1E_PC-LM1E_PC-LM1E-event-if00 -> ../event25

As you can see my keyboard and mouse have multiple ids. If it’s the same for you you’ll have to test which id is the correct one. You can do this knowing that only ids containing event are valid and that you can test the id by using

cat /dev/input/by-id/device_id

You will know you found the correct id when cat starts outputing anything (it could be weird characters too).

If this works for you congrats, but we still need to add two more lines to the <device>. The block we just added emulates the keyboard and mouse as PS/2 devices.

We can add


<devices>
    ...
    <input type='mouse' bus='virtio'/>
    <input type='keyboard' bus='virtio'/>
    ...
</devices>

This will convert the PS/2 devices into virtio inputs. Don’t forget to install virtio drivers !