How to use pushKeyEvent method in redwood

Best JavaScript code snippet using redwood

hotkeys.service.ts

Source:hotkeys.service.ts Github

copy

Full Screen

...66 const events = ['keydown', 'keyup']67 events.forEach(eventType => {68 document.addEventListener(eventType, (nativeEvent: KeyboardEvent) => {69 this._keyEvent.next(nativeEvent)70 this.pushKeyEvent(eventType, nativeEvent)71 if (hostApp.platform === Platform.Web && this.matchActiveHotkey(true) !== null) {72 nativeEvent.preventDefault()73 nativeEvent.stopPropagation()74 }75 })76 })77 })78 // deprecated79 this.hotkey$.subscribe(h => this.matchedHotkey.emit(h))80 this.matchedHotkey.subscribe = deprecate(s => this.hotkey$.subscribe(s), 'matchedHotkey is deprecated, use hotkey$')81 this.keyEvent$.subscribe(h => this.key.next(h))82 this.key.subscribe = deprecate(s => this.keyEvent$.subscribe(s), 'key is deprecated, use keyEvent$')83 }84 /**85 * Adds a new key event to the buffer86 *87 * @param eventName DOM event name88 * @param nativeEvent event object89 */90 pushKeyEvent (eventName: string, nativeEvent: KeyboardEvent): void {91 if (nativeEvent.timeStamp === this.lastEventTimestamp) {92 return93 }94 nativeEvent['event'] = eventName95 const eventData = {96 ctrlKey: nativeEvent.ctrlKey,97 metaKey: nativeEvent.metaKey,98 altKey: nativeEvent.altKey,99 shiftKey: nativeEvent.shiftKey,100 code: nativeEvent.code,101 key: nativeEvent.key,102 eventName,103 time: nativeEvent.timeStamp,104 registrationTime: performance.now(),105 }106 for (const [key, time] of this.pressedKeyTimestamps.entries()) {107 if (time < performance.now() - 2000) {108 this.removePressedKey(key)109 }110 }111 const keyName = getKeyName(eventData)112 if (eventName === 'keydown') {113 this.addPressedKey(keyName, eventData)114 if (!nativeEvent.repeat) {115 this.recognitionPhase = true116 }117 this.updateModifiers(eventData)118 }119 if (eventName === 'keyup') {120 const keystroke = getKeystrokeName([...this.pressedKeys])121 if (this.recognitionPhase) {122 this._keystroke.next(keystroke)123 this.lastKeystrokes.push({124 keystroke,125 time: performance.now(),126 })127 this.recognitionPhase = false128 }129 this.pressedKeys.clear()130 this.pressedKeyTimestamps.clear()131 this.removePressedKey(keyName)132 }133 if (this.pressedKeys.size) {134 this.pressedKeystroke = getKeystrokeName([...this.pressedKeys])135 } else {136 this.pressedKeystroke = null137 }138 const matched = this.matchActiveHotkey()139 this.zone.run(() => {140 if (matched && this.recognitionPhase) {141 this.emitHotkeyOn(matched)142 } else if (this.pressedHotkey) {143 this.emitHotkeyOff(this.pressedHotkey)144 }145 })146 this.zone.run(() => {147 this._key.next(getKeyName(eventData))148 })149 if (process.platform === 'darwin' && eventData.metaKey && eventName === 'keydown' && !['Ctrl', 'Shift', altKeyName, metaKeyName].includes(keyName)) {150 // macOS will swallow non-modified keyups if Cmd is held down151 this.pushKeyEvent('keyup', nativeEvent)152 }153 this.lastEventTimestamp = nativeEvent.timeStamp154 }155 getCurrentKeystrokes (): Keystroke[] {156 if (!this.pressedKeystroke) {157 return []158 }159 return [...this.lastKeystrokes.map(x => x.keystroke), this.pressedKeystroke]160 }161 matchActiveHotkey (partial = false): string|null {162 if (!this.isEnabled() || !this.pressedKeystroke) {163 return null164 }165 const matches: {...

Full Screen

Full Screen

optcarrot.worker.ts

Source:optcarrot.worker.ts Github

copy

Full Screen

