重新映射 ubuntu 中的“下一首歌曲”键以与 spotify 和 wine 配合使用

重新映射 ubuntu 中的“下一首歌曲”键以与 spotify 和 wine 配合使用

我想在 ubuntu 中使用全局键在 spotify 中更改歌曲。在 Windows 上可以使用键盘上的特殊键“下一首歌曲键”(?) 来实现

我如何重新映射 ubuntu 中的某个键(这里没有“下一首歌曲”按钮)以用作“下一首歌曲”按钮?比如“ctrl +右键”。

我该如何让其与通过 wine 运行的 spotify 一起工作呢?

基本上我想知道“下一首歌曲”按钮的 ASCII 码是什么,以及如何将其映射到 ubuntu 下的组合键。并希望它能够顺利地通过 wine 路由此命令。

编辑:我的 ubuntu 机器上没有“下一首歌曲”按钮。

答案1

或者杰邦尼找到了一个使用 dbus-send 的简洁解决方案他的要点如下

或者如果你很着急:

"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause" XF86AudioPlay
"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop" XF86AudioStop
"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next" XF86AudioNext
"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous" XF86AudioPrevious

答案2

对于 Ubuntu,现在有一个“Linux 版 Spotify“。要将组合键映射到“下一首歌曲”,dconf-editor 可能会有所帮助(看这里)。一旦完成“下一首歌曲”的键映射,就会出现一个很好的Spotify 包装器用于 John Reese 编写的媒体密钥集成。

编辑:Linux 版 Spotify 现在支持媒体键。

答案3

我这样做的方法是使用以下 bash 脚本,它是我使用从其他来源借用的各种命令编写的。您只是要求“playpause”功能,但我认为我最好包括整个功能。您想要的部分是:

#!/bin/bash

# Spotify Launch Script by Ben Kraft

# Borrows some commands from Spotify Control by Tommy Matilla:
# http://sites.google.com/site/tommymattila/home/spotifycontrol
# which itself is based on Stuart Colville's
# http://muffinresearch.co.uk/archives/2009/10/22/ubuntu-lock-screen-and-pause-spotify/

# Requires wmctrl and xvkbd

WP="$HOME/.wine"
SPATH="$HOME/.wine/drive_c/Program Files/Spotify/spotify.exe"
TITLE=$(wmctrl -xl | grep -o -e "spotify\.exe\.Wine.*$" | grep -o -e "Spotify.*$")
ACTIVEWIN=$(wmctrl -va ":ACTIVE:" 2>&1 | grep -o -e "0x.*$")

sendkeys () {
    wmctrl -xa "spotify.exe.Wine" || return 1
    xvkbd -q -delay 100 -text "$1"
    wmctrl -ia $ACTIVEWIN
}

if [ -z $TITLE ] ; then
  echo "Spotify is not running!
  exit 1
else
  sendkeys '\ '
fi
exit 0

简而言之,它用于wmctrl移动到 Spotify 所在的工作区,用于xvkbd向 Spotify 发送空格键击,然后用于wmctrl移回。

要使用它,请将其保存在文件中,将文件标记为可执行(在文件权限对话框中),然后使用 CompizConfig 设置管理器将其添加到命令中。(将其放在bash /path/to/script“命令”选项卡的第一个框中,然后在第二个选项卡中添加键绑定。这可以是您想要使用的任何键。)您将需要wmctrlxvkbd安装;在 Ubuntu 软件中心搜索它们。

整个脚本如下所示,它提供了一些其他不错的功能,例如转到上一首或下一首曲目以及显示歌曲标题。您可以像使用上面的代码片段一样使用它,只是您必须bash /path/to/script --option在命令框中输入--option脚本中列出的任何内容。

#!/bin/bash

# Spotify Launch Script by Ben Kraft

# Borrows some commands from Spotify Control by Tommy Matilla:
# http://sites.google.com/site/tommymattila/home/spotifycontrol
# which itself is based on Stuart Colville's
# http://muffinresearch.co.uk/archives/2009/10/22/ubuntu-lock-screen-and-pause-spotify/

# Requires wmctrl and xvkbd
# Usage:
USAGE="
usage: spotify [OPTIONS]

OPTIONS:
--play: Starts Spotify playing if it isn't already
--pause: Pauses Spotify if it isn't already
--playpause: Toggles Spotify between playing and pausing
--prev: Plays the previous song
--next: Plays the next song
--display: Prints the currently playing song and artist to stdout
--notify-send: Use only *after* --display; also pops up a notification with currently playing song and artist
--uri URI: tells Spotify to display URI (e.g. a playlist or user).

With no options, this script will kill a currently running instance of spotify if there is one.  With --uri, it starts Spotify iff it isn't running.  With any other option, it will return 1 if Spotify is not running.
"

WP="$HOME/.wine"
SPATH="$HOME/.wine/drive_c/Program Files/Spotify/spotify.exe"
TITLE=$(wmctrl -xl | grep -o -e "spotify\.exe\.Wine.*$" | grep -o -e "Spotify.*$")
ACTIVEWIN=$(wmctrl -va ":ACTIVE:" 2>&1 | grep -o -e "0x.*$")

sendkeys () {
    wmctrl -xa "spotify.exe.Wine" || return 1
    xvkbd -q -delay 100 -text "$1"
    wmctrl -ia $ACTIVEWIN
}

if [ $# -eq 0 ] ; then
    if pgrep spotify.exe &>/dev/null ; then
        killall spotify.exe
    fi
    exec env WINEPREFIX="$WP" wine "$SPATH"
elif [ "$1" == "--uri" ] ; then
    shift
    exec env WINEPREFIX="$WP" wine "$SPATH" /uri "$@"
elif [ -z "$TITLE" ] ; then
    echo "Spotify is not running" >&2
    if [ "$@" = "--display --notify-send" ] ; then
        notify-send "Spotify is not running"
    fi
    exit 1
else
    case "$1" in
          --playpause) #toggles
        sendkeys '\ '
        ;;

          --next)
        sendkeys '\C\[Right]'
        ;;

          --prev)
        sendkeys '\C\[Left]'
        ;;

            --pause) #pauses if playing; does nothing if not
        if [ -n "${TITLE:9}" ] ; then
            sendkeys '\ '
        fi
        ;;

            --play) #plays if paused; does nothing if not
        if [ -z "${TITLE:9}" ] ; then
            sendkeys '\ '
        fi
        ;;

          --display)
        if [ -n "${TITLE:9}" ] ; then
            OUT1="Now Playing"
            OUT2="${TITLE:9}"
        else
            OUT1="Spotify paused."
            OUT2=""
        fi
        echo "$OUT"
        if [ "$#" -ge 2 ] && [ $2 == "--notify-send" ] ; then
            notify-send "$OUT1" "$OUT2"
        fi
        ;;
            *)
        echo "$USAGE"
        exit 2
        ;;
    esac
fi

exit 0

答案4

Pop!_OS 20.4+ 说明:

  1. 禁用 Chrome贪婪媒体密钥
  2. 在键盘设置中设置按键。

这可能适用于其他版本的 Ubuntu/Gnome。

快捷方式的屏幕截图

相关内容