程序全局热键_如何在Linux下将全局热键绑定到WINE程序

程序全局热键

程序全局热键

Have you ever installed a Windows program in Linux under WINE, only to discover that it doesn’t bind system wide hot-keys anymore? HTG has the work around you’ve been looking for.

您是否曾经在WINE下Linux中安装过Windows程序,却发现它不再绑定系统范围的热键? HTG一直在为您寻找所需的工作。

Image by djeucalyptus

图片来自djeucalyptus

总览 (Overview)

Every one who has even thought of the idea of switching to Linux, has probably very quickly encounter the problem that there is this one Windows app that you NEED to function. We’ve already shown you that you can accomplish this using WINE.

每个想到过切换到Linux的想法的人,可能很快就会遇到一个问题,那就是您需要运行该Windows应用程序。 我们已经向您展示了您可以使用WINE完成此操作

For this writer, the application was a Text-To-Speech application which utilizes the Microsoft SAPI4 engine. Installing the program under WINE was a breeze, however upon completion I’ve quickly found that the hot-keys used to trigger the various actions of the program (start reading, stop reading, etc’) did not function and that this is a known problem with WINE.

对于本作者而言,该应用程序是使用Microsoft SAPI4引擎的Text-To-Speech应用程序。 在WINE下安装程序很容易,但是完成后我很快发现用于触发程序的各种操作(开始阅读,停止阅读等)的热键不起作用,这是众所周知的葡萄酒的问题。

I’m glad to say that after eons of searching, I’ve finally found the solution in the form of a GNU utility which can manipulate the X.org interface using native functions. While not the only one of its kind, xdotool is the one which was the easiest to get working and was already in the Ubuntu/Mint repositories.

我很高兴地说,经过无数次搜索,我终于找到了一种GNU实用程序形式的解决方案,该实用程序可以使用本机函数来操纵X.org接口。 虽然xdotool并不是唯一的一种,但它却是最容易使用的工具,并且已经存在于Ubuntu / Mint存储库中。

xdotool (xdotool)

The xdotool program can do many window related tasks from the CLI, with that said, the only two we are going to utilize are “search” and “key”. The “search” function does just that, searches for a window/s ID according to parameters you set for it. The “key” function enables you to simulate a key-stroke to a window ID.

xdotool程序可以从CLI执行许多与窗口相关的任务,也就是说,我们将要利用的仅有的两个是“搜索”和“键”。 “搜索”功能就是这样做的,它根据您为其设置的参数搜索窗口ID。 “键”功能使您可以模拟对窗口ID的击键。

Installation & configuration

安装与配置

It is assumed that you’ve already installed WINE and the program you need under it. In this example we will be using Balabolka as the “Windows” application because it is a good freeware replica of the original program I needed this solution for (2nd speech center).

假定您已经安装了WINE及其下面所需的程序。 在此示例中,我们将Balabolka用作“ Windows”应用程序,因为它是我需要此解决方案(第二语音中心)的原始程序的很好的免费软件副本。

If you’ve opted to use Balabolka as well, you need to activate its hotkeys ability. Note: You may want to install either TTSReader or 2nd speech center even in demo mode, so that the SAPI voices will be installed. 

如果您还选择使用Balabolka,则需要激活其热键功能。 注意:即使在演示模式下,您可能也想安装TTSReader或第二语音中心,以便安装SAPI语音。

Open the program and go to settings (Shift+F6) under “Options” -> “Settings”.

打开程序并转到“选项”->“设置”下的设置(Shift + F6)。

Go to the hotkeys tab and check the checkbox for “Use global hotkeys”. Click OK.

转到热键选项卡,然后选中“使用全局热键”复选框。 单击确定。

Leave it running in the background so that it can do its job when we hook the keystrokes to it.

让它在后台运行,以便当我们将击键挂到它时就可以完成它的工作。

Install xdotool by issuing:

通过发出以下命令安装xdotool:

sudo apt-get install xdotool

sudo apt-get install xdotool

全局绑定 (Global binding)

The xdotool program on its own doesn’t help us bind globally to hotkeys, but we can use the already existing OS hotkey system. What we will do is create a simple script that utilizes xdotool to send the keystrokes we want to the Balabolka program and call it from the OSs hotkey system.

xdotool程序本身并不能帮助我们全局绑定到热键,但是我们可以使用已经存在的OS热键系统。 我们要做的是创建一个简单的脚本,该脚本利用xdotool将所需的击键发送到Balabolka程序,并从OS的热键系统中调用它。

Create a script called “start_read.sh” with the following content:

创建一个名为“ start_read.sh”的脚本 ,其内容如下:

xdotool key --window $( xdotool search --limit 1 --all --pid $( pgrep balabolka ) --name Balabolka ) "ctrl+alt+F9"

