Reputation: 19
while selecting corrresponding mice or keyboard in prompt box.EventListener is not getting triggered.
expecting:to get mouse low-level event in web browser
any other way to get mouse and keyboard low-level event in web browser
The WebHID API (Human Interface Device API) is a web standard that allows web applications to interact with human input devices such as keyboards, mice, game controllers, and other input devices directly from a web browser. It is designed to provide low-level access to input devices, similar to how the WebUSB and WebBluetooth APIs provide access to USB and Bluetooth devices, respectively.
Here are some key points about the WebHID API:
Low-Level Access: WebHID allows web applications to access input devices at a low level, meaning developers can access and process input events directly from the device, bypassing the browser's default handling of input.
Privacy and Security: Like other web APIs, WebHID is subject to browser security restrictions. It requires user consent to access input devices, ensuring user privacy and security.
Device Enumeration: The API provides methods to enumerate and list the available HID devices connected to the user's computer.
Event Handling: WebHID allows developers to set up event listeners to respond to input events from HID devices. This can include keyboard key presses, mouse movements, button presses on game controllers, and more.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>WebHID Keyboard Example</title>
</head>
<body>
<button id="requestDeviceButton">Request HID Keyboard Device</button>
<input type="text" />
<script>
document.addEventListener("DOMContentLoaded", () => {
const requestDeviceButton = document.getElementById(
"requestDeviceButton"
);
if ("hid" in navigator) {
requestDeviceButton.addEventListener("click", async () => {
try {
const devices = await navigator.hid.requestDevice({
filters: [],
});
devices.forEach(async (device) => {
await device.open();
device.addEventListener(
"inputreport",
(event) => {
const data = new Uint8Array(
event.data.buffer
);
// Process keyboard input data
console.log(
"Received keyboard input:",
data
);
}
);
console.log("Connected HID devices:", device);
});
const getdevices = await navigator.hid.getDevices();
console.log("getdevices", getdevices);
} catch (error) {
console.error(
"Error opening HID keyboard device:",
error
);
}
});
} else {
console.error(
"WebHID API is not supported in this browser"
);
}
});
</script>
</body>
</html>
Upvotes: 1
Views: 512
Reputation: 1011
This doesn't work because WebHID blocks access to these low-level inputs for privacy and security reasons. If you could access low-level input reports for mice and keyboards it would be possible to implement input loggers that steal a user's passwords and other private information.
Blocked devices are listed on the WebHID blocklist.
Upvotes: 0