Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mouse input x, y is absolute not relative to window winapi #278

Closed
TimonPost opened this issue Oct 20, 2019 · 0 comments · Fixed by crossterm-rs/crossterm-input#11
Closed
Milestone

Comments

@TimonPost
Copy link
Member

The y position in with a mouse event is not relative to the window but absolute to screen buffer. This means that when the mouse cursor is at top left it will be x: 0, y: 2295 (y = number of cells counting from the top of the buffer) instead of 0,0 to the window.

image

As you can see the cursor is at: x: 0, y: 2858, although I have the mouse at the top left. We can calculate the relative cursor position very easily. With winapi you can request the current window positions, this structure contains the top, left, bottom, right properties of were the window is in the screen buffer.

We can calculate the absolute with the following:

    let xpos = event.mouse_position.x;
    let ypos = event.mouse_position.y;

    let buffer = ScreenBuffer::current().unwrap().info().unwrap();
    let window_size = buffer.terminal_window();

    println!("windows size: {:?}", window_size);
    println!("absolute mouse position: x: {:?}, y: {:?}", xpos, ypos);
    println!("relative mouse position: x: {:?}, y: {:?}", xpos, ypos - window_size.top);
@TimonPost TimonPost changed the title mouse input y position is absolute not relative to screen mouse input y position is absolute not relative to screen winapi Oct 20, 2019
@TimonPost TimonPost changed the title mouse input y position is absolute not relative to screen winapi mouse input x, y is absolute not relative to window winapi Oct 20, 2019
@TimonPost TimonPost added this to the 0.13 milestone Oct 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant