snyball/hawck

Is there good way to get current focused window info in lua script?

radaiming opened this issue · 2 comments

Hi, I'm using autokey to achieve macOS keymap under Linux, take "cmd + c" as example, I need to map it into "ctrl + c" in most programs, but in terminal I may need to map it into "ctrl + shift + c", the script for autokey will be like:

window_class = window.get_active_class()

if window_class in ['xfce4-terminal.Xfce4-terminal', 'tilix.Tilix']:
    keyboard.send_keys('<ctrl>+<shift>+c')
elif window_class == 'Atom':
    keyboard.send_keys('<ctrl>+<insert>')
else:
    keyboard.send_keys('<ctrl>+c')

Currently the only way I could find is use os.execute to run some external program similar to xprop to get window info, but that may be low efficient, so is there any better way to achieve this? Thanks.

This is difficult to support as there currently isn't any widely deployed standard for these sorts of queries, if you run sway you can use swaymsg -t get_tree | jq and look for an entry with {"focused: true}.

If you run a different WM/DE then you need to refer to their respective documentation, it looks like you already have a lead here with the window.get_active_class() function, you should probably investigate to see how that function works.

PS: Is this X11 or Wayland? On X11 this should be easier.

I'm using Awesome WM and autokey, both only work in X11, actually I'm looking for replacement under wayland for future. Thanks for your advice and I will try when migrating :)