Is there a way to play a system beep on Mac OS?

后端 未结 2 1724
梦谈多话
梦谈多话 2020-12-21 00:17

Is there a way to play a system beep on Mac OS using C++ and Xcode? I understand that I need to use a library. Is there a library that works across both the Mac and Windows

相关标签:
2条回答
  • 2020-12-21 01:02

    I think you probably want to use NSBeep.


    NSBeep

    Plays the system beep.

    #include <AppKit/AppKit.h>
    
    void NSBeep (void);
    

    This seems to work OK for a command line tool:

    #include <AppKit/AppKit.h>
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        cout << "Hello world !" << endl;
        NSBeep ();
        return 0;
    }
    
    $ g++ -Wall -framework AppKit beep.cpp -o beep
    $ ./beep
    
    0 讨论(0)
  • 2020-12-21 01:05

    The cross platform way to play a beep is std::cout << "\007";. I had been trying to play it by passing in a char and then decrementing until 7. That didn't work. Explicitly outputting the code did work though.

    0 讨论(0)
提交回复
热议问题