1

i am trying to learn ncurses library and i came up with code below:

#include <ncurses.h>
#include <stdlib.h>
#include <signal.h>

static void finish(int sig);

int main(int argc, char** argv) {

    char c;
    initscr();
    raw();
    keypad(stdscr, TRUE);
    noecho();

    (void) signal(SIGINT, finish);      /* arrange interrupts to terminate */

    printw("Type any character to see it in bold:\n");
    refresh();
    c = getch();

    /* work around for ctrl+c */
    if(c == 3)
        finish(0);

    while(c != KEY_F(1))
    {
        printw("The pressed key is ");
        attron(A_BOLD);
        printw("%c\n", c);
        attroff(A_BOLD);
        refresh();
        c = getch();

        /* work around for ctrl+c */
        if(c == 3)
            finish(0);

        printf("Code = %d\n", c);
    }

    printw("F1 key pressed.\n");
    endwin();

    return (EXIT_SUCCESS);
}

static void finish(int sig)
{
    endwin();

    /* do your non-curses wrapup here */

    exit(0);
}

The problem in this code is when i press F1 key, terminal help window opens and i can't catch F1 key press. Also i can't catch ctrl+c press by signal mechanism. Is there any way to override F1 key on terminal and how can i use signals in curses mode.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112

2 Answers2

1

In the terminal window's menu bar, Edit -> Preferences. Go to the Shortcuts tab. Clear the conflicting shortcuts.

Better yet, don't use any shortcuts that conflict with the terminal emulator's pre-existing shortcuts.

You should probably leave SIGINT alone. ncurses already intercepts it to clean up the terminal before exit. If you need to run some cleanup code of your own, try the atexit function.

kenm
  • 23,127
  • 2
  • 43
  • 62
  • atexit() function is not i am looking for actually. I want to intterupt and end my promram with ctrl+c. So i still need SIGINT, do you know any solition? – Olcay Ertaş Jul 25 '11 at 12:43
  • Comment out your call to the `raw` function. That intercepts the ctrl-c. Without it, ctrl-c interrupts the program normally and calls the function registered with `atexit`. – kenm Jul 25 '11 at 13:09
  • KEY_F(1) macro returns 265 but when i pres F1, variable c get value of 9. – Olcay Ertaş Jul 25 '11 at 13:10
  • You've got `c` defined as a `char`, but 265 is too large to store in a `char`. Define it as an `int`. You can cast it to a character after determining whether it is actually a character. – kenm Jul 25 '11 at 15:04
  • Thank you, i have noticed that before you write. – Olcay Ertaş Jul 25 '11 at 16:22
0

I have solution in c / c++ both

inline void signal_callback_handler(int signum){signal(SIGINT, signal_callback_handler);}

put this as global

make a call below in main()

signal(SIGINT, signal_callback_handler);

don't forget to include stdio.h and signal.h

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
hr097
  • 1
  • 1
  • 4
  • I find it helpful to include working code and an explanation with my answer. Your answer, in my opinion, isn't very clear and could use some formatting and additional detail. – Tim Jan 05 '22 at 13:39