简体   繁体   English

命令行C和键盘快捷键(在Mac OS中)

[英]Command-line C & keyboard shortcut (in Mac OS)

How can I execute a command when user press a key in a command-line C application? 当用户在命令行C应用程序中按键时,如何执行命令? Is it possible when window isn't focused 窗口不聚焦时有可能吗

Depends on the program itself, you could do either of those: 根据程序本身,您可以执行以下任一操作:

  1. block on unbuffered getc you get the key strokes as they come and not when users hits return. 在无缓冲的getc上阻塞时,您会在击键时获得击键,而不是在用户点击返回时击键。

  2. create some sort of event loop, using select/epoll or event framework like ( libevent / libev ) and get a callback whenever a user hits a key. 使用select / epoll或( libevent / libev )之类的事件框架创建某种事件循环,并在用户按下任何键时获取回调。

  3. use a toolkit like ncurses which provides a pseudo graphical command line interface and an event loop. 使用ncurses之类的工具包,该工具包提供伪图形命令行界面和事件循环。

  4. if the keys you're interesting in capturing are things like CTRL + C , you need signal handlers. 如果您对捕获感兴趣的键是CTRL + C之类的 ,则需要信号处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM