Key binding by file type

Can anyone help me create a key binding that behaves differently based on the type of file that it’s being used in? Here is the stackoverflow for more information: