Unfortunately, AFAIK, the APIs for doing this aren't made public yet.
-However-, OmniGroup has extracted the related functions from Darwin, and you can find the code at their site (GDC Examples or something like that).
Look for a file in there called "InputHelpers.h/.m"