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

[Feature request] Allow forwarding of key events #4937

Open
kuon opened this issue Jan 22, 2020 · 10 comments · May be fixed by #8497
Open

[Feature request] Allow forwarding of key events #4937

kuon opened this issue Jan 22, 2020 · 10 comments · May be fixed by #8497
Labels
enhancement New feature or incremental improvement input/keyboard

Comments

@kuon
Copy link

kuon commented Jan 22, 2020

Shortly put, I'd like to be able to do this:

bindsym F24 forward [class="discord"]

With this example, when F24 is pressed, the first window matching the criteria get a keypress event, exactly as if it was focused and F24 was pressed.

This would allow for a very easy way to handle global hotkeys.

@ddevault
Copy link
Contributor

Hm, neat idea.

@NilsIrl
Copy link
Contributor

NilsIrl commented Jan 26, 2020

I think a way of doing it that would be more flexible is allow to send input. And now that I'm typing it I realise that this is already possible with seats.

So in this case you would do:

for_window [class="discord"] bindsym F24 seat - mmmhh, oops, nope, keyboards don't work yet. #1779

I prefer this solution over adding a redundant command.

@kuon
Copy link
Author

kuon commented Jan 26, 2020

While I understand your argument, and I would accept it as a solution when it is implement, I have two small concerns:

  • The forward keyword avoid duplicating the key and is easy to understand
  • bindsym XX .... syntax instead of for_window... bindsym XX provides easier grouping of global hotkey in the config

I think the config file must be as expressive, concise and easy to organize as possible.

@NilsIrl
Copy link
Contributor

NilsIrl commented Jan 26, 2020

* `bindsym XX ....` syntax instead of `for_window... bindsym XX` provides easier grouping of global hotkey in the config

I'm not sure I understand what you mean here. I would say it's actually the opposite:

for_window [class="discord"] bindsym {
	F21 .......
	F22 .......
	F23 .......
	F24 .......
}

@kuon
Copy link
Author

kuon commented Jan 26, 2020

Well, that's a matter of taste, for me:

bindsym XX ...
bindsym YY ...

is clearer than:

for_window ... bindsym..., other rules
... a bunch of lines
for_window ... bindsym..., other rules

Because I group my for_window directives not in the same way I would group my bindsym global hostkey. With a list of bindsym it's also easier to ensure you did not override a key.

But this is a matter of taste.

Anyway, there is another problem, the for_window wouldn't bind the key if there is no window, and the whole point of global hotkeys is for them to be always bound, regardless of context, if there is no window matching criteria to forward the key to, then the key is a noop.

My syntax would make that clear and would also permit forwarding to multiple apps:

bindsym F24 forward [app_id=A...], forward [app_id=B...]

This would swallow the key if neither A and B are running, if either A or B is running it would forward the key to it, and if both are running, it would forward the key to both.

@emersion
Copy link
Member

forward would require keeping metadata about the key event around and pass it to the command executed by bindsym. Not a fan of this.

@kuon
Copy link
Author

kuon commented Jan 27, 2020

forward would require keeping metadata about the key event around and pass it to the command executed by bindsym. Not a fan of this.

Well, I guess creating a new event with the same keycode would work, like bindsym F24 emit [app_id...] which would emit the same key, and bindsym F24 emit F12 ... which would remap the key.

The idea is to provide an easy way to bind global hotkeys for app that cannot be scripted. I'm sure we can come up with a nice solution.

@NilsIrl
Copy link
Contributor

NilsIrl commented Jan 27, 2020

If syntax sugar is what you want you could use variables:

set $emit seat - keyboard press

(Ignoring the fact keyboards haven't been implemented yet)

Reread the original comment (#4937 (comment)) and realised I misread it. It won't be as easy as using seats. But regardless I still believe it can be done with seats. Especially with if "virtual seats" become a thing.

@jalvesaq
Copy link

A workaround for this issue is to write a script that:

  • Move the focus to the target application.
  • Use wtype to send the key events.
  • Move the focus back to the original workspace.

Below is an example with OBS as the target application:

#!/bin/sh

WRKSPC1=`swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).name'`

swaymsg [app_id="obs"] focus
if [ "$?" = "0" ]
then
    wtype $*

    WRKSPC2=`swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).name'`
    if [ ! "$WRKSPC1" = "$WRKSPC2" ]
    then
        swaymsg workspace number $WRKSPC1
    fi
fi

With the above script saved as send_keys_to_obs and the file turned executable, we could, for instance, press Super+n to send Ctrl+n to OBS:

bindsym $mod+n exec /path/to/send_keys_to_obs -M ctrl -k n -m ctrl

Notes: we can't use the same key combination in both OBS and Sway because it would result in an infinite loop.

@boredland
Copy link

boredland commented Jan 8, 2024

More generalized approach from what @jalvesaq does:

#!/bin/sh
set -x

PID=$(swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true) | .pid')

matcher=$1
shift
type=$@

swaymsg [$matcher] focus
    if [ "$?" = "0" ]
then
    wtype $type
    swaymsg "[pid=$PID] focus"
fi

This way you get your window focus back afterwards.

Using it like this:

type-to-app.sh "class=\"VencordDesktop\"" -M ctrl -M shift -k m -m ctrl -m shift

It is quite annoying that the window flashes into focus briefly tho and am still searching for a solution that doesn't do that...

Tert0 added a commit to Tert0/sway that referenced this issue Dec 11, 2024
Command Syntax: emit <press|release|press-release> <criteria> <keys>
* action: specifies whether to press, release the keys or do both
* criteria: first window that matches the criteria will get the key input
* keys: modifiers and keys separated by +
Examples:
* emit press-release [app_id=com.obsproject.Studio] ALT+F8
* bindsym --no-repeat Ctrl+T emit press [app_id=wev] Ctrl+A
* bindsym --no-repeat --release Ctrl+T emit release [app_id=wev] Ctrl+A
@Tert0 Tert0 linked a pull request Dec 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or incremental improvement input/keyboard
Development

Successfully merging a pull request may close this issue.

7 participants