Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:50:23+00:00 2026-06-01T03:50:23+00:00

How can I get a simple call back when a HID device, or at

  • 0

How can I get a simple call back when a HID device, or at last, any USB/Bluetooth device gets connected/disconnected?

I made a simple app that shows the connected joysticks and the pressed buttons/axis for mac in a pretty way. Since I am not very familiar with cocoa yet, I made the UI using a webview, and used the SDL Joystick library.
Everything is working nice, the only problem is that the user needs to scan for new joysticks manually if he/she connects/disconnects something while the program is running.

With a callback, I I can just call the Scan function. I don’t want to handle the device or do something fancy, just know when there is something new happening…

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:50:24+00:00Added an answer on June 1, 2026 at 3:50 am

    Take a look at IOServiceAddMatchingNotification() and related functions. I’ve only worked with it in the context of serial ports (which are in fact USB to serial adapters, though that doesn’t matter), but it should be applicable to any IOKit accessible device. I’m not sure about Bluetooth, but it should at least work for USB devices. Here’s a snippet of code I use:

    IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), 
                   IONotificationPortGetRunLoopSource(notificationPort), 
                   kCFRunLoopDefaultMode);
    
    CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOSerialBSDServiceValue);
    CFRetain(matchingDict); // Need to use it twice and IOServiceAddMatchingNotification() consumes a reference
    
    CFDictionaryAddValue(matchingDict, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDRS232Type));
    
    io_iterator_t portIterator = 0;
    // Register for notifications when a serial port is added to the system
    kern_return_t result = IOServiceAddMatchingNotification(notificationPort,
                                                            kIOPublishNotification,
                                                            matchingDictort,
                                                            SerialDeviceWasAddedFunction,
                                                            self,           
                                                            &portIterator);
    io_object_t d;
    // Run out the iterator or notifications won't start (you can also use it to iterate the available devices).
    while ((d = IOIteratorNext(iterator))) { IOObjectRelease(d); }
    
    // Also register for removal notifications
    IONotificationPortRef terminationNotificationPort = IONotificationPortCreate(kIOMasterPortDefault);
    CFRunLoopAddSource(CFRunLoopGetCurrent(),
                       IONotificationPortGetRunLoopSource(terminationNotificationPort),
                       kCFRunLoopDefaultMode);
    result = IOServiceAddMatchingNotification(terminationNotificationPort,
                                              kIOTerminatedNotification,
                                              matchingDict,
                                              SerialPortWasRemovedFunction,
                                              self,         // refCon/contextInfo
                                              &portIterator);
    
    io_object_t d;
    // Run out the iterator or notifications won't start (you can also use it to iterate the available devices).
    while ((d = IOIteratorNext(iterator))) { IOObjectRelease(d); }
    

    My SerialPortDeviceWasAddedFunction() and SerialPortWasRemovedFunction() are called when a serial port becomes available on the system or is removed, respectively.

    Relevant documentation is here, particularly under the heading Getting Notifications of Device Arrival and Departure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a simple menu function which can call it example get_menu()
Can I get a complete simple scenario i.e. tutorial that suggest how this should
This somehow simple task is not so simple. I can get the number of
Seems simple, but not for me. I can get the current year with: <jsp:useBean
I've set up a very simple example with LINQ-TO-SQL in WPF. I can get
I know this is rather laughable, but I can't seem to get simple C++
I can't seem to get a simple program (with lots of memory access) to
I can not get to make this comparison in this simple code error ..
I can't get past this issue I am having. Here's a simple example: class
Here's what I want to do. It seems simple, but I can't get it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.