iT邦幫忙

2026 iThome 鐵人賽

DAY 8
0
Software Development

用python做一個X11 WM系列 第 8

Day 8|試著聚焦視窗

  • 分享至 

  • xImage
  •  

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

上一篇
Day 7|試著移動和調整視窗大小
下一篇
Day 9|試著用滑鼠移動視窗
系列文
用python做一個X11 WM14
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言