chore: remove keyPressReportApiAvailable

This commit is contained in:
Siyuan Miao 2025-09-04 16:55:44 +02:00
parent 867ed88c6e
commit 8abcd1efe8
3 changed files with 5 additions and 23 deletions

View File

@ -461,8 +461,8 @@ export interface HidState {
keysDownState: KeysDownState; keysDownState: KeysDownState;
setKeysDownState: (state: KeysDownState) => void; setKeysDownState: (state: KeysDownState) => void;
keyPressReportApiAvailable: boolean; // keyPressReportApiAvailable is no longer needed, we'll simply use hidChannel available to
setkeyPressReportApiAvailable: (available: boolean) => void; // determine if the device supports keyPressReport
isVirtualKeyboardEnabled: boolean; isVirtualKeyboardEnabled: boolean;
setVirtualKeyboardEnabled: (enabled: boolean) => void; setVirtualKeyboardEnabled: (enabled: boolean) => void;
@ -481,9 +481,6 @@ export const useHidStore = create<HidState>(set => ({
keysDownState: { modifier: 0, keys: [0,0,0,0,0,0] } as KeysDownState, keysDownState: { modifier: 0, keys: [0,0,0,0,0,0] } as KeysDownState,
setKeysDownState: (state: KeysDownState): void => set({ keysDownState: state }), setKeysDownState: (state: KeysDownState): void => set({ keysDownState: state }),
keyPressReportApiAvailable: true,
setkeyPressReportApiAvailable: (available: boolean) => set({ keyPressReportApiAvailable: available }),
isVirtualKeyboardEnabled: false, isVirtualKeyboardEnabled: false,
setVirtualKeyboardEnabled: (enabled: boolean): void => set({ isVirtualKeyboardEnabled: enabled }), setVirtualKeyboardEnabled: (enabled: boolean): void => set({ isVirtualKeyboardEnabled: enabled }),

View File

@ -21,23 +21,15 @@ export default function useKeyboard() {
// dynamically set when the device responds to the first key press event or reports its // dynamically set when the device responds to the first key press event or reports its
// keysDownState when queried since the keyPressReport was introduced together with the // keysDownState when queried since the keyPressReport was introduced together with the
// getKeysDownState API. // getKeysDownState API.
const { keyPressReportApiAvailable, setkeyPressReportApiAvailable } = useHidStore();
const enableKeyPressReport = useCallback((reason: string) => {
if (keyPressReportApiAvailable) return;
console.debug(`Enable keyPressReport API because ${reason}`);
setkeyPressReportApiAvailable(true);
}, [setkeyPressReportApiAvailable, keyPressReportApiAvailable]);
// HidRPC is a binary format for exchanging keyboard and mouse events // HidRPC is a binary format for exchanging keyboard and mouse events
const { reportKeyboardEvent, reportKeypressEvent, rpcHidReady } = useHidRpc((message) => { const { reportKeyboardEvent, reportKeypressEvent, rpcHidReady } = useHidRpc((message) => {
switch (message.constructor) { switch (message.constructor) {
case KeysDownStateMessage: case KeysDownStateMessage:
setKeysDownState((message as KeysDownStateMessage).keysDownState); setKeysDownState((message as KeysDownStateMessage).keysDownState);
enableKeyPressReport("HidRPC:KeysDownStateMessage received");
break; break;
case KeyboardLedStateMessage: case KeyboardLedStateMessage:
setKeyboardLedState((message as KeyboardLedStateMessage).keyboardLedState); setKeyboardLedState((message as KeyboardLedStateMessage).keyboardLedState);
enableKeyPressReport("HidRPC:KeyboardLedStateMessage received");
break; break;
default: default:
break; break;
@ -57,7 +49,6 @@ export default function useKeyboard() {
if (rpcHidReady) { if (rpcHidReady) {
console.debug("Sending keyboard report via HidRPC"); console.debug("Sending keyboard report via HidRPC");
reportKeyboardEvent(state.keys, state.modifier); reportKeyboardEvent(state.keys, state.modifier);
enableKeyPressReport("HidRPC:KeyboardReport received");
return; return;
} }
@ -72,7 +63,6 @@ export default function useKeyboard() {
rpcHidReady, rpcHidReady,
send, send,
reportKeyboardEvent, reportKeyboardEvent,
enableKeyPressReport,
], ],
); );
@ -154,7 +144,7 @@ export default function useKeyboard() {
return; return;
} }
if (keyPressReportApiAvailable) { if (rpcHidReady) {
// if the keyPress api is available, we can just send the key press event // if the keyPress api is available, we can just send the key press event
sendKeypressEvent(key, press); sendKeypressEvent(key, press);
} else { } else {
@ -169,11 +159,10 @@ export default function useKeyboard() {
} }
}, },
[ [
keyPressReportApiAvailable, rpcHidReady,
keysDownState, keysDownState,
resetKeyboardState, resetKeyboardState,
rpcDataChannel?.readyState, rpcDataChannel?.readyState,
rpcHidReady,
sendKeyboardEvent, sendKeyboardEvent,
sendKeypressEvent, sendKeypressEvent,
reportKeypressEvent, reportKeypressEvent,

View File

@ -583,7 +583,6 @@ export default function KvmIdRoute() {
const { const {
keyboardLedState, setKeyboardLedState, keyboardLedState, setKeyboardLedState,
keysDownState, setKeysDownState, setUsbState, keysDownState, setKeysDownState, setUsbState,
setkeyPressReportApiAvailable
} = useHidStore(); } = useHidStore();
const [hasUpdated, setHasUpdated] = useState(false); const [hasUpdated, setHasUpdated] = useState(false);
@ -621,7 +620,6 @@ export default function KvmIdRoute() {
const downState = resp.params as KeysDownState; const downState = resp.params as KeysDownState;
console.debug("Setting key down state:", downState); console.debug("Setting key down state:", downState);
setKeysDownState(downState); setKeysDownState(downState);
setkeyPressReportApiAvailable(true); // if they returned a keyDownState, we know they also support keyPressReport
} }
if (resp.method === "otaState") { if (resp.method === "otaState") {
@ -698,7 +696,6 @@ export default function KvmIdRoute() {
if (resp.error.code === -32601) { if (resp.error.code === -32601) {
// if we don't support key down state, we know key press is also not available // if we don't support key down state, we know key press is also not available
console.warn("Failed to get key down state, switching to old-school", resp.error); console.warn("Failed to get key down state, switching to old-school", resp.error);
setkeyPressReportApiAvailable(false);
} else { } else {
console.error("Failed to get key down state", resp.error); console.error("Failed to get key down state", resp.error);
} }
@ -706,11 +703,10 @@ export default function KvmIdRoute() {
const downState = resp.result as KeysDownState; const downState = resp.result as KeysDownState;
console.debug("Keyboard key down state", downState); console.debug("Keyboard key down state", downState);
setKeysDownState(downState); setKeysDownState(downState);
setkeyPressReportApiAvailable(true); // if they returned a keyDownState, we know they also support keyPressReport
} }
setNeedKeyDownState(false); setNeedKeyDownState(false);
}); });
}, [keysDownState, needKeyDownState, rpcDataChannel?.readyState, send, setkeyPressReportApiAvailable, setKeysDownState]); }, [keysDownState, needKeyDownState, rpcDataChannel?.readyState, send, setKeysDownState]);
// When the update is successful, we need to refresh the client javascript and show a success modal // When the update is successful, we need to refresh the client javascript and show a success modal
useEffect(() => { useEffect(() => {