我几乎在使用 Nintendo Switch Joy-Con 控制器演示,我已经对其进行了一些修改,以使其与我的条形码扫描仪一起使用。而且它只是不起作用,如果它确实起作用,它会在 100 次站点刷新中起作用。
console.log = text => {
log.textContent += `${text}\r\n`;
};
let device;
if (!("hid" in navigator)) {
console.log("WebHID is not available yet.");
}
navigator.hid.getDevices().then(devices => {
if (devices.length == 0) {
console.log(`No HID devices selected. Press the "request device" button.`);
return;
}
device = devices[0];
console.log(`User previously selected "${device.productName}" HID device.`);
console.log(`Now press "open device" button to receive input reports.`);
});
requestDeviceButton.onclick = async event => {
document.body.style.display = "none";
try {
const filters = [
{
vendorId: "8792",
productId: "9032"
}
];
[device] = await navigator.hid.requestDevice({ filters });
if (!device) return;
console.log(`User selected "${device.productName}" HID device.`);
console.log(`Now press "open device" button to receive input reports.`);
} finally {
document.body.style.display = "";
}
};
openButton.onclick = async event => {
if (!device) return;
await device.open();
console.log(`Waiting for user to press button...`);
device.addEventListener("inputreport", event => {
const { data, device, reportId } = event;
if (device.productId != "9032") return;
const value = data.getUint8(0);
if (value == 0) return;
console.log(`Data: ${value}.`);
});
};
每次我用条形码扫描仪扫描某些东西时都会触发 openButton.onclick 事件。正因为如此,每次我扫描某些东西时,它都会尝试再次执行 device.open() 。并且 inputreport 事件根本不会触发。
有谁知道是什么原因造成的?