set_input_focus()聚焦視窗,他的參數我還不太懂
from Xlib import display,X,XK,xobject,Xcursorfont
dpy=display.Display()
screen=dpy.screen()
root=screen.root
#註冊事件
screen.root.grab_key(dpy.keysym_to_keycode(XK.string_to_keysym("q")),X.Mod4Mask,1,X.GrabModeAsync, X.GrabModeAsync)
screen.root.grab_key(dpy.keysym_to_keycode(XK.string_to_keysym("r")),X.Mod4Mask,1,X.GrabModeAsync, X.GrabModeAsync)
font = dpy.open_font('cursor') # 開啟 cursor 字型
cursor = font.create_glyph_cursor(
font, # mask 用同一個 font
Xcursorfont.gumby, # 游標形狀(source_char)https://xorg.freedesktop.org/archive/current/doc/libX11/libX11/libX11.html#x_font_cursors
#Xcursorfont.left_ptr,
Xcursorfont.gumby+1, # mask_char(通常是 +1)
(0, 0, 0), # 前景色 RGB(黑色)
(65535, 65535, 65535) # 背景色 RGB(白色)
)
root.change_attributes(cursor=cursor)
q_code=dpy.keysym_to_keycode(XK.string_to_keysym("q"))
r_code=dpy.keysym_to_keycode(XK.string_to_keysym("r"))
while True:
event=dpy.next_event()
root.set_input_focus(X.RevertToParent,X.CurrentTime)
if event.child!=X.NONE:
print(dpy.get_input_focus())
if event.type==X.KeyPress and event.child!= X.NONE:
if event.detail==q_code:
print(f"kill client:{event.child.get_wm_name()}")
event.child.kill_client()
#event.child是滑鼠底下的視窗
if event.detail==r_code:
print(f"moving window:{event.child.get_wm_name()}")
event.child.configure(x=100,y=100,width=200,height=300)
#configure(x,y,width,height)
dpy.flush()
"""
if event.type == X.KeyPress:
print("KeyPress!")
print("child =", event.child)
if event.detail==q_code:
print("Q!")
if event.detail == r_code:
print("R!")
dpy.flush()
"""
#記得關num lock