Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keyboard shortcut support for the mcdu remote display in browser mode #86

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions apps/mcdu/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import './assets/css/App.css';
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import useWebSocket, { ReadyState } from 'react-use-websocket';
import { McduScreen } from './components/McduScreen';
import { McduButtons } from './components/McduButtons';
import { WebsocketContext } from './WebsocketContext';
import { McduKeyboardEvents } from './McduKeyboardEvents';
import darkBg from './assets/images/mcdu-a32nx-dark.png';
import bg from './assets/images/mcdu-a32nx.png';

Expand Down Expand Up @@ -43,7 +44,7 @@ const App = () => {
// automaticaly upgrate to wss if the page is served over https
if (window.location.protocol === 'https:') {
socketUrl = socketUrl.replace('ws', 'wss');
};
}

const [content, setContent] = useState(
{
Expand Down Expand Up @@ -79,6 +80,13 @@ const App = () => {
reconnectInterval: 500,
});

const { onKeyboardInput } = new McduKeyboardEvents(sendMessage);
const rootPanelRef = useRef(null);

useEffect(() => {
rootPanelRef.current.focus();
}, []);

useEffect(() => {
if (readyState === ReadyState.OPEN) {
sendMessage('requestUpdate');
Expand All @@ -100,7 +108,7 @@ const App = () => {
}

return (
<>
<div ref={rootPanelRef} tabIndex={-1} onKeyDown={onKeyboardInput}>
{!fullscreen && (
<>
<div className="normal">
Expand Down Expand Up @@ -151,7 +159,7 @@ const App = () => {
</div>
</>
)}
</>
</div>
);
};

Expand Down
71 changes: 71 additions & 0 deletions apps/mcdu/src/McduKeyboardEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const McduFunctionalKeys = {
Tab: 'DIR',
Insert: 'PROG',
Home: 'PERF',
PageUp: 'INIT',
Enter: 'DATA',
NumpadEnter: 'DATA',
Delete: 'FPLN',
End: 'RAD',
PageDown: 'FUEL',
Escape: 'MENU',
ShiftLeft: 'AIRPORT',
ArrowLeft: 'PREVPAGE',
ArrowRight: 'NEXTPAGE',
ArrowUp: 'UP',
ArrowDown: 'DOWN',
Backspace: 'CLR',
Space: 'SP',
Minus: 'PLUSMINUS',
NumpadSubtract: 'PLUSMINUS',
NumpadAdd: 'PLUSMINUS',
Period: 'DOT',
NumpadDecimal: 'DOT',
NumpadDivide: 'DIV',
Slash: 'DIV',
NumpadMultiply: 'OVFY',
};

export class McduKeyboardEvents {
constructor(socketSender) {
this.socketSender = socketSender;
this.mcduFunctionalKeys = McduFunctionalKeys;
}

getMcduKey = (keyEvent) => {
// match mcdu L/R row input for F keys
if (keyEvent.code.match(/F\d+/)) {
const fn = parseInt(keyEvent.code.replace('F', ''));
return fn <= 6 ? `L${fn}` : `R${fn - 6}`;
}

// match a-z
if (keyEvent.code.match(/Key[A-Z]/)) {
return keyEvent.code.replace('Key', '').toLocaleUpperCase();
}

// match 0-9
if (keyEvent.code.match(/(Digit|Numpad)\d/i)) {
return keyEvent.code.replace(/Digit|Numpad/i, '');
}

// match mcdu function keys
return this.mcduFunctionalKeys[keyEvent.code];
}

onKeyboardInput = (keyEvent) => {
//console.log('event', { key: keyEvent.key, code: keyEvent.code });
const key = this.getMcduKey(keyEvent);

if (key) {
keyEvent.preventDefault();
keyEvent.stopPropagation();
} else {
return;
}

//console.log(`mcdu key: ${key}`);

this.socketSender(`event:left:${key}`);
}
}
3 changes: 3 additions & 0 deletions apps/mcdu/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<div id="root">
<script type='text/javascript' src="index.js"></script>
<script>
// window close prompt
window.onbeforeunload = () => { return false; }

// Workaround for a webkit "feature" where 100vh is not actually 100vh
function appHeight() {
const doc = document.documentElement;
Expand Down
Loading