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

发布于 2024-09-08 15:22:08 字数 46 浏览 7 评论 0原文

当用户在命令行 C 应用程序中按下某个键时,如何执行命令?窗口未聚焦时是否可以

How can I execute a command when user press a key in a command-line C application? Is it possible when window isn't focused

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2024-09-15 15:22:08

根据程序本身,您可以执行以下任一操作:

  1. 阻止无缓冲的 getc 您会在按键出现时获得按键,而不是在用户按回车键时获得。

  2. 使用 select/epoll 或事件框架创建某种事件循环,例如 (libevent /libev) 并在用户按下按键时获取回调。

  3. 使用像ncurses这样的工具包,它提供了伪图形命令行界面和事件

  4. 如果您有兴趣捕获的键是 CTRL+C 之类的内容,则需要信号处理程序。

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.

  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.

  3. use a toolkit like ncurses which provides a pseudo graphical command line interface and an event loop.

  4. if the keys you're interesting in capturing are things like CTRL+C, you need signal handlers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文