x11xorgxmonad

How can I detect a key press / button down event in XMonad?


I've tried to detect this manually by:

myHook :: Event -> X All
myHook e = do
  liftIO $ logToTmpFile $ show e
  pure $ All True

However on the keypress of modm (hyper/windows) it just shows:

PropertyEvent {ev_event_type = 28, ev_serial = 12080, ev_send_event = False, ev_event_display = Display 0x0000000000df2340, ev_window = 27262982, ev_atom = 353, ev_time = 20662387, ev_propstate = 0}

Which seems rather generic, different keys seem to also emit the same event.


Upon inspection of the XMonad source, it seems 'events' only occur for keystrokes that are 'registered' to be listened to.


Solution

  • To register a 'keystroke' for xK_Super_R as an example:

    import Data.Monoid (All(..)) 
    
    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
    

    And

    myHook :: Event -> X All
    myHook e = do
      liftIO $ logToTmpFile $ show e
      pure $ All True
    

    And then add these to your startupHook and handleEventHook values in the config.

    I see the following output:

    KeyEvent {ev_event_type = 3, ev_serial = 2866, ev_send_event = False, ev_event_display = Display 0x0000000001217340, ev_window = 1056, ev_root = 1056, ev_subwindow = 25166105, ev_time = 4248223, ev_x = 1014, ev_y = 1038, ev_x_root = 1014, ev_y_root = 1038, ev_state = 64, ev_keycode = 134, ev_same_screen = True