@@ -99,7 +99,7 @@ function readGamepad() {
99
99
}
100
100
}
101
101
102
- export const listener = ( ) => {
102
+ export const listenerGamepad = ( ) => {
103
103
if ( [ ...navigator . getGamepads ( ) ] . find ( ( e ) => e ?. connected ) ) {
104
104
readGamepad ( ) ;
105
105
} else {
@@ -111,46 +111,43 @@ export const listener = () => {
111
111
} ;
112
112
113
113
export const startKeyboardSimulation = ( ) => {
114
+ const map = Object . entries ( {
115
+ // scroll
116
+ w : GamepadBtnIndex . Up ,
117
+ a : GamepadBtnIndex . Left ,
118
+ s : GamepadBtnIndex . Down ,
119
+ d : GamepadBtnIndex . Right ,
120
+ // select/start
121
+ f : GamepadBtnIndex . Select ,
122
+ h : GamepadBtnIndex . Start ,
123
+ // detail/switch
124
+ j : GamepadBtnIndex . B ,
125
+ k : GamepadBtnIndex . A ,
126
+ space : GamepadBtnIndex . A ,
127
+ // exit
128
+ 4 : GamepadBtnIndex . FrontLeftTop ,
129
+ // settings
130
+ 5 : GamepadBtnIndex . FrontRightTop ,
131
+ // page/tab navigation
132
+ 6 : GamepadBtnIndex . FrontLeftBottom ,
133
+ 7 : GamepadBtnIndex . FrontRightBottom ,
134
+ } ) ;
114
135
addEventListener (
115
136
'keydown' ,
116
- hotkeys ( {
117
- // scroll
118
- w : ( ) => dispatchPressEvent ( GamepadBtnIndex . Up ) ,
119
- a : ( ) => dispatchPressEvent ( GamepadBtnIndex . Left ) ,
120
- s : ( ) => dispatchPressEvent ( GamepadBtnIndex . Down ) ,
121
- d : ( ) => dispatchPressEvent ( GamepadBtnIndex . Right ) ,
122
- // select/start
123
- f : ( ) => dispatchPressEvent ( GamepadBtnIndex . Select ) ,
124
- h : ( ) => dispatchPressEvent ( GamepadBtnIndex . Start ) ,
125
- // detail/switch
126
- j : ( ) => dispatchPressEvent ( GamepadBtnIndex . B ) ,
127
- k : ( ) => dispatchPressEvent ( GamepadBtnIndex . A ) ,
128
- space : ( ) => dispatchPressEvent ( GamepadBtnIndex . A ) ,
129
- // exit
130
- 4 : ( ) => dispatchPressEvent ( GamepadBtnIndex . FrontLeftTop ) ,
131
- // settings
132
- 5 : ( ) => dispatchPressEvent ( GamepadBtnIndex . FrontRightTop ) ,
133
- // page/tab navigation
134
- 6 : ( ) => dispatchPressEvent ( GamepadBtnIndex . FrontLeftBottom ) ,
135
- 7 : ( ) => dispatchPressEvent ( GamepadBtnIndex . FrontRightBottom ) ,
136
- } ) ,
137
+ hotkeys (
138
+ map . reduce (
139
+ ( p , [ key , btn ] ) => Object . assign ( p , { [ key ] : ( ) => dispatchPressEvent ( btn ) } ) ,
140
+ { } as Record < string , ( ) => void > ,
141
+ ) ,
142
+ ) ,
137
143
) ;
138
144
addEventListener (
139
145
'keyup' ,
140
- hotkeys ( {
141
- w : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . Up ) ,
142
- a : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . Left ) ,
143
- s : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . Down ) ,
144
- d : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . Right ) ,
145
- f : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . Select ) ,
146
- h : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . Start ) ,
147
- j : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . B ) ,
148
- k : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . A ) ,
149
- space : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . A ) ,
150
- 4 : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . FrontLeftTop ) ,
151
- 5 : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . FrontRightTop ) ,
152
- 6 : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . FrontLeftBottom ) ,
153
- 7 : ( ) => dispatchReleaseEvent ( GamepadBtnIndex . FrontRightBottom ) ,
154
- } ) ,
146
+ hotkeys (
147
+ map . reduce (
148
+ ( p , [ key , btn ] ) => Object . assign ( p , { [ key ] : ( ) => dispatchReleaseEvent ( btn ) } ) ,
149
+ { } as Record < string , ( ) => void > ,
150
+ ) ,
151
+ ) ,
155
152
) ;
156
153
} ;
0 commit comments