Skip to content

Manic EMU is an all-in-one retro game emulator for iOS. It packs powerful features while keeping a clean, sleek UI and delivering buttery-smooth gameplay.

Swift
1.8 KB
Raw Blame
export const codeMap={Enter:13,Backspace:8,Delete:46,ArrowLeft:37,ArrowRight:39,ArrowUp:38,ArrowDown:40,Escape:27,Digit0:48,Digit1:49,Digit2:50,Digit3:51,Digit4:52,Digit5:53,Digit6:54,Digit7:55,Digit8:56,Digit9:57,Numpad0:96,Numpad1:97,Numpad2:98,Numpad3:99,Numpad4:100,Numpad5:101,Numpad6:102,Numpad7:103,Numpad8:104,Numpad9:105,NumpadDivide:111,NumpadMultiply:106,F1:112,F2:113,F3:114,Space:32,KeyA:65,KeyB:66,KeyC:67,KeyD:68,KeyE:69,KeyF:70,KeyG:71,KeyH:72,KeyI:73,KeyJ:74,KeyK:75,KeyL:76,KeyM:77,KeyN:78,KeyO:79,KeyP:80,KeyQ:81,KeyR:82,KeyS:83,KeyT:84,KeyU:85,KeyV:86,KeyW:87,KeyX:88,KeyY:89,KeyZ:90};export class KeyRepeatManager{static TIME_TO_FIRST_REPEAT=500;static REPEAT_INTERVAL=30;keyStates=new Map;listener=null;register(e){null===e||"function"==typeof e?this.listener=e:console.error("KeyRepeater.register: Provided callback is not a function or null.")}post(e,t,r={}){if(!t)return void console.error("KeyRepeater.post: 'key' argument is required.");const i=this.keyStates.get(t);if(e)if(i)i.args=r||{};else{const e={args:r||{},timeoutToFirstRepeatId:null,repeatIntervalId:null};this.keyStates.set(t,e),this.emit("down",t,e.args),e.timeoutToFirstRepeatId=setTimeout(()=>{this.emit("repeat",t,e.args),e.repeatIntervalId=setInterval(()=>{this.emit("repeat",t,e.args)},KeyRepeatManager.REPEAT_INTERVAL)},KeyRepeatManager.TIME_TO_FIRST_REPEAT)}else i&&(this.emit("up",t,i.args),null===i.repeatIntervalId?(clearTimeout(i.timeoutToFirstRepeatId),this.emit("click",t,i.args)):clearInterval(i.repeatIntervalId),this.keyStates.delete(t))}emit(e,t,r){if(this.listener)try{this.listener(e,t,r)}catch(e){console.error("Error in key event listener:",e)}}reset(){for(const e of this.keyStates.values())clearTimeout(e.timeoutToFirstRepeatId),clearInterval(e.repeatIntervalId);this.keyStates.clear()}}