【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:切换到关于页
相关推荐
roamingcode5 小时前
我是如何 Vibe Coding,将 AI CLI 工具从 Node.js 迁移到 Rust 并成功发布的
人工智能·rust·node.js·github·claude·github copilot
初恋叫萱萱6 小时前
构建高性能生成式AI应用:基于Rust Axum与蓝耘DeepSeek-V3.2大模型服务的全栈开发实战
开发语言·人工智能·rust
superman超哥2 天前
Serde 性能优化的终极武器
开发语言·rust·编程语言·rust serde·serde性能优化·rust开发工具
sayang_shao2 天前
Rust多线程编程学习笔记
笔记·学习·rust
鸿乃江边鸟2 天前
Spark Datafusion Comet 向量化Rust Native--读数据
rust·spark·native·arrow
硬汉嵌入式2 天前
基于Rust构建的单片机Ariel RTOS,支持Cortex-M、RISC-V 和 Xtensa
单片机·rust·risc-v
低调滴开发3 天前
Tauri开发桌面端服务,配置指定防火墙端口
rust·tauri·桌面端·windows防火墙规则
咚为3 天前
Rust Cell使用与原理
开发语言·网络·rust
咸甜适中4 天前
rust的docx-rs库,自定义docx模版批量生成docx文档(逐行注释)
开发语言·rust·docx·docx-rs
FAFU_kyp4 天前
RISC0_ZERO项目在macOs上生成链上证明避坑
开发语言·后端·学习·macos·rust