change keyframe tangents on selected keys

hi, i am trying to create a script that will change the tangents on existing keyframes selected in the timeline.
i figured out how to do this on all the keyframes, but i need a command to switch the tangents on only the selected keyframes.
can anyone help me? bare in mind i am a noob but would be grateful if someone could explain this.. thanks!