将键盘输入发送到正在运行的 Linux 进程

发布于 2024-09-27 00:59:47 字数 260 浏览 1 评论 0原文

我正在为 mp3 播放器(mpg123 linux)开发网络界面。 mpg123 是一个命令行 mp3 播放器,可以使用键盘输入进行控制。例如:

$ mpg123 -C filename.mp3

它将开始播放歌曲并监视键盘输入以进行控制。按 's' 将暂停歌曲 'q' 以退出等。

我正在使用 Perl 脚本生成 mpg123 进程。我想从该脚本向该流程发送输入。我有进程的 pid,我只需要将击键发送到该进程以进行控制。

I am developing web interface for an mp3 player (mpg123 linux). The mpg123 is a command-line mp3 player and can be controlled using keyboard inputs. For example:

$ mpg123 -C filename.mp3

it will start playing the song and monitor keyboard inputs for control. Pressing 's' will pause the song 'q' for quit etc.

I am spawning an mpg123 process using a Perl script. From that script I want to send inputs to this process. I have the pid of the process, I just need to send keystrokes to this process for control purpose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-10-04 00:59:47

您只需从 Perl 中将 mp3 播放器作为管道生成即可。像这样:

$| = 1; # Set unbuffered output.
open( my $mp3player, "| mpg123" ) or die "cannot start mp3 player: $!";
print $mp3player "s";
...
print $mp3player "q";
close $mp3player

第二次尝试多个脚本调用:在交互式 shell 中输入 tty。这将为您提供一个伪终端名称。现在在这个 shell 中启动你的播放器。在另一个 shell 中,写入该伪终端。例如 echo "s" > /dev/pts/11。玩家将收到此信息作为输入。如果这有效,请使用 Perl 脚本而不是 echo 写入伪终端。

You just have to spawn your mp3-player as a pipe from perl. Like so:

$| = 1; # Set unbuffered output.
open( my $mp3player, "| mpg123" ) or die "cannot start mp3 player: $!";
print $mp3player "s";
...
print $mp3player "q";
close $mp3player

Second try for multiple script invocations: In an interactive shell enter tty. That will give you a pseudo-terminal name. Now start your player in this shell. In another shell, write to that pseudo-terminal. E.g. echo "s" > /dev/pts/11. The player will receive this as input. If this works, use your perl script instead of echo to write to the pseudo-terminal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文