export interface State { up : number ; down : number ; left : number ; right : number ; forward : number ; back : number ; pitchup : number ; pitchdown : number ; } export const state = { up : 0 , down : 0 , left : 0 , right : 0 , forward : 0 , back : 0 , pitchup : 0 , pitchdown : 0 } ; const keyPressListeners = { } ; function setState ( k , s ) { const cs = state ; if ( k === 37 ) cs . left = s ; else if ( k === 39 ) cs . right = s ; else if ( k === 38 ) cs . forward = s ; else if ( k === 40 ) cs . back = s ; else if ( k === 87 ) cs . up = s ; else if ( k === 83 ) cs . down = s ; else if ( k === 81 ) cs . pitchup = s ; else if ( k === 65 ) cs . pitchdown = s ; } function onKeyDown ( ev ) { setState ( ev . keyCode , 1.0 ) ; const code = ev . keyCode . toString ( ) ; if ( keyPressListeners . hasOwnProperty ( code ) ) { keyPressListeners [ code ] ( ) ; } } function onKeyUp ( ev ) { setState ( ev . keyCode , 0.0 ) ; } let initialized = false ; export function init ( ) { if ( initialized ) return ; document . addEventListener ( 'keydown' , onKeyDown , true ) ; document . addEventListener ( 'keyup' , onKeyUp , true ) ; initialized = true ; } export function setKeyPressListener ( code , fn ) { keyPressListeners [ code . toString ( ) ] = fn ; }