How some applications accept mouse click in bash over ssh?

IMHO, the simplest way to write such a TUI application is to use ncurses.

"New Curses" is a library that abstracts the design of the TUI from the details of the underlying device. All the software you cited use ncurses to render their interface.

When you click on a terminal emulator (e.g. xterm, gnome-term, etc), the terminal emulator translates the click in a sequence of ANSI Escape codes. These sequences are read and translated in events by the ncurses library.

You can find an example on Stack Overflow: Mouse movement events in NCurses


It's pretty simple, in a old school serial terminal session, the mouse was not really an option for sending input to the interactive shell session. (I am sure there were some early exceptions.)

However when you are running a terminal emulator inside a graphical desktop session the terminal emulator can be mouse aware.

What happens on a high level is that the mouse events are translated by the terminal emulator (xterm for example) into terminal sequences and those are sent to the interactive shell session the application you are running, htop, vim whatever. In this case vim just sees the terminal sequence (like a simple series of keyboard events would produce), not the mouse event.