有什么方法可以检测ncurses中鼠标/触摸板的水平滚动吗?

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

我正在使用 C 中的 ncurses 库将文件列表打印到 pad。我已经通过检测鼠标事件成功地编写了向上和向下方向的垂直滚动代码。有没有办法也检测水平滚动?

示例代码

#include <ncurses.h>
#include <stdio.h>

#define MAX_FILES 100
#define FILE_NAME_SZ 100


int main() {
    initscr();              
    cbreak();               
    noecho();               
    keypad(stdscr, TRUE);   
    mousemask(ALL_MOUSE_EVENTS, NULL);  

    int pad_ht, pad_width, pad_start_y, pad_start_x, pad_end_y, pad_end_x;
    int pad_scroll_y = 0;
    int matches = MAX_FILES;  
    char filelist[MAX_FILES][FILE_NAME_SZ];
    int ch;
    MEVENT event;
    WINDOW *pad;

    
    /* dummy file list */
    for (int i = 0; i < matches; i++) {
        snprintf(filelist[i], FILE_NAME_SZ, "file_%03d.txt", i);
    }


    /* Initialize pad */
    pad_ht = matches;
    pad_width = 3 * (getmaxx(stdscr) / 4);
    pad_start_y = 0;
    pad_start_x = getmaxx(stdscr) / 4;
    pad_end_x = getmaxx(stdscr);
    pad_end_y = 3 * (getmaxy(stdscr) / 4);

    /* Create pad and enable scrolling */
    pad = newpad(pad_ht, pad_width);
    scrollok(pad, TRUE);

    /* Print list of files into pad and show pad */
    wmove(pad, 0, 0);
    for (int i = 0; i < matches; i++) {
        wprintw(pad, "%d: %s\n", i, filelist[i]);
    }

    prefresh(pad, pad_scroll_y, 0, pad_start_y, pad_start_x, pad_end_y - 1, pad_end_x - 1);

    /* scroll the screen, to see the list displayed */
    /* loop to show pad and detect key events */
    while (true) {
        ch = getch();   

        /* If mouse event detected, check the direction */
        if (ch == KEY_MOUSE) {
            if (getmouse(&event) == OK) {
                /* Scroll up */
                if (event.bstate & BUTTON4_PRESSED) {
                    /* Decrement pad_scroll_y only if within screen limits */
                    if (pad_scroll_y > 0)
                        --pad_scroll_y;
                    prefresh(pad, pad_scroll_y, 0, pad_start_y, pad_start_x, pad_end_y - 1, pad_end_x - 1);
                /* Scroll down */
                } else if (event.bstate & BUTTON5_PRESSED) {
                    /* Increment pad_scroll_y only if within screen limits */
                    if (pad_scroll_y < matches - (pad_end_y - pad_start_y))
                        ++pad_scroll_y;
                    prefresh(pad, pad_scroll_y, 0, pad_start_y, pad_start_x, pad_end_y - 1, pad_end_x - 1);
                }
            }
        } else if (ch == 'q') {
        /* exit if q is pressed */
            break;  
        }
    }

    delwin(pad);
    endwin();  

    return 0;
}

我知道用于水平滚动的逻辑,并且可以使用箭头键来实现它,但我更希望有一种方法可以检测鼠标/触控板上的水平滚动。如果这是不可能的,也许有一种方法可以检测滚动时是否按下了任何修饰键。

c ncurses
1个回答
0
投票

是的!

查看 mousemask (3NCURSES) 的文档以启用报告

BUTTON_SHIFT
状态。

现在,当您收到 b4 或 b5 事件时,查看 Shift 键的状态。如果向下,您应该水平滚动。

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