命令行 C&键盘快捷键(在 Mac OS 中)
当用户在命令行 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据程序本身,您可以执行以下任一操作:
阻止无缓冲的 getc 您会在按键出现时获得按键,而不是在用户按回车键时获得。
使用 select/epoll 或事件框架创建某种事件循环,例如 (libevent /libev) 并在用户按下按键时获取回调。
使用像ncurses这样的工具包,它提供了伪图形命令行界面和事件
如果您有兴趣捕获的键是 CTRL+C 之类的内容,则需要信号处理程序。
Depends on the program itself, you could do either of those:
block on unbuffered getc you get the key strokes as they come and not when users hits return.
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.
use a toolkit like ncurses which provides a pseudo graphical command line interface and an event loop.
if the keys you're interesting in capturing are things like CTRL+C, you need signal handlers.