Skip to content

Commit 3aec062

Browse files
author
while
committed
手柄
1 parent 39074d4 commit 3aec062

9 files changed

Lines changed: 213 additions & 48 deletions

File tree

‎packages/app/src/App.vue‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import { computed, onBeforeUnmount, onMounted, ref, watchEffect } from 'vue'
33
import { exit } from '@tauri-apps/plugin-process'
44
import {
5-
PAD_BUTTON_LIST,
6-
codeLabel,
75
NesScreen,
86
RomStorePanel,
97
SaveStatePanel,
@@ -17,6 +15,7 @@ import {
1715
isTv,
1816
useAutoHideToolbar,
1917
useFullscreen,
18+
useInputMonitor,
2019
useRemoteNav,
2120
type SaveState,
2221
} from '@nes-emulator/player'
@@ -81,13 +80,8 @@ if (typeof document !== 'undefined') {
8180
const modKey: 'ctrl' | 'shift' = isTauri ? 'ctrl' : 'shift'
8281
const modLabel = isTauri ? 'Ctrl' : 'Shift'
8382
84-
// 按键说明:展示玩家1的键盘映射,改键后自动同步。
85-
const keyHint = computed(() => {
86-
const keymap = settings.players[0].keymap
87-
return PAD_BUTTON_LIST.filter((i) => keymap[i.btn])
88-
.map((i) => `${i.label}=${codeLabel(keymap[i.btn])}`)
89-
.join(' · ')
90-
})
83+
// 调试:当前按下的按键(键盘/手柄/遥控),仅在设置开启时显示于状态栏。
84+
const { label: inputLabel } = useInputMonitor()
9185
9286
async function openRom() {
9387
const picked = await pickRomFile()
@@ -340,9 +334,11 @@ onBeforeUnmount(() => {
340334
</Transition>
341335

342336
<footer class="footer">
343-
<span class="keys">键盘 {{ keyHint }}</span>
344337
<button class="link-btn" @click="helpOpen = true">查看所有快捷键</button>
345338
<span v-if="navEnabled && !hasGamepad" class="tv-tip">游戏操作需连接手柄;遥控器用于菜单导航,返回键退出</span>
339+
<span v-if="settings.misc.showInputDebug" class="input-debug">
340+
按键 <b>{{ inputLabel || '—' }}</b>
341+
</span>
346342
<span class="meta">{{ romName ?? '未载入' }} · {{ isTauri ? 'Tauri App' : '浏览器预览' }}</span>
347343
</footer>
348344
</div>
@@ -536,13 +532,19 @@ textarea,
536532
color: #aaa;
537533
font-size: 12px;
538534
}
539-
.footer .keys {
540-
letter-spacing: 0.2px;
541-
}
542535
.footer .meta {
543536
color: #777;
544537
margin-left: auto;
545538
}
539+
/* 调试:当前按下的按键 */
540+
.footer .input-debug {
541+
color: #888;
542+
font-family: ui-monospace, 'Cascadia Code', monospace;
543+
}
544+
.footer .input-debug b {
545+
color: #6ab0ff;
546+
font-weight: 600;
547+
}
546548
.footer .tv-tip {
547549
color: #6ab0ff;
548550
}

‎packages/player/src/components/NesScreen.vue‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
buildCodeToButton,
1414
buildPadToButton,
1515
ensurePadProfile,
16+
GAMEPAD_AXIS_THRESHOLD,
1617
isPadSignalActive,
1718
isTurbo,
1819
listGamepads,
@@ -371,6 +372,14 @@ function pollGamepads() {
371372
for (const sig in map) {
372373
if (isPadSignalActive(pad, sig)) now.add(map[sig])
373374
}
375+
// 左摇杆(axes[0]=X,axes[1]=Y)始终兼作方向键,与十字键并存:
376+
// NES 方向映射默认只绑 D-pad,这里叠加摇杆,让摇杆/十字键都能操作方向。
377+
const ax = pad.axes[0] ?? 0
378+
const ay = pad.axes[1] ?? 0
379+
if (ax < -GAMEPAD_AXIS_THRESHOLD) now.add(PadButton.Left)
380+
else if (ax > GAMEPAD_AXIS_THRESHOLD) now.add(PadButton.Right)
381+
if (ay < -GAMEPAD_AXIS_THRESHOLD) now.add(PadButton.Up)
382+
else if (ay > GAMEPAD_AXIS_THRESHOLD) now.add(PadButton.Down)
374383
}
375384
const prev = padActive[pi] ?? new Set<number>()
376385
now.forEach((pb) => {

‎packages/player/src/components/RomStorePanel.vue‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -631,22 +631,23 @@ function formatError(err: unknown): string {
631631
min-height: 0;
632632
overflow: auto;
633633
padding: 12px 18px 18px;
634+
display: grid;
635+
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
636+
gap: 10px;
637+
align-content: start;
634638
}
635639
.game-card {
636-
display: grid;
637-
grid-template-columns: minmax(0, 1fr) auto;
638-
align-items: center;
639-
gap: 16px;
640+
display: flex;
641+
flex-direction: column;
642+
gap: 10px;
640643
padding: 12px;
641644
border: 1px solid #333942;
642645
border-radius: 8px;
643646
background: #252a31;
644647
}
645-
.game-card + .game-card {
646-
margin-top: 8px;
647-
}
648648
.game-main {
649649
min-width: 0;
650+
flex: 1;
650651
}
651652
.game-title-row {
652653
display: flex;
@@ -731,6 +732,7 @@ function formatError(err: unknown): string {
731732
cursor: default;
732733
}
733734
.empty {
735+
grid-column: 1 / -1;
734736
margin: 40px 0;
735737
color: #9da5b3;
736738
text-align: center;
@@ -791,7 +793,7 @@ function formatError(err: unknown): string {
791793
.error {
792794
text-align: left;
793795
}
794-
.game-card {
796+
.game-list {
795797
grid-template-columns: 1fr;
796798
}
797799
.game-actions {
@@ -854,15 +856,15 @@ function formatError(err: unknown): string {
854856
}
855857
.game-list {
856858
padding: 6px 12px 8px;
859+
grid-template-columns: 1fr;
860+
gap: 6px;
857861
}
858862
.game-card {
859-
grid-template-columns: minmax(0, 1fr) auto;
863+
flex-direction: row;
864+
align-items: center;
860865
padding: 6px 8px;
861866
gap: 8px;
862867
}
863-
.game-card + .game-card {
864-
margin-top: 6px;
865-
}
866868
.game-title-row h3 {
867869
font-size: 13px;
868870
}

‎packages/player/src/components/SettingsModal.vue‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ onBeforeUnmount(() => {
347347
<option v-for="h in TURBO_HZ_OPTIONS" :key="h" :value="h">{{ h }} 次/秒</option>
348348
</select>
349349
</label>
350+
<label class="row">
351+
<span>显示按下的按键<br /><small class="hint-text">调试用,状态栏实时显示键盘/手柄/遥控按键</small></span>
352+
<input v-model="settings.misc.showInputDebug" type="checkbox" />
353+
</label>
350354
<button class="reset" @click="resetSettings">恢复全部默认设置</button>
351355
</div>
352356
</section>
@@ -454,6 +458,10 @@ onBeforeUnmount(() => {
454458
color: #888;
455459
font-size: 12px;
456460
}
461+
.hint-text {
462+
color: #888;
463+
font-size: 11px;
464+
}
457465
.row {
458466
display: flex;
459467
align-items: center;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* 输入调试监视:在 window 层捕获所有按键(键盘、遥控器、实体手柄)并汇成一行
3+
* 文本,供状态栏实时显示,方便排查按键映射 / 遥控器键值 / 手柄按钮编号。
4+
*
5+
* 与游戏输入(NesScreen)完全解耦:直接挂 window 监听 + 轮询 gamepad,不受模态
6+
* 锁定 / 是否已载入游戏影响,因此连未绑定到游戏的键(遥控器 BACK、手柄空闲键等)
7+
* 也能看到。仅在 `settings.misc.showInputDebug` 开启时才附加监听,关闭时零开销。
8+
*
9+
* 显示取原始标识而非友好名 —— 键盘用 `KeyboardEvent.code`(遥控器特殊键 code 为空时
10+
* 退回 key),手柄用 `b{n}` / `a{n}±` —— 这些正是按键映射录入所用的标识,调试最直接。
11+
*/
12+
import { onBeforeUnmount, onMounted, ref, watch, type Ref } from 'vue'
13+
import { GAMEPAD_AXIS_THRESHOLD, settings } from '../emulator/settings'
14+
15+
export interface InputMonitorApi {
16+
/** 当前按下的所有按键描述,未按任何键时为空串(全部松开后短暂保留再清空)。 */
17+
label: Ref<string>
18+
}
19+
20+
/** 全部松开后保留最后一次显示的时长(ms),便于看清瞬时按下的键。 */
21+
const HOLD_MS = 900
22+
23+
export function useInputMonitor(): InputMonitorApi {
24+
const label = ref('')
25+
// 当前按下的键 -> 显示文本;保持插入顺序(≈按下先后)。
26+
// 键盘条目键为 `k:{id}`,手柄条目键为 `g:{index}:{sig}`。
27+
const pressed = new Map<string, string>()
28+
let raf = 0
29+
let clearTimer = 0
30+
31+
function refresh() {
32+
const next = [...pressed.values()].join(' ')
33+
if (next) {
34+
if (clearTimer) {
35+
clearTimeout(clearTimer)
36+
clearTimer = 0
37+
}
38+
if (next !== label.value) label.value = next
39+
} else if (label.value && !clearTimer) {
40+
// 全松开:保留片刻再清,避免瞬按一闪而过看不清。
41+
clearTimer = window.setTimeout(() => {
42+
label.value = ''
43+
clearTimer = 0
44+
}, HOLD_MS)
45+
}
46+
}
47+
48+
function onKeyDown(e: KeyboardEvent) {
49+
const id = e.code || e.key
50+
if (!id) return
51+
const key = `k:${id}`
52+
if (pressed.has(key)) return // 键盘自动重复:幂等
53+
pressed.set(key, `⌨ ${id}`)
54+
refresh()
55+
}
56+
function onKeyUp(e: KeyboardEvent) {
57+
const id = e.code || e.key
58+
if (pressed.delete(`k:${id}`)) refresh()
59+
}
60+
61+
// 手柄无事件,每帧轮询所有手柄的按钮 / 轴,按边沿维护 pressed 里的 `g:` 条目。
62+
function pollPads() {
63+
raf = requestAnimationFrame(pollPads)
64+
const pads = typeof navigator !== 'undefined' && navigator.getGamepads ? navigator.getGamepads() : []
65+
const now = new Set<string>()
66+
for (const pad of pads) {
67+
if (!pad) continue
68+
for (let i = 0; i < pad.buttons.length; i++) {
69+
const b = pad.buttons[i]
70+
if (b && (b.pressed || b.value > 0.5)) {
71+
const key = `g:${pad.index}:b${i}`
72+
now.add(key)
73+
if (!pressed.has(key)) pressed.set(key, `🎮#${pad.index} b${i}`)
74+
}
75+
}
76+
for (let i = 0; i < pad.axes.length; i++) {
77+
const v = pad.axes[i]
78+
if (v > GAMEPAD_AXIS_THRESHOLD) {
79+
const key = `g:${pad.index}:a${i}+`
80+
now.add(key)
81+
if (!pressed.has(key)) pressed.set(key, `🎮#${pad.index} a${i}+`)
82+
} else if (v < -GAMEPAD_AXIS_THRESHOLD) {
83+
const key = `g:${pad.index}:a${i}-`
84+
now.add(key)
85+
if (!pressed.has(key)) pressed.set(key, `🎮#${pad.index} a${i}-`)
86+
}
87+
}
88+
}
89+
// 移除已松开的手柄条目(键盘条目由 keyup 维护,不在此清理)。
90+
for (const key of pressed.keys()) {
91+
if (key.startsWith('g:') && !now.has(key)) pressed.delete(key)
92+
}
93+
refresh()
94+
}
95+
96+
// 用捕获阶段监听,确保先于游戏/模态逻辑收到,且我们不 preventDefault,不干扰原有行为。
97+
function start() {
98+
window.addEventListener('keydown', onKeyDown, true)
99+
window.addEventListener('keyup', onKeyUp, true)
100+
if (!raf) raf = requestAnimationFrame(pollPads)
101+
}
102+
function stop() {
103+
window.removeEventListener('keydown', onKeyDown, true)
104+
window.removeEventListener('keyup', onKeyUp, true)
105+
if (raf) {
106+
cancelAnimationFrame(raf)
107+
raf = 0
108+
}
109+
if (clearTimer) {
110+
clearTimeout(clearTimer)
111+
clearTimer = 0
112+
}
113+
pressed.clear()
114+
label.value = ''
115+
}
116+
117+
watch(
118+
() => settings.misc.showInputDebug,
119+
(on) => (on ? start() : stop()),
120+
)
121+
122+
onMounted(() => {
123+
if (settings.misc.showInputDebug) start()
124+
})
125+
onBeforeUnmount(stop)
126+
127+
return { label }
128+
}

‎packages/player/src/composables/useRemoteNav.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ function onKeyDown(e: KeyboardEvent): void {
259259
const typing = isTextEntry(active)
260260
const multilineTyping = isMultilineTextEntry(active)
261261

262+
// OK/确定键:各家 TV/机顶盒遥控器键值不统一 —— 标准 Enter,部分设备报 'Select'/'Accept',
263+
// Android DPAD_CENTER 常为 keyCode 23(WebView 里 e.key 未必是 'Enter')。统一在此识别,
264+
// 否则中间确认键按下无反应(能上下左右移焦点却无法"按下"开始游戏)。
265+
if (e.key === 'Enter' || e.key === 'Select' || e.key === 'Accept' || e.keyCode === 13 || e.keyCode === 23) {
266+
if (typing) return // 文本框内回车交原生(如提交/换行)
267+
e.preventDefault()
268+
top.ok()
269+
return
270+
}
271+
262272
switch (e.key) {
263273
case 'ArrowUp':
264274
if (multilineTyping) return
@@ -280,11 +290,6 @@ function onKeyDown(e: KeyboardEvent): void {
280290
e.preventDefault()
281291
top.move('right')
282292
return
283-
case 'Enter':
284-
if (typing) return // 文本框内回车交原生(如提交/换行)
285-
e.preventDefault()
286-
top.ok()
287-
return
288293
case 'Escape':
289294
case 'Backspace':
290295
case 'GoBack':

‎packages/player/src/emulator/settings.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export interface Settings {
7575
speed: number
7676
/** 连发频率(次/秒)。 */
7777
turboHz: number
78+
/** 调试:在状态栏实时显示当前按下的按键(键盘/手柄/遥控)。 */
79+
showInputDebug: boolean
7880
}
7981
}
8082

@@ -200,6 +202,7 @@ function defaultSettings(): Settings {
200202
touchPad: 'auto',
201203
speed: 1,
202204
turboHz: 16,
205+
showInputDebug: false,
203206
},
204207
}
205208
}

‎packages/player/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
} from './composables/useRemoteNav'
1515
export { useFullscreen, type FullscreenApi } from './composables/useFullscreen'
1616
export { useAutoHideToolbar, type AutoHideToolbarApi } from './composables/useAutoHideToolbar'
17+
export { useInputMonitor, type InputMonitorApi } from './composables/useInputMonitor'
1718
export { Button, NesRunner, SCREEN, type SaveState } from './emulator/runner'
1819
export {
1920
createSaveState,

0 commit comments

Comments
 (0)