Skip to content

Instantly share code, notes, and snippets.

@unarist
Last active March 12, 2022 13:00
Show Gist options
  • Save unarist/1d3c51a664abfb600162cc8fa0a40a01 to your computer and use it in GitHub Desktop.
Save unarist/1d3c51a664abfb600162cc8fa0a40a01 to your computer and use it in GitHub Desktop.
SpaceMouseを押すと青く光って音がなるやつ。要WebHID=Chromeのみ。
<ol>
<li>音声ファイルを選ぶ: <input type="file"></li>
<li><button>connect</button> を押してSpaceMouseを選んで接続する</li>
<li>押すと光って音がなる(Windowsとかだと Stop 3DxWare してからやるといい感じ)</li>
</ol>
<script>
let audioUrl = undefined;
document.querySelector('input').onchange = ({target}) => {
const file = target.files[0];
audioUrl = file ? URL.createObjectURL(file) : undefined;
};
document.querySelector('button').onclick = async function() {
const device = (await navigator.hid.requestDevice({filters:[]}))[0];
await device.open();
let last = false;
device.addEventListener('inputreport', async ({data, reportId}) => {
if (reportId !== 0x01) return;
const s = data.getInt16(4, true) > 180;
if (last !== s && s) {
new Audio(audioUrl).play();
await device.sendReport(0x04, Uint8Array.from([0x01]));
await new Promise(r=>setTimeout(r, 200));
await device.sendReport(0x04, Uint8Array.from([0x00]));
}
last = s;
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment