baskerville / sxhkd

Simple X hotkey daemon
BSD 2-Clause "Simplified" License
2.79k stars 139 forks source link

Is it possible to assign commands to a single `ctrl` keypress? #244

Open TahsinTariq opened 3 years ago

TahsinTariq commented 3 years ago

Title. I need a script to run each time I press ctrl to change keyboard backlight and show useful shortcuts. If sxhkd can't do it, is there some alternate way?

emanuele6 commented 3 years ago

Hello.

sxhkd can do it:

~{Control_L,Control_R}
    {_,_}command_to_run_when_i_press_either_ctrl_key

ctrl is a modifier, not a key. You need to specify a key, the control keys are two: Control_L (left) and Control_R (right). You also want to add a ~ before the key names (this makes sxhkd replay the key event), if you don't add the ~ windows won't be able to detect when you press ctrl.

Cheers.

TahsinTariq commented 3 years ago

Thanks. I can't figure out the syntax for when I want a combination of just two modifiers. For example, Super_L + Control_L or Super_L + Control_L + Shift_L. The arch wiki doesn't have enough documentation on this and I can't find them anywhere else.

emanuele6 commented 3 years ago

two modifiers. For example, Super_L + Control_L or Super_L + Control_L + Shift_L

Those are not modifiers, they are keys..

All chords must have exactly one key and any number of modifiers:

TahsinTariq commented 3 years ago

I also can't run

@Control_L
    echo "foo"

I've tried using @ctrl but doesn't work

emanuele6 commented 3 years ago

I also can't run

@Control_L
    echo "foo"

It does work. make sure that you don't have typos and that you are not redefining @Control_L later on on the file.

I've tried using @ctrl but doesn't work

that is not a valid chord.

TahsinTariq commented 3 years ago

It does work. make sure you have that you don't have typos and that you are not redefining @Control_L later on on the file.

Ohh, So does that mean I can only execute a command for either keypress or release?

~Control_L
    echo "bar"
@Control_L
    echo "foo"

This only ever prints "bar" on keypress. And If I remove the ~ from the first line, I do get both "foo" and "bar" but I can't use ctrl+c to copy something as I normally do

emanuele6 commented 3 years ago

It does work. make sure you have that you don't have typos and that you are not redefining @Control_L later on on the file.

Ohh, So does that mean I can only execute a command for either keypress or release?

sxhkd will execute everything you write with "$SHELL" -c what_you_wrote (or with "$SXHKD_SHELL" -c what_you_wrote if SXHKD_SHELL is set in the environment). In shell syntax, you can use ; to run multiple commands:

super + n
    echo foo; echo bar
# or, on multiple lines (note sxhkd removes the newlines)
super + x
    echo foz; \
    echo baz; \
    echo zuz

This only ever prints "bar" on keypress. And If I remove the ~ from the first line, I do get both "foo" and "bar" but I can't use ctrl+c to copy something as I normally do

I can confirm that that happens. It is proabably a bug.

It seems a little weird because i posted an sxhkd feature request (#198) last year in which I show @x and ~x working together and there have not been any updates to sxhkd since then. I wonder why this doesn't work anymore.

Jackojc commented 3 years ago

Also having trouble with this. I would like to display caps on/off because my keyboard doesn't have an indicator light.

emanuele6 commented 3 years ago

@Jackojc How is that related to this issue?

Jackojc commented 3 years ago

Assigning commands to a single caps lock key press.

emanuele6 commented 3 years ago

Again, how is that related?

You do it like so:

Caps_Lock
  notify-send 'i work; what is the issue?'
Jackojc commented 3 years ago

The problem like above is that I cannot activate a script on both press and release and sometimes the keypress causes other keys pressed in quick succession to be swallowed.

emanuele6 commented 3 years ago

I see. what you mean is that these work:

# these work
{_,@}Caps_Lock
    notify-send Caps_Lock {pressed,released}

but, only the pressed part works if you add the replay option:

# only "pressed" works
{~,@}Caps_Lock
    notify-send '~Caps_Lock' {pressed,released}
Jackojc commented 3 years ago

So I have a keybinding set up like:

~@Caps_Lock
    notify_keyboard "caps" "$(keyboard_caps)"

which will show me a notification on key release the current state of the caps lock modifier but for some reason if I press caps, subsequent key presses seem to be swallowed for a moment. So say I press caps and then J right away, the J might be swallowed so I'm not sure if ~@ is working as intended.

moviuro commented 2 years ago

How should I configure sxhkd for both to work independantly (both shortcuts should absolutely never trigger at the same time):

The current behavior is that chords where "super" is a modifier correctly work but the "single press Super_L for xterm(1)" also triggers.

# { focus, move } the node in the given direction
super + {_,shift + }{Left,Down,Up,Right}
 bspc node --{focus,swap} {west,south,north,east}

# broken - it ALWAYS triggers with the previous shortcut; this is UNWANTED behavior
#Super_L
#@Super_L
#~Super_L
#@Super_L;
#~Super_L;
~@Super_L;
 xterm

EDIT: seems to be a real issue with sxhkd: https://github.com/baskerville/sxhkd/issues/126

ortango commented 2 years ago

@moviuro

that specific issue could be dealt with by using xcape.

xcape -e 'Super_L=Super_L|t'

where super + t launches xterm.

xcape will not solve all of these types of issues though.

pataquets commented 2 years ago

I can't help here with the OP, but as I'm looking into sxhkd to use it under i3, I find useful to use sxhkd to launch programs with "dead key taps". However, I'm wondering if some people might be looking here for more complex keyboard ergonomics, and I'd like to share my 2c in ergonomics (as is often the case for xcape users, for instance).

So, OFFTOPIC warning ahead, I'd like to add here my recommendation for those looking for complex key remappings, specially in ergonomics scenarios.

I'm using kmonad to achieve (among other things) a home row mod as I describe here, just for ergonomics sake. Disclaimer: linked reply is mine.

For more info on what can be achieved with kmonad, check out this fine reddit thread. I think both tools are complementary to each other. Hope it helps improve health/ergonomics for others, as it did for me. I hope you don't mind the offtopic, since I thought it might be useful for keyboard freaks, hanging out here for sure.