add simple mouse controls

This commit is contained in:
2025-07-19 07:05:28 -04:00
parent cf9cfa9ef9
commit d3b12890bb
6 changed files with 1029 additions and 813 deletions
+55 -7
View File
@@ -91,6 +91,7 @@ div.menu a:hover {
var keys = [];
var fetchqueue = [];
var curLed = 0;
var sendKeyInterval = null;
const indicator_list = [
{id: "num", label: "NumLock"},
{id: "caps", label: "CapsLock"},
@@ -102,7 +103,7 @@ const macro_list = [
{id: "close", label: "ALT+F4", keys: ["AltLeft", "F4"]},
{id: "cycle", label: "ALT+Tab", keys: ["AltLeft", "Tab"]}
];
const key_list = [
const keyboard_list = [
[
{id: "Escape", label: "ESC", width: 2},
{id: "F1", label: "F1"},
@@ -223,6 +224,30 @@ const key_list = [
{id: "NumpadDecimal", label: "Del<br>."}
],
];
const mouse_list = [
[
{id: "", label: ""},
{id: "MouseUp", label: "<br>&uArr;", repeat: 1},
{id: "", label: ""}
],
[
{id: "MouseLeft", label: "<br>&lArr;", repeat: 1},
{id: "", label: ""},
{id: "MouseRight", label: "<br>&rArr;", repeat: 1}
],
[
{id: "", label: ""},
{id: "MouseDown", label: "<br>&dArr;", repeat: 1},
{id: "", label: ""}
],
[
{id: "", label: "", width: 3}
],
[
{id: "MouseClickLeftt", label: "Left Click", width: 1.5, height: 0.75},
{id: "MouseClickRight", label: "Right Click", width: 1.5, height: 0.75}
],
];
window.setInterval("refreshIndicators();",2000);
@@ -231,7 +256,8 @@ window.addEventListener("keyup", onKeyUp, true);
window.onload = (event) => {
createMacros("macros");
createKeys("keyboard");
createKeys("keyboard", keyboard_list);
createKeys("mouse", mouse_list);
}
function onKeyDown(event) {
@@ -245,12 +271,15 @@ function onKeyDown(event) {
event.preventDefault();
}
function pressKey(code) {
function pressKey(code, repeat=false) {
if (keys.includes(code)) { return; }
keys.push(code);
sendKeys(keys);
if ( sendKeyInterval == null && repeat) {
sendKeyInterval = setInterval( function() { sendKeys(keys); }, 200 );
}
}
function onKeyUp(event) {
@@ -264,19 +293,24 @@ function onKeyUp(event) {
event.preventDefault();
}
function releaseKey(code) {
function releaseKey(code, repeat=false) {
if (keys.includes(code)) {
keys.splice(keys.indexOf(code),1);
}
sendKeys(keys);
if (sendKeyInterval != null && repeat) {
clearInterval(sendKeyInterval);
sendKeyInterval = null;
}
}
function sendKeys(curKeys) {
const url="sendkeys.cgi?keys=".concat(curKeys);
fetchqueue.push(url);
console.log(fetchqueue);
//console.log(fetchqueue);
document.getElementById("downKeys")
.innerHTML = curKeys;
prev_keys = document.getElementsByClassName("pressed");
@@ -314,8 +348,8 @@ function refreshIndicators() {
indicatorFrame.contentWindow.location.reload(true);
}
function createKeys(keyboard_id) {
let keyboardDiv = document.getElementById(keyboard_id);
function createKeys(div_id, key_list) {
let keyboardDiv = document.getElementById(div_id);
for (let row of key_list) {
let newRow = document.createElement("div");
keyboardDiv.appendChild(newRow);
@@ -325,6 +359,16 @@ function createKeys(keyboard_id) {
newDiv.id = key.id;
newDiv.innerHTML = key.label;
newDiv.className = "key";
if (key.repeat) {
newDiv.addEventListener("touchstart", function (event) {
event.preventDefault(); pressKey(key.id, true); });
newDiv.addEventListener("touchend", function (event) {
event.preventDefault(); releaseKey(key.id, true); });
newDiv.addEventListener("mousedown", function () {
pressKey(key.id, true); });
newDiv.addEventListener("mouseup", function () {
releaseKey(key.id, true); });
} else {
newDiv.addEventListener("touchstart", function (event) {
event.preventDefault(); pressKey(key.id); });
newDiv.addEventListener("touchend", function (event) {
@@ -333,6 +377,7 @@ function createKeys(keyboard_id) {
pressKey(key.id); });
newDiv.addEventListener("mouseup", function () {
releaseKey(key.id); });
}
} else {
newDiv.className = "key blank";
}
@@ -372,7 +417,10 @@ function createMacros(macro_id) {
<body>
<div class="container">
<div id="keyboard"></div>
<div>
<div id="indicators"></div>
<div id="mouse"></div>
</div>
</div>
<div id="macros"></div>
+931 -791
View File
File diff suppressed because it is too large Load Diff
+28 -4
View File
@@ -13,7 +13,9 @@ void parse_key_list(char * keys) {
uint8_t keypos = 2; // bytes 2-7 are used for normal keys
uint8_t code = 0x00;
static unsigned char boot_report[8];
static unsigned char mouse_report[3];
memset(boot_report, 0x00, 8);
memset(mouse_report, 0x00, 3);
// Javascript sends the list as comma delimited, so split into individual
// keys by splitting at commas
@@ -27,20 +29,43 @@ void parse_key_list(char * keys) {
keypos++;
}
// if a scan code was not returned, it might be a modifier
// if a scan code was not returned, it might be a modifier or mouse
if (code == 0x00) {
// search for the correct modifier bit and add it to byte 0 of the
// USB boot keyboard report
if (code == 0x00) {
code = parse_mod(token);
if (code) {
boot_report[0] = boot_report[0] | code;
} else if ( strncmp(&token[0],"Mouse", 5) == 0 ){
if ( strncmp(&token[5], "Left", 4) == 0 ) {
mouse_report[1] = -1*MOUSE_SPEED;
} else if ( strncmp(&token[5], "Right", 5) == 0 ) {
mouse_report[1] = 1*MOUSE_SPEED;
} else if ( strncmp(&token[5], "Up", 2) == 0 ) {
mouse_report[2] = -1*MOUSE_SPEED;
} else if ( strncmp(&token[5], "Down", 4) == 0 ) {
mouse_report[2] = 1*MOUSE_SPEED;
} else if ( strncmp(&token[5], "ClickLeft", 9) == 0 ) {
mouse_report[0] = mouse_report[0] | 1;
} else if ( strncmp(&token[5], "ClickRight", 10) == 0) {
mouse_report[0] = mouse_report[0] | 2;
}
}
}
token = strtok(NULL, ",");
}
// print resulting HID boot mouse report to CDC for debugging
printf("Mouse report: ");
for(int i=0; i<3; i++){
printf("%02X ",mouse_report[i]);
}
printf("\n");
tud_hid_report(REPORT_ID_MOUSE, mouse_report, 3);
// print resulting HID boot keyboard report to CDC for debugging
printf("HID report: ");
printf("Keyboard report: ");
for(int i=0; i<8; i++){
printf("%02X ",boot_report[i]);
}
@@ -75,4 +100,3 @@ uint8_t parse_mod(char * key) {
}
return 0x00;
}
+2
View File
@@ -1,6 +1,8 @@
#ifndef PARSE_KEYS_H_
#define PRASE_KEYS_H_
#define MOUSE_SPEED 10
static unsigned char boot_buf[8];
void parse_key_list(char * keys);
+2 -1
View File
@@ -89,7 +89,8 @@ uint8_t const * tud_descriptor_device_cb(void)
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD))
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD)),
TUD_HID_REPORT_DESC_MOUSE( HID_REPORT_ID(REPORT_ID_MOUSE))
};
+1
View File
@@ -29,6 +29,7 @@ enum
{
REPORT_ID_KEYBOARD = 1,
REPORT_ID_CONSUMER_CONTROL,
REPORT_ID_MOUSE,
REPORT_ID_COUNT
};