AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 696526
Accepted
Artem S. Tashkinov
Artem S. Tashkinov
Asked: 2022-03-24 06:11:17 +0800 CST2022-03-24 06:11:17 +0800 CST 2022-03-24 06:11:17 +0800 CST

一个简单的全局键盘快捷键处理程序

  • 772

是的,我知道actkbd允许分配全局键盘快捷键,这些快捷键在任何地方都可以使用,包括文本控制台和图形会话,但我不想为单个键盘快捷键运行额外的守护程序(也长期无人维护)。我想要一些更简单的东西,没有配置选项,并且具有绝对最少的代码量。

任务是在按下此组合键时运行命令:

Win+ End->systemctl suspend

这可能值得在 stackoverflow.com 上发布,但我不完全确定。

linux-kernel keyboard-shortcuts
  • 1 1 个回答
  • 228 Views

1 个回答

  • Voted
  1. Best Answer
    Marcus Müller
    2022-03-24T12:25:46+08:002022-03-24T12:25:46+08:00

    所以,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您可以确保守护程序已启动(立即启动,并且在以后每次启动时)。

    • 1

相关问题

  • 如何让我的帧缓冲控制台工作?

  • 阻止挂载系统调用

  • 为什么无线工具版本 30 成为永久测试版?

  • 程序堆栈大小

  • 哪些 802.11ac(或更高版本)WiFi 加密狗适用于 Linux 4.13 内核

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve