在 VirtualBox 中运行的 Windows 10 上检测 USB 输入设备的插入/移除 [英] Detecting insertion/removal of USB input devices on Windows 10 running in VirtualBox

查看:38
本文介绍了在 VirtualBox 中运行的 Windows 10 上检测 USB 输入设备的插入/移除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再一次已经有了一些可用的 Python 代码来检测 Windows 10 中特定 USB 设备类型的插入/移除(来自 此处).

I once again already have some working Python code to detect the insertion/removal of specific USB device types in Windows 10 (from here).

import wmi

device_connected_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
device_disconnected_wql = "SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"

c = wmi.WMI()
connected_watcher = c.watch_for(raw_wql=device_connected_wql)
disconnected_watcher = c.watch_for(raw_wql=device_disconnected_wql)

while 1:
    try:
        connected = connected_watcher(timeout_ms=10)
    except wmi.x_wmi_timed_out:
      pass
    else:
        if connected:
            print("Keyboard connected")

    try:
        disconnected = disconnected_watcher(timeout_ms=10)
    except wmi.x_wmi_timed_out:
      pass
    else:
        if disconnected:
            print("Keyboard disconnected")

我想在运行于 Windows 10 VirtualBox 6.0.22 和 Ubuntu 18.04 (x64) 的 Python 脚本中使用此代码.VirtualBox Guest Additions 已安装.

I wanted to use this code in a Python script that runs on Windows 10 in VirtualBox 6.0.22 on Ubuntu 18.04 (x64). VirtualBox Guest Additions are installed.

不幸的是,此脚本不起作用,因为在插入或移除 USB 键盘时它不会显示任何消息.是否需要为此更改 VirtualBox 配置?

Unfortunately this script does not work, because it does not display any message when a USB keyboard is inserted or removed. Does the VirtualBox configuration need to be changed for this?

但是,退出脚本时出现如下错误:进程结束,退出代码 -1

However, the following error appears when exiting the script: Process finished with exit code -1

推荐答案

首先,使用 While True: 代替 while 1:.然后,确保在 VirtualBox 中正确配置了您的 USB 驱动器(此 link 可能有帮助).而我发现的 Process finished with exit code -1 都与 PyCharm 相关,如果你正在使用它,idk.希望这会有所帮助.

Firstly, instead of while 1:, use While True:. Then, make sure that your USB drive is configured correctly in VirtualBox (this link may help). And what I found for Process finished with exit code -1 is all related to PyCharm, idk if you are using this. Hope this helps.

这篇关于在 VirtualBox 中运行的 Windows 10 上检测 USB 输入设备的插入/移除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