...31 render: (image: Uint8Array) => void,32 playAudio: (audio: Int16Array) => void,33 progress: (input: ProgressInput) => void34 ): void;35 pushKeyEvent(code: number, pressed: boolean): void;36}37const OPTCARROT_HEADLESS_DRIVER = `38module Optcarrot39 # Audio output driver for Web Audio API40 class WebAudioAudio < Audio41 def tick(output)42 JS::eval("globalThis.Optcarrot.tickAudio()")43 end44 end45 # Video output driver for Web Canvas46 class CanvasVideo < Video47 def tick(screen)48 JS::eval("globalThis.Optcarrot.tickVideo()")49 end50 end51 BrowserInput = Input52end53`;54const OPTCARROT_WEB_DRIVER = `55module Optcarrot56 # Audio output driver for Web Audio API57 class WebAudioAudio < Audio58 def tick(output)59 bin = output.pack(@pack_format)60 File.binwrite(File.join("/OPTCARROT_TMP/audio.data"), bin)61 JS::eval("globalThis.Optcarrot.tickAudio()")62 rescue => e63 JS::eval("console.warn('#{ e.inspect }')")64 end65 end66 # Video output driver for Web Canvas67 class CanvasVideo < Video68 def init69 super70 @palette = @palette_rgb.map do |r, g, b|71 0xff000000 | (b << 16) | (g << 8) | r72 end73 end74 def dispose75 end76 def tick(screen)77 bin = screen.pack("L*")78 File.binwrite(File.join("/OPTCARROT_TMP", File.basename(@conf.video_output, ".EXT") + ".data"), bin)79 JS::eval("globalThis.Optcarrot.tickVideo()")80 super81 rescue => e82 JS::eval("console.warn('#{ e.inspect }')")83 end84 end85 # Input driver for browser input86 class BrowserInput < Input87 def init88 end89 def dispose90 end91 def tick(frame, pads)92 event = JS::eval("return globalThis.Optcarrot.fetchKeyEvent()").inspect93 return if event == ""94 code, pressed = event.split(",")95 code = code.to_i96 if pressed == "true"97 pads.keydown(0, code)98 else99 pads.keyup(0, code)100 end101 end102 end103end104`;105export class Optcarrot implements OptcarrotWorkerPort {106 wasmFs: WasmFs;107 wasi: WASI;108 vm: RubyVM;109 keyEventConsumer: KeyEventBuffer;110 tickVideo: () => void;111 tickAudio: () => void;112 remoteRender: (image: Uint8Array) => void;113 remotePlayAudio: (audio: Int16Array) => void;114 constructor() {115 this.wasmFs = new WasmFs();116 this.wasmFs.fs.mkdirSync("/OPTCARROT_TMP", 0o777);117 const args = ["ruby.wasm", "-e_=0"];118 this.wasi = new WASI({119 bindings: {120 ...WASI.defaultBindings,121 fs: this.wasmFs.fs,122 path,123 },124 args,125 preopenDirectories: {126 "/OPTCARROT_TMP": "/OPTCARROT_TMP",127 },128 });129 const originalWriteSync = this.wasmFs.fs.writeSync.bind(this.wasmFs.fs);130 // @ts-ignore131 this.wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {132 const text = new TextDecoder("utf-8").decode(buffer);133 const handlers = {134 1: (line) => console.log(line),135 2: (line) => console.warn(line),136 };137 if (handlers[fd]) handlers[fd](text);138 return originalWriteSync(fd, buffer, offset, length, position);139 };140 this.keyEventConsumer = new KeyEventBuffer([]);141 }142 computeOptions(options: Options): string[] {143 const optionsArray = [];144 const ROMS = {145 "Lan_Master.nes": "/optcarrot/examples/Lan_Master.nes",146 };147 if (options.opt) {148 optionsArray.push("--opt");149 }150 if (ROMS[options.rom]) {151 optionsArray.push(ROMS[options.rom]);152 } else {153 throw new Error(`Unknown ROM: ${options.rom}`);154 }155 return optionsArray;156 }157 async init(158 options: Options,159 render: (image: Uint8Array) => void,160 playAudio: (audio: Int16Array) => void,161 progress: (input: ProgressInput) => void162 ) {163 this.remoteRender = render;164 this.remotePlayAudio = playAudio;165 // Fetch and instantiate WebAssembly binary166 progress({ kind: "message", value: "Downloading..." });167 progress({ kind: "progress", value: 0 });168 const response = await fetch("./optcarrot.wasm");169 const buffer = await response.arrayBuffer();170 progress({ kind: "progress", value: 0.2 });171 progress({ kind: "message", value: "Instantiating Optcarrot..." });172 const imports = {173 wasi_snapshot_preview1: this.wasi.wasiImport,174 };175 const vm = new RubyVM();176 vm.addToImports(imports);177 // Instantiate the WebAssembly module178 const { instance } = await WebAssembly.instantiate(buffer, imports);179 await vm.setInstance(instance);180 progress({ kind: "progress", value: 0.3 });181 // Initialize WASI application182 this.wasi.setMemory(instance.exports.memory as WebAssembly.Memory);183 (instance.exports._initialize as any)();184 // Initialize Ruby VM185 vm.initialize();186 progress({ kind: "progress", value: 0.6 });187 console.time("init-optcarrot");188 console.log("Options:", options);189 vm.eval(`190 require "js"191 JS::eval("console.time('require-optcarrot')")192 require_relative "/optcarrot/lib/optcarrot.rb"193 JS::eval("console.timeEnd('require-optcarrot')")194 `);195 progress({ kind: "progress", value: 0.8 });196 this.tickVideo = options.headless197 ? () => {198 this.remoteRender(new Uint8Array());199 }200 : () => {201 const bytes = this.videoBytes();202 this.remoteRender(Comlink.transfer(bytes, [bytes.buffer]));203 };204 this.tickAudio = options.headless205 ? () => {206 this.remotePlayAudio(new Int16Array());207 }208 : () => {209 const bytes = this.audioBytes();210 this.remotePlayAudio(Comlink.transfer(bytes, [bytes.buffer]));211 };212 vm.eval(`213 ${options.headless ? OPTCARROT_HEADLESS_DRIVER : OPTCARROT_WEB_DRIVER}214 # Monkey patch the Optcarrot to use web drivers215 Optcarrot::Driver.define_singleton_method(:load) do |conf|216 video = Optcarrot::CanvasVideo.new(conf)217 audio = Optcarrot::WebAudioAudio.new(conf)218 input = Optcarrot::BrowserInput.new(conf, video)219 [video, audio, input]220 end221 args = [222 ${this.computeOptions(options)223 .map((option) => `"${option}"`)224 .join(", ")},225 "--audio-sample-rate=11050",226 ]227 JS::eval("console.time('Optcarrot::NES.new')")228 $nes = Optcarrot::NES.new(args)229 JS::eval("console.timeEnd('Optcarrot::NES.new')")230 `);231 progress({ kind: "progress", value: 1 });232 progress({ kind: "done" });233 vm.eval(`$nes.reset`);234 this.vm = vm;235 this.run();236 }237 run() {238 setTimeout(this.run.bind(this), 0);239 this.vm.eval(`$nes.step`);240 }241 videoBytes(): Uint8Array {242 return this.wasmFs.fs.readFileSync(243 "/OPTCARROT_TMP/video.data"244 ) as Uint8Array;245 }246 audioBytes(): Int16Array {247 const bytes = this.wasmFs.fs.readFileSync(248 "/OPTCARROT_TMP/audio.data"249 ) as Uint8Array;250 return new Int16Array(bytes.buffer);251 }252 pushKeyEvent(code: number, pressed: boolean): void {253 this.keyEventConsumer.push(code, pressed);254 }255 fetchKeyEvent(): string {256 const event = this.keyEventConsumer.consume();257 if (!event) return "";258 return event.join(",");259 }260}261const main = () => {262 const optcarrot = new Optcarrot();263 // @ts-ignore264 globalThis.Optcarrot = optcarrot;265 Comlink.expose({266 init(267 options: Options,268 render: (image: Uint8Array) => void,269 playAudio: (audio: Int16Array) => void,270 progress: (input: ProgressInput) => void271 ): void {272 try {273 optcarrot.init(options, render, playAudio, progress).catch((e) => {274 progress({275 kind: "error",276 message: "Failed to initialize Optcarrot: " + e.message,277 });278 });279 } catch (e) {280 progress({281 kind: "error",282 message: "Failed to initialize Optcarrot: " + e.message,283 });284 }285 },286 pushKeyEvent(code: number, pressed: boolean): void {287 optcarrot.pushKeyEvent(code, pressed);288 },289 });290};291if (292 // @ts-ignore293 typeof WorkerGlobalScope !== "undefined" &&294 // @ts-ignore295 self instanceof WorkerGlobalScope296) {297 main();...

Full Screen

Full Screen

plugin.js

Source:plugin.js Github

copy

Full Screen

...119 /**120 * Wrapper/Helper Methods121 */122 keyDown: function(modstate,sym,unicode,snd) {123 this.pushKeyEvent(1,modstate,sym,unicode,snd)124 },125 keyUp: function(modstate,sym,unicode,snd) {126 this.pushKeyEvent(0,modstate,sym,unicode,snd)127 },128 resize: function(width, height) {129 this.setWidth(width)130 this.setHeight(height)131 },132 /**133 * Random Shit134 */135 hsvToRgb: function(h, s, v) {136 var r, g, b;137 var i;138 var f, p, q, t;139 // Make sure our arguments stay in-range140 h = Math.max(0, Math.min(360, h));...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { pushKeyEvent } from '@redwoodjs/web'2 query FIND_POST_BY_ID($id: Int!) {3 post: post(id: $id) {4 }5 }6export const Loading = () => <div>Loading...</div>7export const Empty = () => <div>Post not found</div>8export const Success = ({ post }) => {9 const [isEditing, setIsEditing] = useState(false)10 const [title, setTitle] = useState(post.title)11 const [body, setBody] = useState(post.body)12 const onSave = () => {13 const { id } = post14 const input = { id, title, body }15 updatePost({ variables: { input } })16 setIsEditing(false)17 }18 const [updatePost, { loading, error }] = useMutation(UPDATE_POST_MUTATION, {19 onCompleted: () => {20 toast.success('Post updated')21 },22 })23 useEffect(() => {24 pushKeyEvent('Escape', () => {25 setIsEditing(false)26 })27 }, [])28 return (29 <h2>{post.title}</h2>30 {isEditing ? (31 value={title}32 onChange={(e) => setTitle(e.target.value)}33 value={body}34 onChange={(e) => setBody(e.target.value)}35 <button type="button" disabled={loading} onClick={onSave}>36 disabled={loading}37 onClick={() => setIsEditing(false)}38 ) : (39 <p>{post.body}</p>40 <button type="button" onClick={() => setIsEditing(true)}>41 )}42}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { pushKeyEvent } from '@redwoodjs/testing'2pushKeyEvent('keydown', 'ArrowUp')3pushKeyEvent('keydown', 'ArrowDown')4pushKeyEvent('keydown', 'ArrowLeft')5pushKeyEvent('keydown', 'ArrowRight')6pushKeyEvent('keydown', 'Enter')7pushKeyEvent('keydown', 'Escape')8pushKeyEvent('keydown', ' ')9pushKeyEvent('keydown', 'a')10### `pushMouseEvent(type, options)`11import { pushMouseEvent } from '@redwoodjs/testing'12pushMouseEvent('click', {13})14### `render(ui, { wrapper: Wrapper, ...options })`15import { render } from '@redwoodjs/testing'16import { MyComponent } from './MyComponent'17describe('MyComponent', () => {18 it('renders successfully', () => {19 expect(() => {20 render(<MyComponent />)21 }).not.toThrow()22 })23})24import { render, screen } from '@redwoodjs/testing'25import { MyComponent } from './MyComponent'26describe('MyComponent', () => {27 it('renders successfully', () => {28 render(<MyComponent />

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var device = redwood.getDevice();3device.pushKeyEvent('KEYCODE_HOME', 'DOWN');4device.pushKeyEvent('KEYCODE_HOME', 'UP');5device.pushKeyEvent('KEYCODE_MENU', 'DOWN');6device.pushKeyEvent('KEYCODE_MENU', 'UP');7device.pushKeyEvent('KEYCODE_BACK', 'DOWN');8device.pushKeyEvent('KEYCODE_BACK', 'UP');9device.pushKeyEvent('KEYCODE_VOLUME_UP', 'DOWN');10device.pushKeyEvent('KEYCODE_VOLUME_UP', 'UP');11device.pushKeyEvent('KEYCODE_VOLUME_DOWN', 'DOWN');12device.pushKeyEvent('KEYCODE_VOLUME_DOWN', 'UP');13device.pushKeyEvent('KEYCODE_VOLUME_MUTE', 'DOWN');14device.pushKeyEvent('KEYCODE_VOLUME_MUTE', 'UP');15device.pushKeyEvent('KEYCODE_POWER', 'DOWN');16device.pushKeyEvent('KEYCODE_POWER', 'UP');17device.pushKeyEvent('KEYCODE_CAMERA', 'DOWN');18device.pushKeyEvent('KEYCODE_CAMERA', 'UP');19device.pushKeyEvent('KEYCODE_SEARCH', 'DOWN');20device.pushKeyEvent('KEYCODE_SEARCH', 'UP');21device.pushKeyEvent('KEYCODE_ENTER', 'DOWN');22device.pushKeyEvent('KEYCODE_ENTER', 'UP');23device.pushKeyEvent('KEYCODE_ESCAPE', 'DOWN');24device.pushKeyEvent('KEYCODE_ESCAPE', 'UP');25device.pushKeyEvent('KEYCODE_DPAD_UP', 'DOWN');26device.pushKeyEvent('KEYCODE_DPAD_UP', 'UP');27device.pushKeyEvent('KEYCODE_DPAD_DOWN', 'DOWN');28device.pushKeyEvent('KEYCODE_DPAD_DOWN', 'UP');29device.pushKeyEvent('KEYCODE_DPAD_LEFT', 'DOWN');30device.pushKeyEvent('KEYCODE_DPAD_LEFT', 'UP');31device.pushKeyEvent('KEYCODE_DPAD_RIGHT', 'DOWN');32device.pushKeyEvent('KEYCODE_DPAD_RIGHT', 'UP');33device.pushKeyEvent('KEYCODE_DPAD_CENTER', 'DOWN');34device.pushKeyEvent('KEYCODE_DPAD_CENTER', 'UP');35device.pushKeyEvent('KEYCODE_MOVE_HOME', 'DOWN');36device.pushKeyEvent('KEYCODE_MOVE_HOME', 'UP');37device.pushKeyEvent('KEYCODE_MOVE_END', 'DOWN');38device.pushKeyEvent('KEYCODE_MOVE_END', 'UP');39device.pushKeyEvent('KEYCODE_PAGE_UP', 'DOWN');40device.pushKeyEvent('KEYCODE_PAGE_UP', 'UP');41device.pushKeyEvent('KEYCODE_PAGE_DOWN', 'DOWN');42device.pushKeyEvent('KEYCODE_PAGE_DOWN', 'UP');43device.pushKeyEvent('

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var device = new redwood.Device();3device.connect();4device.on('connected', function() {5 console.log('Device connected');6 device.pushKeyEvent(66, 0);7 device.pushKeyEvent(66, 1);8 device.pushKeyEvent(66, 0);9});10device.on('disconnected', function() {11 console.log('Device disconnected');12});13device.on('error', function(err) {14 console.log('Error: ' + err);15});

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('./redwood');2var fs = require('fs');3var path = require('path');4var util = require('util');5var eventEmitter = require('events').EventEmitter;6var redwood = new redwood();7var redwood = new redwood();

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var redwoodClient = redwood.createClient({3});4var key = 0x1C;5var event = 0x01;6redwoodClient.pushKeyEvent(key, event, function(err, result) {7 if (err) {8 console.log(err);9 } else {10 console.log(result);11 }12});

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var rw = new redwood.RedwoodClient();3rw.connect(function(err) {4 if (err) {5 console.log("error connecting to redwood server");6 return;7 }8 rw.pushKeyEvent('keydown', 65, function(err) {9 if (err) {10 console.log("error sending key event");11 return;12 }13 console.log("key event sent");14 });15});

Full Screen

Using AI Code Generation

copy

Full Screen

1var KEY_LEFT = 37;2var KEY_UP = 38;3var KEY_RIGHT = 39;4var KEY_DOWN = 40;5var currentKey = null;6var redwood = null;7function init() {8 redwood = new Redwood();9 var canvas = document.getElementById("canvas");10 canvas.addEventListener("keydown", keyDown, false);11 canvas.addEventListener("keyup", keyUp, false);12}13function keyDown(event) {14 if (currentKey == null) {15 currentKey = event.keyCode;16 redwood.pushKeyEvent(currentKey);17 }18}19function keyUp(event) {20 currentKey = null;21}22function unload() {23 redwood.disconnect();24}25window.addEventListener("load", init, false);26window.addEventListener("unload", unload, false);27var KEY_RIGHT = 39;28var KEY_DOWN = 40;29var currentKey = null;30var redwood = null;31function init() {32 redwood = new Redwood();33 var canvas = document.getElementById("canvas");34 canvas.addEventListener("keydown", keyDown, false);35 canvas.addEventListener("keyup", keyUp, false);36}37function keyDown(event) {38 if (currentKey == null) {39 currentKey = event.keyCode;40 redwood.pushKeyEvent(currentKey);41 }42}43function keyUp(event) {44 currentKey = null;45}46function unload() {47 redwood.disconnect();48}49window.addEventListener("load", init, false);50window.addEventListener("unload", unload, false);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful