
ええ、私もそこにいることを知っていますモバイル知識ベーステキストコンソールやグラフィックセッションなど、どこでも利用可能なグローバルキーボードショートカットを割り当てることはできますが、単一のキーボードショートカットに対して追加のデーモンプロセスを実行したくない(長く維持されていない)。私は設定オプションがなく、最小限のコードのみを使用してより簡単なものを望んでいました。
タスクは、次のキーの組み合わせを押すとコマンドを実行することです。
Win+ End->systemctl suspend
これはstackoverflow.comに投稿する価値があるかもしれませんが、完全にはわかりません。
答え1
したがって、Linuxにはこの種の仕事のための非常に良いフレームワークがあります。uinput
evdevは何も隠さない素晴らしいインターフェースです。細身です。
デフォルトでは、すべてのLinuxディストリビューションにはpython3-evdev
パッケージがあります(少なくともdebian、ubuntu、およびfedoraではこれはパッケージ名です)。
その後、デーモンを書くためのコードは数行だけ必要です。はいコード、あなたが何をしているかを知るためにいくつかの説明を追加しました。
#!/usr/bin/python3
# Copyright 2022 Marcus Müller
# SPDX-License-Identifier: BSD-3-Clause
# Find the license text under https://spdx.org/licenses/BSD-3-Clause.html
from evdev import InputDevice, ecodes
from subprocess import run
# /dev/input/event8 is my keyboard. you can easily figure that one
# out using `ls -lh /dev/input/by-id`
dev = InputDevice("/dev/input/event8")
# we know that right now, "Windows Key is not pressed"
winkey_pressed = False
# suspending once per keypress is enough
suspend_ongoing = False
# read_loop is cool: it tells Linux to put this process to rest
# and only resume it, when there's something to read, i.e. a key
# has been pressed, released
for event in dev.read_loop():
# only care about keyboard keys
if event.type == ecodes.EV_KEY:
# check whether this is the windows key; 125 is the
# keyboard for the windows key
if event.code == 125:
# now check whether we're releasing the windows key (val=00)
# or pressing (1) or holding it for a while (2)
if event.value == 0:
winkey_pressed = False
# clear the suspend_ongoing (if set)
suspend_ongoing = False
if event.value in (1, 2):
winkey_pressed = True
# We only check whether the end key is *held* for a while
# (to avoid accidental suspend)
# key code for END is 107
elif winkey_pressed and event.code == 107 and event.value == 2:
run(["systemctl", "suspend"])
# disable until win key is released
suspend_ongoing = True
それはすべてです。あなたのデーモンはわずか16行のコードです。
:を使用して直接実行できますが、sudo python
自動的に起動したい場合があります。
ファイルとして保存/usr/local/bin/keydaemon
してsudo chmod 755 /usr/local/bin/keydaemon
実行可能にします。/usr/lib/systemd/system/keydaemon.unit
コンテンツを含むファイルを追加する
[Unit]
Description=Artem's magic suspend daemon
[Service]
ExecStart=/usr/local/bin/keydaemon
[Install]
WantedBy=multi-user.target
その後、sudo systemctl enable --now keydaemon
デーモンが開始されたことを確認できます(即時およびそれ以降の起動時に)。