adapte macos
This commit is contained in:
53
src/main/services/mediaShortcuts.ts
Normal file
53
src/main/services/mediaShortcuts.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { globalShortcut, type BrowserWindow } from 'electron'
|
||||
import { sendPlayerControl } from './playerControl'
|
||||
|
||||
/**
|
||||
* 媒体快捷键:
|
||||
* - 播放/暂停:全局 Super+P(macOS ⌘P / Windows Win+P),托盘后台也可用
|
||||
* - 快退/快进:仅窗口聚焦时 Super+← / Super+→,避免全局抢占方向键
|
||||
*
|
||||
* Electron 的 Super = Windows 徽标键 / macOS Command。
|
||||
* 注意:Windows 系统常占用 Win+P(投影),注册失败时会打日志。
|
||||
*/
|
||||
|
||||
const GLOBAL_PLAY_PAUSE = 'Super+P'
|
||||
|
||||
export function registerMediaShortcuts(): void {
|
||||
try {
|
||||
const ok = globalShortcut.register(GLOBAL_PLAY_PAUSE, () =>
|
||||
sendPlayerControl('playpause')
|
||||
)
|
||||
if (!ok) {
|
||||
console.warn(`[shortcuts] 注册失败(可能被系统占用): ${GLOBAL_PLAY_PAUSE}`)
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`[shortcuts] 注册异常: ${GLOBAL_PLAY_PAUSE}`, e)
|
||||
}
|
||||
}
|
||||
|
||||
export function unregisterMediaShortcuts(): void {
|
||||
try {
|
||||
globalShortcut.unregister(GLOBAL_PLAY_PAUSE)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
/** 窗口聚焦时的快退 / 快进(不注册全局,避免影响其它应用) */
|
||||
export function bindSeekShortcuts(window: BrowserWindow): void {
|
||||
window.webContents.on('before-input-event', (event, input) => {
|
||||
if (input.type !== 'keyDown') return
|
||||
// meta = macOS Command / Windows 徽标键
|
||||
if (!input.meta || input.control || input.alt) return
|
||||
|
||||
if (input.code === 'ArrowLeft') {
|
||||
event.preventDefault()
|
||||
sendPlayerControl('seek-backward')
|
||||
return
|
||||
}
|
||||
if (input.code === 'ArrowRight') {
|
||||
event.preventDefault()
|
||||
sendPlayerControl('seek-forward')
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user