Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shift + Esc to Tilde Character? #7

Open
porkloin opened this issue Jan 30, 2021 · 2 comments
Open

Shift + Esc to Tilde Character? #7

porkloin opened this issue Jan 30, 2021 · 2 comments

Comments

@porkloin
Copy link

I'm trying to put together an inception-k2k rule for my 65% keyboard to get shift+escape bound to the ~ character. My keyboard doesn't have a grave/tilde key, but I use tilde quite often sicne it's the *nix shortcut for home directory. I figured this would be relatively straightfoward, but it turns out that it's made complicated by the fact that ~ isn't a key - it's a character that gets printed when Shift+` is typed.

Based on what I've read from the source code, it doesn't seem possible to write a multibind that targets another multibind, like

{ .keys = { KEY_SHIFT, KEY_ESCAPE }, PRESS_ON_DOWN(KEY_SHIFT, KEY_GRAVE), DOWN_IFF_ALL_DOWN(2) }

The other thought I had was that perhaps I could leverage an external tool to write the character for me, but it appears that we don't have any support for doing something like ydotool calls (the wayland compatible variant of xdotool) in response to keybind being pressed.

Any thoughts on how I could accomplish this particular binding?

@zsugabubus
Copy link
Owner

zsugabubus commented Jan 30, 2021

we don't have any support for doing something like ydotool calls (the wayland compatible variant of xdotool) in response to keybind being pressed.

You can use an unused key code for this purpose. So instead of emitting a KEY_FOO, you write 0xbeaf (or anything) that you can handle at the other end of the pipe. You see that key 0xbeaf is down and you can start a process. When key 0xbeaf is up, you just ignore that event. It is basically a map rule that ignores key but does some custom stuff.

{ .keys = { KEY_SHIFT, KEY_ESCAPE }, PRESS_ON_DOWN(KEY_SHIFT, KEY_GRAVE), DOWN_IFF_ALL_DOWN(2) }

PRESS_ON_DOWN(key) takes only one argument. It means that it presses key (down event followed by an up event) when this rule is toggled down and nothing when up. Basically a one shot key. (At least it should do so according to docs and my memories. :))

You would need two-two down and up events fired, that currently is not possible.

I would say you need something like this:

{ .keys = { KEY_SHIFT, KEY_ESCAPE },
  .down_press = { KEY_SHIFT, KEY_RESERVED, KEY_GRAVE, KEY_RESERVED },
  .up_press   = { KEY_RESERVED, KEY_GRAVE, KEY_RESERVED, KEY_SHIFT },
  DOWN_IFF_ALL_DOWN(2) }

And here is the patch for it:

diff --git a/k2k.c b/k2k.c
index 435d1cd..35aac6c 100644
--- a/k2k.c
+++ b/k2k.c
@@ -82,9 +82,9 @@ static struct tap_rule {
  */
 static struct multi_rule {
     int const keys[8]; /** Keys to watch. */
-    int const down_press[2]; /** Press first key and release second key when
+    int const down_press[4]; /** Press first key and release second key when
                                toggled down. */
-    int const up_press[2]; /** Press first key and release second key when
+    int const up_press[4]; /** Press first key and release second key when
                              toggled up. */
     int const nbeforedown; /** Only allow toggling down after this many `keys`
                              have been down. Negative value means inequality. */
@@ -539,7 +539,7 @@ main(void) {
             if (v->can_toggle && (!v->is_down
                         ? ndown == ntotal
                         : (v->nup >= 0 ? ndown == v->nup : ndown != -v->nup))) {
-                int press[2];
+                int press[4];
 
                 v->is_down ^= 1;
                 memcpy(press, v->is_down ? v->down_press : v->up_press, sizeof press);
@@ -555,6 +555,12 @@ main(void) {
 
                     if (press[1] != KEY_RESERVED)
                         write_key_event(press[1], EVENT_VALUE_KEYUP);
+
+                    if (press[2] != KEY_RESERVED)
+                        write_key_event(press[2], EVENT_VALUE_KEYDOWN);
+
+                    if (press[3] != KEY_RESERVED)
+                        write_key_event(press[3], EVENT_VALUE_KEYUP);
                 }
 
                 for (j = 0; j < ntotal; ++j) {

(Sorry for my laziness, it is absolutely untested.)

@musjj
Copy link

musjj commented Aug 15, 2023

Just curious, but if you don't need the physical escape key (caps2esc, etc.), wouldn't this work if you just map Esc to the Grave character?

EDIT: Just tested it, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants