how can i run script when keyboard detached?

I followed eskimo answer on how to run script when usb device connected. But how about when it is disconnected? Do we have some event in iokit that handle this? Thank you


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>com.example.KeyboardAttach</string>

<key>ProgramArguments</key>

<array>

<string>/usr/local/bin/xpc_set_event_stream_handler</string>

<string>/usr/local/bin/keymapRazer</string>

</array>

<key>LaunchEvents</key>

<dict>

<key>com.apple.iokit.matching</key>

<dict>

<key>com.example.KeyboardAttach.Event</key>

<dict>

<key>idVendor</key>

<integer>5426</integer>

<key>idProduct</key>

<integer>545</integer>

<key>IOProviderClass</key>

<string>IOUSBDevice</string>

<key>IOMatchLaunchStream</key>

<true/>

</dict>

</dict>

</dict>

</dict>

</plist>

Replies

The basic idea for

LaunchEvents
is that you use it to get your daemon started, and then you deal with the actual events with code. So, in your case I’d use
LaunchEvents
to learn about the keyboard getting plugged in and then write I/O Registry code to monitor its ongoing presence.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

how do i know what happen behind when usb device get connected or disconnected? which event happen?

The I/O Registry has a bunch of APIs for interrogating the structure of the registry and being notified of changes therein. See Accessing Hardware From Applications for all the details.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"