在屏幕上的任何地方聆听按键

问题描述 投票:0回答:0

我正在使用 x11rb,X11 Rust 绑定。我修改了this教程中的一些代码,这样我就可以听到来自

parent_win
screen.root
)而不是
win

的按键
use std::error::Error;
use x11rb::connection::Connection;
use x11rb::protocol::xproto::*;
use x11rb::protocol::Event;
use x11rb::COPY_DEPTH_FROM_PARENT;

fn print_modifiers(mask: x11rb::protocol::xproto::KeyButMask) {
    println!("Modifier mask: {:#?}", mask);
}

fn main() -> Result<(), Box<dyn Error>> {
    // Open the connection to the X server. Use the DISPLAY environment variable.
    let (conn, screen_num) = x11rb::connect(None)?;

    // Get the screen #screen_num
    let screen = &conn.setup().roots[screen_num];

    // Ask for our window's Id
    let win = conn.generate_id()?;
    let parent_win = screen.root;

    println!("win: {}", win);
    println!("parentWin: {}", parent_win);

    // NOTE: This window is not being used
    let values = CreateWindowAux::default().background_pixel(screen.white_pixel);
    conn.create_window(
        COPY_DEPTH_FROM_PARENT,    // depth
        win,                       // window Id
        screen.root,               // parent window
        0,                         // x
        0,                         // y
        150,                       // width
        150,                       // height
        10,                        // border_width
        WindowClass::INPUT_OUTPUT, // class
        screen.root_visual,        // visual
        &values,
    )?;

    let values = ChangeWindowAttributesAux::default().event_mask(EventMask::KEY_PRESS);

    conn.change_window_attributes(screen.root, &values)?;

    // Map the window on the screen
    conn.map_window(win)?;
    conn.flush()?;

    loop {
        let event = conn.wait_for_event()?;
        match event {
            Event::KeyPress(event) => {
                print_modifiers(event.state);
                println!("Key pressed in window {}", event.event);
            }
            _ => {
                // Unknown event type, ignore it
                println!("Unknown event: {:?}", event);
            }
        }
    }
}

我认为如果我在屏幕上的任何地方按下按键,这将使

println!("Key pressed in window {}", event.event);
在终端中记录一些东西。但是没有任何记录。

正确的做法是什么?

铁锈游乐场

rust x11
© www.soinside.com 2019 - 2024. All rights reserved.