Since you like to do it progmmatically, I would recommend checking out http://alphababy.sourceforge.net/ and make a fork of that with the bits you need.
If you would do it without a program in-between I would say the hint about using "xinput set-int-prop [device_id] "Device Enabled" 8 0" from Disable Keyboard & Mouse input on unix (under X)Disable Keyboard & Mouse input on unix (under X) would be better, but I understand it as that it is not what you are asking for.