c - 按下键时多次执行功能(ncurses)
问题描述
我希望能够execlp(...)
在按下右键时多次运行该功能(这只适用于mac,它基本上用于oasascript
按下相应的键来增加亮度,因为我知道这些特殊键不能用ncurses模拟)。
程序编译并运行,但是当我按右键时,它会增加一次亮度然后中断。我想让它继续运行,所以当再次按下右键以在按住键的同时重复操作时。
#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();
}
解决方案
就其性质而言,您不能execlp()
多次运行,但您可以将其替换fork()
为等。但我的偏好是寻找一个直接的 API 解决方案来控制亮度,而不是启动一个单独的进程来伪造按键。为此,我发现了这个:在 Mac OS X 应用程序中调整屏幕亮度