【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:切换到关于页
相关推荐
fqbqrr14 小时前
2510rs,rust清单1
rust
熊猫钓鱼>_>14 小时前
Rust语言特性深度解析:所有权、生命周期与模式匹配之我见
算法·rust·软件开发·函数·模式匹配·异步编程·质量工具
fqbqrr14 小时前
2510rs,rust清单2
rust
Source.Liu14 小时前
【pulldown-cmark】 初学者指南
rust·markdown·pulldown-cmark
呼啦啦嘎嘎15 小时前
《100 Exercises To Learn Rust》练习笔记
rust
Amos_Web16 小时前
Rust实战课程--网络资源监控器(初版)
前端·后端·rust
WujieLi1 天前
初识 Vite+:一文了解 Rust 驱动的新一代前端工具链
javascript·rust·vite
std860211 天前
Rust 与 Python – 这是未来的语言吗?
开发语言·python·rust
std78792 天前
Rust 与 Go – 比较以及每个如何满足您的需求
开发语言·golang·rust
Amos_Web2 天前
Rust实战教程--文件管理命令行工具
前端·rust·全栈