Re: [hackers] [dwm][PATCH] Implement key shortcut mapping

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun, 24 Sep 2023 11:29:50 +0200

On Sun, Sep 24, 2023 at 05:34:48AM +0000, suiso67_AT_macadamia.rocks wrote:
> From: suiso67 <suiso67_AT_macadamia.rocks>
>
> ---
> config.def.h | 43 +++++++++++++++++++++++++++++++++++++++++++
> dwm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 90 insertions(+)
>
> diff --git a/config.def.h b/config.def.h
> index 061ad66..98ff36f 100644
> --- a/config.def.h
> +++ b/config.def.h
> _AT_@ -51,6 +51,8 @@ static const Layout layouts[] = {
> { MODKEY|ControlMask, KEY, toggleview, {.ui = 1 << TAG} }, \
> { MODKEY|ShiftMask, KEY, tag, {.ui = 1 << TAG} }, \
> { MODKEY|ControlMask|ShiftMask, KEY, toggletag, {.ui = 1 << TAG} },
> +#define KEYEVENT(SRC_MOD,SRC_KEY,DST_MOD,DST_KEY) \
> + { SRC_MOD, SRC_KEY, sendkeyevent, { .v = &(const KeyShortcut){ DST_MOD, DST_KEY } } },
>
> /* helper for spawning shell commands in the pre dwm-5.0 fashion */
> #define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }
> _AT_@ -84,6 +86,47 @@ static const Key keys[] = {
> { MODKEY, XK_period, focusmon, {.i = +1 } },
> { MODKEY|ShiftMask, XK_comma, tagmon, {.i = -1 } },
> { MODKEY|ShiftMask, XK_period, tagmon, {.i = +1 } },
> + // Navigation(two-handed)
> + KEYEVENT(Mod1Mask, XK_h, 0, XK_Left)
> + KEYEVENT(Mod1Mask, XK_l, 0, XK_Right)
> + KEYEVENT(Mod1Mask, XK_k, 0, XK_Up)
> + KEYEVENT(Mod1Mask, XK_j, 0, XK_Down)
> + KEYEVENT(Mod1Mask, XK_p, 0, XK_Up)
> + KEYEVENT(Mod1Mask, XK_n, 0, XK_Down)
> + KEYEVENT(Mod1Mask, XK_i, ControlMask, XK_Left)
> + KEYEVENT(Mod1Mask, XK_o, ControlMask, XK_Right)
> + KEYEVENT(Mod1Mask, XK_q, ControlMask, XK_Left)
> + KEYEVENT(Mod1Mask, XK_w, ControlMask, XK_Right)
> + KEYEVENT(Mod1Mask, XK_equal, ControlMask, XK_Home)
> + KEYEVENT(Mod1Mask, XK_minus, ControlMask, XK_End)
> + // Navigation(one-handed)
> + KEYEVENT(Mod1Mask, XK_s, 0, XK_Up)
> + KEYEVENT(Mod1Mask, XK_x, 0, XK_Down)
> + KEYEVENT(Mod1Mask, XK_z, 0, XK_Left)
> + KEYEVENT(Mod1Mask, XK_c, 0, XK_Right)
> + KEYEVENT(Mod1Mask, XK_d, 0, XK_Return)
> + KEYEVENT(Mod1Mask, XK_a, 0, XK_Home)
> + KEYEVENT(Mod1Mask, XK_e, 0, XK_End)
> + // Selection(two-handed)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_h, ShiftMask, XK_Left)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_l, ShiftMask, XK_Right)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_k, ShiftMask, XK_Up)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_j, ShiftMask, XK_Down)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_p, ShiftMask, XK_Up)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_n, ShiftMask, XK_Down)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_i, ControlMask|ShiftMask, XK_Left)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_o, ControlMask|ShiftMask, XK_Right)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_equal, ControlMask|ShiftMask, XK_Home)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_minus, ControlMask|ShiftMask, XK_End)
> + // Selection(one-handed)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_s, ShiftMask, XK_Up)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_x, ShiftMask, XK_Down)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_z, ShiftMask, XK_Left)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_c, ShiftMask, XK_Right)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_q, ControlMask|ShiftMask, XK_Left)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_w, ControlMask|ShiftMask, XK_Right)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_a, ShiftMask, XK_Home)
> + KEYEVENT(Mod1Mask|ShiftMask, XK_e, ShiftMask, XK_End)
> TAGKEYS( XK_1, 0)
> TAGKEYS( XK_2, 1)
> TAGKEYS( XK_3, 2)
> diff --git a/dwm.c b/dwm.c
> index e5efb6a..6988c77 100644
> --- a/dwm.c
> +++ b/dwm.c
> _AT_@ -106,6 +106,11 @@ typedef struct {
> const Arg arg;
> } Key;
>
> +typedef struct {
> + unsigned int mod;
> + KeySym keysym;
> +} KeyShortcut;
> +
> typedef struct {
> const char *symbol;
> void (*arrange)(Monitor *);
> _AT_@ -196,6 +201,7 @@ static void restack(Monitor *m);
> static void run(void);
> static void scan(void);
> static int sendevent(Client *c, Atom proto);
> +static void sendkeyevent(const Arg *arg);
> static void sendmon(Client *c, Monitor *m);
> static void setclientstate(Client *c, long state);
> static void setfocus(Client *c);
> _AT_@ -1458,6 +1464,47 @@ sendevent(Client *c, Atom proto)
> return exists;
> }
>
> +XKeyEvent
> +createkeyevent(Display *display, Window win, Window rootWindow, int type, KeyShortcut *keyShortcut)
> +{
> + int keysym = keyShortcut->keysym;
> + unsigned int modifier = keyShortcut->mod;
> +
> + XKeyEvent event;
> + event.type = type;
> + event.display = display;
> + event.window = win;
> + event.root = rootWindow;
> + event.subwindow = None;
> + event.time = CurrentTime;
> + event.x = 1;
> + event.y = 1;
> + event.x_root = 1;
> + event.y_root = 1;
> + event.same_screen = True;
> + event.keycode = XKeysymToKeycode(display, keysym);
> + event.state = modifier;
> +
> + return event;
> +}
> +
> +void sendkeyevent(const Arg *arg)
> +{
> + Window rootWindow = XDefaultRootWindow(dpy);
> +
> + Window focusedWindow;
> + int revert;
> + XGetInputFocus(dpy, &focusedWindow, &revert);
> +
> + KeyShortcut *keyShortcut = (KeyShortcut *)arg->v;
> +
> + XKeyEvent event = createkeyevent(dpy, focusedWindow, rootWindow, KeyPress, keyShortcut);
> + XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
> +
> + event = createkeyevent(dpy, focusedWindow, rootWindow, KeyRelease, keyShortcut);
> + XSendEvent(event.display, event.window, True, KeyReleaseMask, (XEvent *)&event);
> +}
> +
> void
> setfocus(Client *c)
> {
> --
> 2.42.0
>
>
>

Hi,

You can upload it to the wiki if you want,

-- 
Kind regards,
Hiltjo
Received on Sun Sep 24 2023 - 11:29:50 CEST

This archive was generated by hypermail 2.3.0 : Sun Sep 24 2023 - 11:36:35 CEST