Vertebrate hair cells are responsible for the high fidelity encoding of mechanical stimuli into trains of action potentials (spikes) in afferent neurons. Here, we generated a transgenic zebrafish line expressing Channelrhodopsin-2 (ChR2) under the control of the hair-cell specific myo6b promoter, in order to examine the role of the mechanoelectrical transduction (MET) channel in sensory encoding in afferent neurons. We performed in vivo recordings from afferent neurons of the zebrafish lateral line while activating hair cells with either mechanical stimuli from a waterjet or optical stimuli from flashes of ~470-nm light. Comparison of the patterns of encoded spikes during 100-ms stimuli revealed no difference in mean first spike latency between the two modes of activation. However, there was a significant increase in the variability of first spike latency during optical stimulation as well as an increase in the mean number of spikes per stimulus. Next, we compared encoding of spikes during hair-cell stimulation at 10, 20, and 40-Hz. Consistent with the increased variability of first spike latency, we saw a significant decrease in the vector strength of phase-locked spiking during optical stimulation. These in vivo results support a physiological role for the MET channel in the high fidelity of first spike latency seen during encoding of mechanical sensory stimuli. Finally, we examined whether remote activation of hair cells via ChR2 activation was sufficient to elicit escape responses in free-swimming larvae. In transgenic larvae, 100-ms flashes of ~470-nm light resulted in escape responses that occurred concomitantly with field recordings indicating Mauthner cell activity. Altogether, the myo6b:ChR2 transgenic line provides a platform to investigate hair-cell function and sensory encoding, hair-cell sensory input to the Mauthner cell, and the ability to remotely evoke behavior in free-swimming zebrafish.