【问题标题】:Use backtick/Grave/Tilde as modifier key使用反引号/坟墓/波浪号作为修饰键
【发布时间】:2015-11-17 21:22:27
【问题描述】:

我正在寻找一种在 Linux 中使用反引号 (`) / 波浪号 (~) 键和其他键创建键盘快捷键的方法。理想情况下:

  1. 按下波浪号没有任何作用
  2. 按下波浪号时按下另一个键会触发(可自定义)快捷方式
  3. 在释放波浪号之前/不按其他键时,只需发送波浪号击键即可。

我在 AutoHotKey for Windows 中有类似的东西,我一直在寻找一种在(任何)Linux 环境中重新创建它的方法。如果我可以让它工作,我会考虑使用任何 GUI,但当然更“通用”的解决方案会更好。

【问题讨论】:

    标签: linux user-interface shortcut modifier tilde


    【解决方案1】:

    我想我终于明白了!

    我使用 xmodmapgrave 键转换为修饰符Hyper_L,并使用XCape 发送坟墓,如果在没有按下另一个键的情况下释放该键。

    Xcape 旨在在没有其他键的情况下按下并释放meta-键(“windows 键”)时打开应用程序菜单(“开始菜单”),因此作为额外的奖励,它也可以这样做。这意味着您可以使用Meta 作为修饰符,例如Meta-F 打开文件管理器并分别使用meta-key 打开胡须菜单。

    如果一切正常,您可以使用~-k 打开键盘设置管理器,并且可以使用~-键创建新的快捷键。因为这仍然很烦人并且不容易在不同系统之间移植,所以我已经包含了一些使用 xfconf-query 的快捷方式,它们可能只适用于 Xfce。

    这是我的脚本的基础知识:

    #!/bin/sh
    
    # reset pretty much ALL keyboard settings 
    setxkbmap
    
    # Free up the mod3 and mod4 flags from all keys it may be associated with:
    xmodmap -e "clear mod3"
    xmodmap -e "clear mod4"
    
    # Add Hyper_L to the grave key (49)
    xmodmap -e "keycode 49 = Hyper_L asciitilde grave asciitilde"
    
    # You need a grave key somewhere else (!) so, bind it to an unused key:
    xmodmap -e "keycode 250 = grave"
    
    # Restore Mod4 but without Hyper_L (which was at location 4)
    xmodmap -e "add mod4 = Super_L Super_R Super_L"
    
    # Assign the mod3 to Hyper_L:
    xmodmap -e "add mod3 = Hyper_L"
    
    dist=100
    /usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Right -s "xdotool mousemove_relative -- $dist 0" --create -t string
    /usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Down  -s "xdotool mousemove_relative -- 0 $dist" --create -t string
    /usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Left  -s "xdotool mousemove_relative -- -$dist 0" --create -t string
    /usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>Up    -s "xdotool mousemove_relative -- 0 -$dist" --create -t string
    /usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>space -s "xdotool click 1" --create -t string
    
    /usr/bin/xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/\<Hyper\>k -s "xfce4-keyboard-settings" --create -t string
    
    # (re)starting xcape to produce a ` after key-up if no other key was pressed
    killall xcape
    xcape -t5000 -e "#49=grave;Super_L=Control_L|Escape" &
    

    可以在here 找到更多扩展版本的脚本,其中包含更多快捷方式。

    【讨论】:

    • 这对我有用,但是当我按下 shift+grave 时,我会同时得到一个波浪号和反引号字符,如下所示: ~` 。有什么想法吗?
    【解决方案2】:

    在另一台机器上切换到 Ubuntu 后,我还想像在 AHK 脚本中那样使用波浪号作为修饰键。

    我对 ex 的不同工具进行了相当多的研究。 xdotool、xev、autotools、xbindkeys 等等,终于找到了解决方案。步骤如下。

    1. 安装 Autokey、python、python evded 模块、xte(sudo apt-get install xautomation)。
    2. 阅读一些关于 Autokey 以及它如何启动 python 脚本或创建热字串的信息。在 Autokey 中,我们可以设置一个热键来调用 python 脚本。因此,您可以将下面的 python 脚本分配给您的波浪号键或您计划创建的任何自定义热键。
    3. 这是所需的自定义功能(还没有完全移植到 linux,我已经用 autohotkey 编写了脚本,很喜欢它。它可以让手粘在键盘上;))
      • 波浪号 + 向上箭头:将鼠标指针向上移动 100 个位置
      • 波浪号 + 向下箭头:将鼠标指针向下移动 100 个位置
      • 波浪号 + 右箭头:将鼠标指针向右移动 100 个位置
      • 波浪号 + 向左箭头:将鼠标指针向左移动 100 个位置
      • 波浪号 + Enter:鼠标左键单击(python 脚本中不存在)
      • 波浪号 + Alt + Enter:鼠标右键
    4. 我使用波浪号 (KEY_GRAVE) 作为修饰键。按下此键时,Autokey 会启动 python 脚本。脚本会循环运行,直到释放波浪号键。在循环中,脚本继续检测键盘输入。在向上箭头键 (KEY_UP) 按下时,脚本会使用“xte”等发送命令以相对位置(0,-100)移动鼠标。 from evdev import InputDevice, categorize, ecodes from select import select dev = InputDevice('/dev/input/event4') releasekey = False while releasekey==False: r,w,x = select([dev], [], []) for event in dev.read(): if event.type == ecodes.EV_KEY: #system.exec_command("xte 'mousermove 0 3'", False) #break if event.code == ecodes.KEY_UP: if event.value == 1: system.exec_command("xte 'mousermove 0 -100'", False) if event.code == ecodes.KEY_DOWN: if event.value == 1: system.exec_command("xte 'mousermove 0 100'", False) if event.code == ecodes.KEY_RIGHT: if event.value == 1: system.exec_command("xte 'mousermove 100 0'", False) if event.code == ecodes.KEY_LEFT: if event.value == 1: system.exec_command("xte 'mousermove -100 0'", False)
      if event.code == ecodes.KEY_GRAVE: if event.value == 0: releasekey = True break
    5. 您必须调整 dev = InputDevice('/dev/input/event4') 行以分配正确的键盘名称。就我而言, event4 是我的键盘。你的可能不一样。您可以在 python-evdev 上查看方便的教程“阅读事件”。该代码实际上输出 /dev/input 下列出的键盘名称。实际上,我的脚本是该教程脚本的扩展。
    6. 唯一的问题是python脚本必须以root用户身份启动,否则无法访问键盘输入设备。您可以通过创建一个 udev 规则文件来克服这个问题,该文件更改设备的权限以使其可用于读写,例如。创建一个规则文件并添加这一行 KERNEL=='event4', MODE="0660" 并加载规则。最后,您必须将自己添加到对设备具有读/写权限的 GROUP 中。有关文件权限的信息可以在 /dev/input 文件夹中使用 ls -la 找到。

    我希望它对你有用。起初它不起作用,然后喝杯咖啡并继续战斗直到它起作用;)

    【讨论】:

    • 非常感谢,我最终使用了不同的方法,但你确实在这方面帮助了我很多!
    【解决方案3】:

    我不确定它是否适合你,但你应该检查一下:

    这两种工具都可以让您创建一些自定义操作和快捷方式。

    这里是 xdotool 的一个例子:https://askubuntu.com/questions/212154/create-a-custom-shortcut-that-types-clipboard-contents

    希望对你有帮助,祝你好运:)

    布鲁诺

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 2013-10-13
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 2015-05-06
      • 2014-06-10
      • 2020-02-12
      • 2014-05-20
      相关资源
      最近更新 更多