程式運行時禁用按鍵

程式運行時禁用按鍵

將問題重複至如何在特定程式運行時停用按鍵?,從未得到答覆。 (哪個選項更糟糕,是我重新發布相同的問題,還是我把舊帖子串起來?)

無論如何,有沒有辦法在特定程式運行時禁用特定按鍵?或者,當特定程式運行時禁用 Dash?

答案1

簡單的解決方案

建立兩個快捷方式,一種用於停用Super金鑰,一種用於隨意恢復金鑰。

進入系統設定 -> 鍵盤 -> 捷徑 -> 自訂,然後按一下+按鈕。將新快捷方式命名為「停用 Dash」。命令是

 gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ show-launcher 'Disabled'

若要建立重新啟用腳本的快捷方式,請重複上述步驟,但命令應該是

 gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ show-launcher '<Super>'

腳本解決方案

Super當使用者指定的程式具有焦點時,下面的腳本將停用該鍵。請注意,使用者仍然可以用滑鼠點擊破折號圖示來呼叫破折號。程式名稱必須用單引號引起來,並且與 Unity Launcher 中顯示的名稱完全相同。可以以相同的格式指定多個窗口,以空格分隔

在此輸入影像描述

例如,要在每次 Firefox 視窗獲得焦點時停用 Super 鍵,必須將腳本呼叫為

disable_super_key.sh 'Firefox Web Browser'

禁用兩者firefoxgnome-terminal執行以下操作

disable_super_key.sh 'Firefox Web Browser' 'Terminal'

如何取得腳本

使用者可以在此處複製原始程式碼,也可以按照以下說明從 github 取得原始碼:

  1. sudo apt-get install git
  2. cd /opt ; sudo git clone https://github.com/SergKolo/sergrep.git
  3. sudo chmod -R +x sergrep

該腳本將位於/opt/sergrep/disable_super_key.sh

若要讓腳本在每次登入時自動啟動,請參閱如何在登入時自動啟動應用程式?。提供/opt/sergrep/disable_super_key.sh(完整路徑)作為命令

腳本來源

#!/usr/bin/env bash
#
###########################################################
# Author: Serg Kolo , contact: [email protected] 
# Date: April 12 , 2016
# Purpose: Disable super key that brings up Unity Dash
#          per specific application
# 
# Written for: https://askubuntu.com/q/754884/295286
# Tested on: Ubuntu 14.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#    
#     Permission to use, copy, modify, and distribute this software is hereby granted
#     without fee, provided that  the copyright notice above and this permission statement
#     appear in all copies.
#
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
#     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#     DEALINGS IN THE SOFTWARE.

ARGV0="$0"
ARGC=$#
enable_dash_key()
{
  gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ show-launcher '<Super>'
}

disable_dash_key()
{
gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ show-launcher 'Disabled'
}



get_active_app()
{
   qdbus org.ayatana.bamf \
        /org/ayatana/bamf/matcher \
        org.ayatana.bamf.matcher.ActiveApplication
}

get_active_app_name()
{
  qdbus org.ayatana.bamf \
   $(get_active_app)   \
   org.ayatana.bamf.view.Name
}

check_active_app()
{
  active_name=$(get_active_app_name)
  local is_found
  for win in  "${windows_list[@]}"
  do
    if [ "$active_name" = "$win" ] ; then
      is_found=true
      break
    else
      is_found=false
    fi
  done

  if $is_found ; then
     disable_dash_key
  else
     enable_dash_key
  fi
}


print_usage()
{
cat << EOF
Copyright Serg Kolo , 2016

Usage: disable_super_key.sh 'App Name 1' [ 'App Name 2' 'App Name 3' ...  ]

The script disables the Super key for the specified set of applications
under Ubuntu's Unity environment. The list of windows must be space
separated, each app name single quoted and exactly as it appears on the
launcher (or as it appears in the .desktop file of that app), so spelling
and spacing matter.

Note that the script only disables the Super key as shortcut for Dash.
The user still will be able to invoke Dash by manually clicking on the 
Ubuntu icon in the launcher
EOF
}

main()
{

  if [ $ARGC -eq 0   ]; then
     print_usage
     exit
  fi

  local windows_list
  windows_list=( "$@" )
  dbus-monitor --profile "type='signal',member='FocusedWindowChanged'" |\
  while read line
  do
     case "$line" in
       *FocusedWindowChanged*) check_active_app ;;
     esac         
  done
}

main "$@"

相關內容