I've been use an addon called Bindpad for that function. It's really easy to use and you don't have to bother with scripts.