0

I would like to be able to run the function execlp(...) more than once when pressing the right key (this works only on mac, it basically uses oasascript to press the corresponding key for increasing the brightness as I know these special keys can't be simulated with ncurses).

The program compiles and runs, but when I press the right key, it increases the brightness once then interrupts. I would like to keep it running so when the right key is pressed again to repeat the action while the key is kept pressed.

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

int main(int argc, char *argv[]) {
  int key = 0;

  initscr();
  noecho();
  curs_set(TRUE);
  keypad(stdscr, TRUE);
  nodelay(stdscr, TRUE);

  while(1) {
    clear();

    refresh();
    key = getch();

    if (key == KEY_RIGHT) {
      execlp(
        "osascript",
        "osascript",
        "-e",
        "tell application \"System Events\" to key code 144 using {option down, shift down}",
        NULL
      );
    }
  }
  endwin();
}
skamsie
  • 2,614
  • 5
  • 36
  • 48

1 Answers1

0

By its nature, you can't run execlp() more than once, but you can replace it with fork() or the like. But my preference would be to look for a direct API solution to control the brightness, rather than launching a separate process to fake a keypress. To that end, I found this: Adjust screen brightness in Mac OS X app

William McBrine
  • 2,166
  • 11
  • 7
  • Thanks, but you are pointing to a `swift` solution and I am intersted in a `C` implementation. There is already a nice low level implementation for in `C` as well here: https://github.com/nriley/brightness, but this has the caveat of not updating the brightness slider to the value you are setting via the app. Anyway, in my case I have solved it by using `system()` instead of `execlp()` – skamsie Dec 28 '20 at 17:08