I use command:
sudo kextunload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext
sudo kextload /System/Library/Extensions/AppleUSBTopCase.kext/Contents/PlugIns/AppleUSBTCKeyboard.kext
to disable/enable mac build-in keyboard manually. It seems to unload/load driver for build-in keyboard and works fine.
So how to do it in the code. In my opinion, I needed to do two tasks.
1. unload/load the driver
2. run sudo command
The first task is simple, just use KextManager API will do me favor.
The second one, I found Apple documents say when you want to run sudo authentication task, with the security consideration, you should use something like:
1. launchd
2. SMJobless
3. XPC service
4. etc...
to abstract the high rights task to a sub helper tool run as a launchd process, and use XPC service to communication between main application and this helper tool.
I tried all of these, and helper tool works fine, but when I use
KextManagerUnloadKextWithIdentifier(CFSTR("com.apple.driver.AppleUSBTCKeyboard"));
in the helper tool. It did not work, the build-in keyboard is not disabled properly.
Do I miss something or there is another way to do the job??
解决方案
When I restarted my macbook, it worked... so it is a right way to solve this problem.
If you have another ideas, just comment it.