xdotool key --window $( xdotool search --limit 1 --all --pid $( pgrep balabolka ) --name Balabolka ) "ctrl+alt+F9"

Create another script this time called “stop_read.sh” with the following content:

这次创建另一个名为“ stop_read.sh”的脚本 ,其内容如下:

xdotool key --window $( xdotool search --limit 1 --all --pid $( pgrep balabolka ) --name Balabolka ) "ctrl+alt+F7"

xdotool key --window $( xdotool search --limit 1 --all --pid $( pgrep balabolka ) --name Balabolka ) "ctrl+alt+F7"

Note: I know this is a one liner that doesn’t require a script, but the Mint/Ubuntu “Keyboard Shortcuts” program, wasn’t cooperating with just invoking it directly. If you know how to do it, please share in the comments below.

注意:我知道这是一个不需要脚本的衬板,但是Mint / Ubuntu的“键盘快捷键”程序并不能直接调用它。 如果您知道该怎么做,请在下面的评论中分享。

Braking this command to its components, what we see is:

将这个命令与其组件结合起来,我们看到的是:

  • The “–pid $( pgrep balabolka )” part, executes a “pgrep” on the program we want to use as to ascertain its process ID. This will narrow the xdotool filed of “search” to just that PID.

    “ –pid $(pgrep balabolka)”部分在要用来确定其进程ID的程序上执行“ pgrep ”。 这会将xdotool的“搜索”字段范围缩小到该PID

  • The “xdotool search –limit 1 –all … –name Balabolka” part, narrows the filed of search of xdotool even more and limits the returned answers to 1. As in our case it doesn’t matter which of the window IDs returned of the program, limiting the result acts as a formatter for the “key” command. You may find you need to massage this part more if it does matter for the program that you are using.

    “ xdotool搜索–limit 1 –all…–name Balabolka”部分将xdotool的搜索范围进一步缩小,并将返回的答案限制为1。在我们的情况下,返回的窗口ID无关紧要程序,限制结果充当“ key”命令的格式化程序。 如果对您使用的程序确实很重要,您可能会发现需要进一步按摩这一部分。
  • The “xdotool key –window %WINDOW_ID% “ctrl+alt+F7″” part, sends the desired keystroke to the windowID which was obtained by the previous parts.

    “ xdotool键–窗口%WINDOW_ID%“ ctrl + alt + F7””部分将所需的击键发送到由先前部分获得的windowID。

Make the scripts executable.

使脚本可执行

Linux Mint Keyboard shortcuts

Linux Mint键盘 快捷键

Under Linux Mint, the global hotkeys are set in the “Keyboard Shortcuts” program.

Linux Mint下 ,全局热键在“键盘快捷键”程序中设置。

Once opened Click on “Add” to create a new custom shortcut:

打开后,单击“添加”以创建新的自定义快捷方式:

Give it a name and under “Command” give the full path to one of the scripts we’ve created above. Repeat the process for the second script.

给它起一个名字,在“命令”下给出我们上面创建的脚本之一的完整路径 。 对第二个脚本重复该过程。

Now, on the “Shortcuts” Column, click on the “Disabled” word to get the option to set a new key combo. Note: You may, if you wish, use something other then the program’s default. In a sense creating a “remap” to key bindings that, depending on the program you use, would otherwise be out of your control.

现在,在“快捷方式”列上,单击“禁用”字样,以获取设置新按键组合的选项。 注意:如果愿意,可以使用程序默认值以外的其他值。 从某种意义上说,要创建“重映射”到键绑定,否则将无法控制,具体取决于您使用的程序。

Hit the combo you’ve selected and hear the magic.

击中您选择的组合,然后听到魔法。

Ubuntu Keyboard

Ubuntu键盘

Under Ubuntu, the program that sets the global hotkeys is just called “Keyboard”.

在Ubuntu下,设置全局热键的程序仅称为“键盘”。

Switch to the “Shortcuts” tab and select “Custom Shortcuts”.

切换到“快捷方式”选项卡,然后选择“自定义快捷方式”。

Click the plus sign to add a shortcut. Give it a name and under “Command” give the full path to one of the scripts we’ve created above. Repeat the process for the second script. Now click on the “Disabled” word to get the option to set a new key combo.

单击加号以添加快捷方式。 给它起一个名字,在“命令”下给出我们上面创建的脚本之一的完整路径 。 对第二个脚本重复该过程。 现在,单击“ Disabled(禁用)”字样,以获取设置新按键组合的选项。

Note: Repeating on the note from the Mint section, you may, if you wish, use something other then the program’s default. In a sense creating a “remap” to key bindings that, depending on the program you use, would otherwise be out of your control.

注意:重复“薄荷”部分的注释,如果需要,可以使用程序默认值以外的其他值。 从某种意义上说,要创建“重映射”到键绑定,否则将无法控制,具体取决于您使用的程序。

作者笔记 (Author’s Notes)

