Is there any way to detect horizontal scrolling for a mouse/touchpad in ncurses?

I’m printing a list of files to a pad using the ncurses library in C. I’ve already managed to code vertical scrolling for both up and down directions by detecting mouse event. Is there some way to detect horizontal scrolling as well?

Example Code

#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: %sn", 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;
}

I’m aware of the logic used for horizontal scrolling and can implement it using arrow keys, but I’d prefer if there was a way to detect horizontal scrolling on the mouse/track pad. If that’s not possible, maybe a way to detect if any modifier key is pressed while scrolling.

1

No, you cannot do this (barring a lot of work):

  • ncurses mouse support uses the escape sequences sent by xterm, encoding those into a 32-bit mmask_t.
  • vertical scrolling is well established as button events 4 and 5 (this is not the same thing as the physical buttons on your mouse, but the way X reports them).
  • horizontal scrolling probably (there is no existing documentation) is buttons 6 and 7.
  • the existing 5 buttons and modifiers use 30 bits of that 32-bit mask. bits)
  • adding two more buttons would require another 10 bits.

Changing that mmask_t to be 64 bits for example would change the binary interface. Going from the previous ABI 5 to ncurses 6 was a lot of work by all involved.

One could decode the mouse escape sequences directly, but that also would be a lot of work.

Other than adding a new function (for the rare developer who is able to manipulate a wheel mouse in this fashion), the possibility remains to build an ABI 7 ncurses which would provide the 64-bit mmask_t. That much was done in May 2020.

EDIT:
Thomas Dickey knows what he is talking about, and I assume he understands the question better than I do. I leave my original response as follows:

Yes!

Look at the documentation for mousemask (3NCURSES) to enable the BUTTON_SHIFT state being reported.

Now when you get the b4 or b5 events, look at the state of the Shift key. If down, you should be scrolling horizontally.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật