Skip to content

Commit

Permalink
publish: 6.22.32
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Aug 28, 2019
1 parent eb4d0b7 commit 61a6e36
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 6 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,27 @@ export default JsDos;

Then you can use it as simple html tag ```<JsDos />```

### Keyboard in iframe

Normally when you use js-dos in iframe keyboard should work. But remember that iframe must be focused
to receive keyboard events. By default when you click on iframe it recives focus, but if you use
mouse event listener with prevent default at the end, then iframe will not recieve focus. You can solve
this problem by calling `iframe.focus()` function.

### Simulating keyboard events

You can easily simulate key press and release using [CommandInterface.simulateKeyEvent(keyCode: number, pressed: boolean)](https://js-dos.com/6.22/docs/api/generate.html?page=js-dos-ci). Or you can use shortcut for single event `simulateKeyPress(keyCode: number)`, like in example:

```javascript
Dos(canvas).ready((fs, main) => {
fs.extract("digger.zip").then(() => {
main(["-c", "DIGGER.COM"]).then((ci) => {
ci.simulateKeyPress(37); // left arrow
})
});
});
```

## Building

Building process have two steps:
Expand Down
4 changes: 2 additions & 2 deletions dist/docs/api/js-dos-ts/js-dos-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ gulpfile.js --> generateBuildInfo
```
export const Build = {
version: "6.22.31 (c994d52c63db2dbfb069d76356df78e0)",
jsVersion: "97110c66c9e0731fa6728b56e7e944f3c5f5a144",
version: "6.22.32 (b41b4a60d500f0255d9de52283a44732)",
jsVersion: "eb4d0b7c1dbef664e89bcae806ae531f2cd1a712",
jsSize: 196977,
wasmVersion: "03ff8f9208bc11b041bebc7cce39e56d",
wasmSize: 1810179,
Expand Down
71 changes: 71 additions & 0 deletions dist/docs/api/js-dos-ts/js-dos-ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,77 @@ resolves when commands sequence is executed
return -1;
}
```







* `simulateKeyEvent(keyCode, pressed)` - allows to simulate key press OR release on js-dos canvas




```
public simulateKeyEvent(keyCode: number, pressed: boolean): void {
const name = pressed ? "keydown" : "keyup";
const event = document.createEvent("KeyboardEvent") as any;
const getter: any = {
get() {
return this.keyCodeVal;
},
};
```







Chromium Hack




```
Object.defineProperty(event, "keyCode", getter);
Object.defineProperty(event, "which", getter);
Object.defineProperty(event, "charCode", getter);
event.initKeyboardEvent
? event.initKeyboardEvent(name, true, true, document.defaultView, false, false, false, false, keyCode, keyCode)
: event.initKeyEvent(name, true, true, document.defaultView, false, false, false, false, keyCode, 0);
event.keyCodeVal = keyCode;
this.dos.canvas && this.dos.canvas.dispatchEvent(event);
}
```







* `simulateKeyPress(keyCode)` - allows to simulate key press AND release on js-dos canvas




```
public simulateKeyPress(keyCode: number): void {
this.simulateKeyEvent(keyCode, true);
setTimeout(() => this.simulateKeyEvent(keyCode, false), 100);
}
private sendKeyPress(code: number) {
this.api.send("sdl_key_event", code + "");
}
Expand Down
4 changes: 2 additions & 2 deletions js-dos-ts/js-dos-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// gulpfile.js --> generateBuildInfo

export const Build = {
version: "6.22.31 (c994d52c63db2dbfb069d76356df78e0)",
jsVersion: "97110c66c9e0731fa6728b56e7e944f3c5f5a144",
version: "6.22.32 (b41b4a60d500f0255d9de52283a44732)",
jsVersion: "eb4d0b7c1dbef664e89bcae806ae531f2cd1a712",
jsSize: 196977,
wasmVersion: "03ff8f9208bc11b041bebc7cce39e56d",
wasmSize: 1810179,
Expand Down
29 changes: 29 additions & 0 deletions js-dos-ts/js-dos-ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ export class DosCommandInterface {
return -1;
}

// * `simulateKeyEvent(keyCode, pressed)` - allows to simulate key press OR release on js-dos canvas
public simulateKeyEvent(keyCode: number, pressed: boolean): void {
const name = pressed ? "keydown" : "keyup";
const event = document.createEvent("KeyboardEvent") as any;
const getter: any = {
get() {
return this.keyCodeVal;
},
};

// Chromium Hack
Object.defineProperty(event, "keyCode", getter);
Object.defineProperty(event, "which", getter);
Object.defineProperty(event, "charCode", getter);

event.initKeyboardEvent
? event.initKeyboardEvent(name, true, true, document.defaultView, false, false, false, false, keyCode, keyCode)
: event.initKeyEvent(name, true, true, document.defaultView, false, false, false, false, keyCode, 0);

event.keyCodeVal = keyCode;
this.dos.canvas && this.dos.canvas.dispatchEvent(event);
}

// * `simulateKeyPress(keyCode)` - allows to simulate key press AND release on js-dos canvas
public simulateKeyPress(keyCode: number): void {
this.simulateKeyEvent(keyCode, true);
setTimeout(() => this.simulateKeyEvent(keyCode, false), 100);
}

private sendKeyPress(code: number) {
this.api.send("sdl_key_event", code + "");
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-dos",
"version": "6.22.31",
"version": "6.22.32",
"description": "Easiest API to run dos programs in browser",
"main": "dist/js-dos.js",
"types": "dist/typescript/js-dos.ts",
Expand Down
5 changes: 4 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<script>
Dos(document.getElementById("jsdos")).ready((fs, main) => {
fs.extract("test/digger.zip").then(() => {
main(["-c", "DIGGER.COM"]).then((ci) => window.Module = ci.dos);
main(["-c", "DIGGER.COM"]).then((ci) => {
window.Module = ci.dos;
window.ci = ci;
});
});
});
</script>
Expand Down
19 changes: 19 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,25 @@ test("js-dos can run digger.zip", (done) => {
});
});

test("js-dos can simulate key events", (done) => {
const dos = Dos(document.getElementById("canvas") as HTMLCanvasElement, {
wdosboxUrl: "/wdosbox.js",
});

doReady(dos, (fs, main) => {
doNext(fs.extract("digger.zip"), () => {
doNext(main(["DIGGER.COM"]), (ci) => {
ci.simulateKeyPress(37); // left arrow
const fn = () => {
compareAndExit("digger-end.png", ci, done);
};

setTimeout(fn, 5000);
});
});
});
});

const saveImage = (ci: DosCommandInterface) => {
ci.screenshot().then((data) => {
const w = window.open("about:blank", "image from canvas");
Expand Down

0 comments on commit 61a6e36

Please sign in to comment.