【Rust GUI开发入门】编写一个本地音乐播放器(13. 实现按键绑定)

本系列教程对应的代码已开源在 Github zeedle

本篇文章介绍Slint UI如何为播放器页面添加按键绑定,核心思路是当窗口聚焦时,扫描用户按键输入,命中指定按键时,自动调用相关回调函数 。Slint UI支持FocusScope来处理上述逻辑:

slint 复制代码
export component MainWindow inherits Window {
    
    ...
    
    forward-focus: key-input-handler;
	
	...
	
    key-input-handler := FocusScope {
        key-released(event) => {
            if event.text == Key.Space {
                root.toggle_play();
                return accept;
            } else if event.text == Key.RightArrow || event.text == Key.DownArrow {
                root.play_next();
                return accept;
            } else if event.text == Key.LeftArrow || event.text == Key.UpArrow {
                root.play_prev();
                return accept;
            } else if event.text == Key.F1 {
                tabs.current-index = 0;
                return accept;
            } else if event.text == Key.F2 {
                tabs.current-index = 1;
                return accept;
            } else if event.text == Key.F3 {
                tabs.current-index = 2;
                return accept;
            } else if event.text == Key.F4 {
                tabs.current-index = 3;
                return accept;
            }
            return reject;
        }
    }
}

代码解释

上述代码指定了,当某个按键按下时,所触发的操作:

  • 空格键:播放/暂停当前歌曲
  • 上键头/左键头:上一首歌曲
  • 下箭头。右箭头:下一首歌曲
  • F1:切换到音乐列表页
  • F2:切换到歌词页
  • F3:切换到设置页
  • F4:切换到关于页
相关推荐
unconditional16 小时前
我拆了 sdkm 的自定义 SDK 机制:所谓“内置”,就是官方提前帮你填好的表
rust·开源
Ramble_Naylor1 天前
Rust深度解读:Send 与 Sync 的本质与线程安全边界
python·算法·rust
Ramble_Naylor1 天前
Rust深度解读:异步流(Async Streams)与生成器的处理艺术
开发语言·后端·rust
RobinDevNotes2 天前
Memvid 把 Agent 长期记忆塞进一个文件
人工智能·rust·openresty
miofly2 天前
GitHub 今日推荐|agent-desktop:通过无障碍树让 AI 代理直接操作桌面应用
rust·开源·github
geovindu2 天前
rust:N-Barrier Pattern
开发语言·后端·设计模式·rust·屏障模式·并发型模式
Ramble_Naylor2 天前
Rust深度解读:高性能 Web 网关与 Axum 架构剖析
前端·架构·rust
Hello.Reader2 天前
100 人同时操作一座三维城市,为什么不需要服务器渲染 6000 帧?Rust + wgpu 多端并发架构实战
服务器·架构·rust
遇码2 天前
2026-11-01-daw-retry-banner-body
人工智能·ai·rust·数据分析·大模型·agent
不积跬步无以至千里mmbb262 天前
rust_analysis
开发语言·后端·rust