简体   繁体   English

如何在 C++ 上制作全局键盘监听器? (Mac OSX)

[英]How do I make a global keyboard listener on C++? (Mac OSX)

I want to get global keyboard input for an application I am making which runs in the background.我想为我正在制作的在后台运行的应用程序获取全局键盘输入。

Expected results include being able to print the character in the console when I press a keyboard key outside of the window that the application is running on (in this case, Terminal).预期的结果包括当我按下应用程序正在运行的 window 之外的键盘键(在本例中为终端)时,能够在控制台中打印字符。

I have tries using ncurses.h and curses.h but they seem to have symbols which are not recognised by Xcode:我尝试使用ncurses.hcurses.h但它们似乎有 Xcode 无法识别的符号:

Undefined symbols for architecture x86_64:
  
  "_stdscr", referenced from:
      _main in main.o

  "_wgetch", referenced from:
      _main in main.o

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here's my code:这是我的代码:

#include <ApplicationServices/ApplicationServices.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreFoundation/CoreFoundation.h>
#include <stdio.h>
#include <ncurses.h>
#include <iostream>
#include <curses.h>

int main() {
    std::cout << getch();
    
}

Can anyone suggest a library contained in the OS X files that can help me?任何人都可以建议一个包含在 OS X 文件中的库来帮助我吗? Or if possible, can anybody help me with figuring out how to get past the errors above?或者如果可能的话,有人可以帮我弄清楚如何克服上述错误吗?

edit编辑

I am a second-time poster (my first one went wrong), please point out if I can improve this or if I should delete this question altogether.我是第二次发帖(我的第一次发错了),请指出我是否可以改进这个问题或者我是否应该完全删除这个问题。

You need to specifically link the library, ie:您需要专门链接库,即:

c++ -oprogname progname.cpp -lcurses

However, this will only resolve the "Undefined symbols" errors;但是,这只会解决“未定义符号”错误; it won't get you a "global keyboard listener".它不会让你成为“全球键盘监听器”。

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

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