There are two approaches:
1) For the piece in question you're considering to move, look at the vector going through it to the king. For example, if they king and the piece are separated horizontally/vertically, look at the line going through it to the king. Is there an enemy rook/queen on this line, such that your piece is currently between it and your king? Or, if the piece is on a diagonal from the king, see if there's an enemy bishop/queen on this diagonal. However, if your piece moves along this vector (and thus continues blocking your king), it's fine. For example, assume your king is on h8, your queen is on e5, and the opponent's bishop is on a1. There's an enemy piece on the a1-h8 vector (which your queen and king are on), so moving the queen off this vector/diagonal is illegal. But moving the queen along the vector (e.g., anywhere from b2 to g7) won't violate the pin.
2) The simpler approach is a general purpose illegal move checker. Make a list of all pseudo-legal moves, and then check each to see if it results in an illegal position where your king is en prise. This encompasses what you're trying to do with not moving a pinned piece out of the way, since it would result in an illegal position.