如何在 xmonad 中设置按键释放时发生的操作?

发布于 2024-11-18 20:14:29 字数 261 浏览 3 评论 0原文

如何在 xmonad 中设置按键释放时发生的操作?

我不喜欢菜单栏和面板。 我不想使用像 xmobar 这样的面板,而是希望在按住组合键时显示全屏信息页面(时间、当前选择的窗口和工作区等),然后在松开按键时消失。 我可以自己编写信息页面应用程序。 我可以将信息页面设置为在按键时生成。

我无法设置按键释放时发生的任何事情。

如何设置按键释放时发生的操作?

我正在考虑自己扩展 xmonad 来做到这一点。 我希望我不必这样做,因为这真的很烦人。

How can I set an action to occur on a key release in xmonad?

I don't like menu bars and panels.
Instead of a panel like xmobar I want to have a full screen page of info, (time, currently selected window and workspace etc) appear when I hold down a key combo and then vanish when I let the keys go.
I could code the info page application myself.
I can set the info page to spawn on a key press.

I can not set anything to happen on a key release.

How can I set an action to occur on a key release?

I am considering extending xmonad myself to do this.
I hope I don't have to though because it'd be really annoying.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-11-25 20:14:29

XMonad 将所有接收到的事件(包括 KeyPress 事件)传递给 handleEventHook,因此此代码能够对 keyRelease 事件做出反应:

module KeyUp where

import Data.Monoid
import qualified Data.Map as M

import XMonad
import Control.Monad

keyUpEventHook :: Event -> X All
keyUpEventHook e = handle e >> return (All True)

keyUpKeys (XConf{ config = XConfig {XMonad.modMask = modMask} }) = M.fromList $ 
    [ ((modMask, xK_v), io (print "Hi")) ]

handle :: Event -> X ()
handle (KeyEvent {ev_event_type = t, ev_state = m, ev_keycode = code})
    | t == keyRelease = withDisplay $ \dpy -> do
        s  <- io $ keycodeToKeysym dpy code 0
        mClean <- cleanMask m
        ks <- asks keyUpKeys
        userCodeDef () $ whenJust (M.lookup (mClean, s) ks) id
handle _ = return ()

您可以像在 xmonad 中那样使用它。 hs 文件:

handleEventHook    = handleEventHook defaultConfig `mappend`
                     keyUpEventHook `mappend`
                     fullscreenEventHook

不幸的是,这还不起作用:它只会对在常规 keys 配置中具有相应条目的 KeyRelease 事件做出反应。这是由于 XMonad.Main 中的 grayKeys 仅抓取 keys 中提到的键。您可以通过为您想要在 KeyUp 中处理的每个组合定义一个虚拟操作来解决此问题:

myKeys conf@(XConfig {XMonad.modMask = modMask}) = M.fromList $
    ...
    , ((modMask              , xK_v     ), return ())

XMonad passes all received events, including KeyPress events, to the handleEventHook, so this code would be able to react on keyRelease events:

module KeyUp where

import Data.Monoid
import qualified Data.Map as M

import XMonad
import Control.Monad

keyUpEventHook :: Event -> X All
keyUpEventHook e = handle e >> return (All True)

keyUpKeys (XConf{ config = XConfig {XMonad.modMask = modMask} }) = M.fromList $ 
    [ ((modMask, xK_v), io (print "Hi")) ]

handle :: Event -> X ()
handle (KeyEvent {ev_event_type = t, ev_state = m, ev_keycode = code})
    | t == keyRelease = withDisplay $ \dpy -> do
        s  <- io $ keycodeToKeysym dpy code 0
        mClean <- cleanMask m
        ks <- asks keyUpKeys
        userCodeDef () $ whenJust (M.lookup (mClean, s) ks) id
handle _ = return ()

You would use it like that in your xmonad.hs file:

handleEventHook    = handleEventHook defaultConfig `mappend`
                     keyUpEventHook `mappend`
                     fullscreenEventHook

Unfortunately, this does not work yet: It will only react on KeyRelease events that have a corresponding entry in the regular keys configuration. This is due to grayKeys in XMonad.Main, grabbing only keys mentioned in keys. You can work-around this by defining a dummy action for every combination that you want to handle in KeyUp:

myKeys conf@(XConfig {XMonad.modMask = modMask}) = M.fromList $
    ...
    , ((modMask              , xK_v     ), return ())
沉鱼一梦 2024-11-25 20:14:29
myStartupHook :: X ()
myStartupHook = do
  XConf { display = dpy, theRoot = rootw } <- ask
  myKeyCode <- io $ (keysymToKeycode dpy xK_Super_R)
  io $ grabKey dpy (myKeyCode) anyModifier rootw True grabModeAsync grabModeAsync
  spawn "~/ScriptsVcs/hideTint2.sh"

myHook :: Event -> X All
myHook e = do
  case e of
    ke@(KeyEvent _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) -> do
      if ev_keycode ke == 134
        then if ev_state ke == 0
          then do
            -- key has been pressed
            spawn "~/ScriptsVcs/showTint2.sh"
          else do
            spawn "~/ScriptsVcs/hideTint2.sh"
        else pure ()
    _ -> pure ()
  pure $ All True

以上是一个例子。请注意,使用修饰键 (ev_state) 可能会发生“按键释放”。

myStartupHook :: X ()
myStartupHook = do
  XConf { display = dpy, theRoot = rootw } <- ask
  myKeyCode <- io $ (keysymToKeycode dpy xK_Super_R)
  io $ grabKey dpy (myKeyCode) anyModifier rootw True grabModeAsync grabModeAsync
  spawn "~/ScriptsVcs/hideTint2.sh"

myHook :: Event -> X All
myHook e = do
  case e of
    ke@(KeyEvent _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) -> do
      if ev_keycode ke == 134
        then if ev_state ke == 0
          then do
            -- key has been pressed
            spawn "~/ScriptsVcs/showTint2.sh"
          else do
            spawn "~/ScriptsVcs/hideTint2.sh"
        else pure ()
    _ -> pure ()
  pure $ All True

The above is an example. Do take note that a 'key release' could occur with a modifier key (ev_state).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文