With lua + autohotkey scripts in windows this is definitely possible the last time I checked. You run a luascript on another keyboard mapped to a generic key, luascript inputs that as the proper text-based language
Mac, I can only assume its something you would write in applescript. But maybe you find some interesting ideas there. I found something called "http://www.orderedbytes.com/controllermate/" that might be of interest to you
http://www.oblita.com/interception