Skip to content

Commit

Permalink
Add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 31, 2023
1 parent 8c24df9 commit 46e7928
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apps/virtual-keyboard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*.nro
/node_modules
/romfs/main.js
/romfs/main.js.map
17 changes: 17 additions & 0 deletions apps/virtual-keyboard/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "virtual-keyboard",
"version": "0.0.0",
"private": true,
"description": "nx.js app that invokes the virtual keyboard",
"scripts": {
"build": "esbuild --bundle --sourcemap --sources-content=false --target=es2020 --format=esm src/main.ts --outfile=romfs/main.js",
"nro": "nxjs-pack"
},
"license": "MIT",
"devDependencies": {
"esbuild": "^0.17.19",
"nxjs-constants": "workspace:^",
"nxjs-pack": "workspace:^",
"nxjs-runtime": "workspace:^"
}
}
52 changes: 52 additions & 0 deletions apps/virtual-keyboard/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Hid, Swkbd } from 'nxjs-constants';
const { Button } = Hid;

const canvas = Switch.screen;
const ctx = Switch.screen.getContext('2d');
const vk = navigator.virtualKeyboard;

function render() {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.font = '24px system-ui';
ctx.fillStyle = '#444';
ctx.fillText('Press "ZL" to show num keyboard', 10, 32);
ctx.fillText('Press "ZR" to show text keyboard', 10, 68);

ctx.font = '32px system-ui';
ctx.fillStyle = 'black';
ctx.fillText(`${vk.value}`, 10, 120);

const { width } = ctx.measureText(vk.value.slice(0, vk.cursorIndex));
ctx.fillStyle = 'green';
ctx.fillRect(13 + width, 90, 3, 32);
}

Switch.addEventListener('buttondown', (e) => {
const isOpen = vk.boundingRect.height > 0;
if (isOpen) {
if (e.detail & Button.Plus) {
e.preventDefault();
} else if (e.detail & Button.ZL) {
vk.hide();
}
} else {
if (e.detail & Button.ZR) {
vk.type = Swkbd.Type.Normal;
vk.okButtonText = 'Done';
vk.show();
} else if (e.detail & Button.ZL) {
vk.type = Swkbd.Type.NumPad;
vk.okButtonText = 'Submit';
vk.leftButtonText = ':';
vk.rightButtonText = '.';
vk.show();
}
}
});

vk.addEventListener('change', render);
vk.addEventListener('cursormove', render);
vk.addEventListener('geometrychange', render);
render();
16 changes: 16 additions & 0 deletions apps/virtual-keyboard/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es2020",
"moduleResolution": "node",
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"types": [
"nxjs-runtime"
]
},
"include": [
"src/**/*.ts"
]
}
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46e7928

Please sign in to comment.