Every time I’ve seriously considered moving to Linux, this issue was the first on my list of problems. Its not that Linux doesn’t have problems, but this was the real hurdle, for me. I’ve tried time and time again, asked friends/people in the field and even made it into a bounty… I am happy this saga is over and that my soul can finally rest. It is my hope that I’ve helped someone out there to not have to go through the same ordeal.

每当我认真考虑迁移到Linux时,这个问题就列在我的问题清单上。 不是说Linux没有问题 ,但这对我来说是真正的障碍。 我一次又一次地尝试着,问问该领域的朋友/人们,甚至使它变成了赏金……我很高兴这个传奇结束了,我的灵魂终于可以安息了 。 我希望我已经帮助过某个人而不必经历同样的磨难。



Doc Brown: It’s taken me nearly thirty years and my entire family fortune to realize the vision of that day. My God, has it been that long?

布朗博士 :我花了将近30年的时间和整个家庭的财富来实现这一天的梦想。 天哪,有那么久吗?

翻译自: https://www.howtogeek.com/125664/how-to-bind-global-hotkeys-to-a-wine-program-under-linux/

程序全局热键

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 python 第三方库「pyHook」或「Xlib」来编写 Linux 系统下的程序。 - pyHook:是一个 Python 钩子(hook)管理器,可以监听和记录全局盘和鼠标事件,因此可以很容易地实现功能。 - Xlib:是一个 Python 库,可以直接与 X Window 系统通信,因此可以实现捕获的功能。 需要注意的是,这两个库的使用都需要一的编程知识,需要您具备一的 Python 基础。 ### 回答2: 在Linux系统下使用Python编写程序是非常方便和易于实现的。Python提供了一些强大的库和模块,可以帮助我们实现各种功能。下面是一个简单的例子,展示了如何使用Python创建一个程序: ```python import subprocess import time from pynput import keyboard # 回调函数 def on_hotkey(): subprocess.Popen(["gnome-terminal"]) # 打开终端 time.sleep(0.5) # 等待终端打开 subprocess.Popen(["gedit"]) # 打开文本编辑器 # 设置 hotkey = keyboard.HotKey( keyboard.HotKey.parse('<ctrl>+<alt>+t'), on_hotkey) # 监听 with keyboard.Listener(on_hotkey=hotkey.press) as listener: listener.join() ``` 在这个例子中,我们使用了`pynput`库来监听事件。`subprocess`库用于启动终端和文本编辑器。首先,我们义了一个回调函数`on_hotkey()`,当被触发时,该函数会被调用。在这个例子中,按下"Ctrl + Alt + T"会打开终端,并在0.5秒后打开文本编辑器。 然后,我们使用`keyboard.HotKey()`函数设置了为"Ctrl + Alt + T",并传入回调函数作为参数。最后,使用`keyboard.Listener()`函数监听事件,当被按下时,会调用回调函数。 这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。Python在Linux系统下编写程序非常灵活和强大,你可以使用其他库来实现更复杂的功能,比如控制音乐播放、打开特应用程序等。 ### 回答3: 在Linux系统下,我们可以使用Python编写程序。Python的大量第三方库和模块使得创建程序变得相对容易。 首先,我们需要使用X Window系统提供的库来监听盘事件。这可以通过使用`python-xlib`库实现。该库提供了与X Window系统进行通信的功能,包括检测盘事件。 接下来,我们需要的触发条件。例如,我们可以指某个特的按组合来触发,比如Ctrl+Alt+P。在`python-xlib`库中,我们可以使用`KeyPress`事件来监听按按下的事件。 然后,我们义一个回调函数来处理触发后的操作。这个函数可以是任何我们想要执行的操作,比如打开一个应用程序、切换窗口、执行某个命令等等。 最后,我们将X Window系统的事件循环与我们的程序结合起来。事件循环会不断监听盘事件,并在检测到符合条件的事件时调用我们义的回调函数。 以下是一个简单的示例代码: ```python from Xlib import X, display def hotkey_callback(): # 在此处触发后的操作 print("触发") def main(): # 设置监听器 d = display.Display() root = d.screen().root root.change_attributes(event_mask=X.KeyPressMask) # 设置组合 keys_combination = [X.ControlMask, X.Mod1Mask, X.KeyPress] key_code = d.keysym_to_keycode(ord('P')) while True: event = d.next_event() if event.type == X.KeyRelease and event.detail == key_code and \ (event.state & keys_combination) == tuple(keys_combination): hotkey_callback() if __name__ == "__main__": main() ``` 以上代码示例使用`python-xlib`库获取并监听盘事件,当检测到按下Ctrl+Alt+P时,会调用`hotkey_callback()`函数。你可以根据需要进行修改和扩展,执行相应的操作。 请注意,由于程序需要与系统进行交互,可能需要在使用之前以管理员权限运行该程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值