Despite recent advances in optogenetics, it remains challenging to manipulate gene expression in specific populations of neurons. We present a dual-protein switch system, Cal-Light, that translates neuronal-activity-mediated calcium signaling into gene expression in a light-dependent manner. In cultured neurons and brain slices, we show that Cal-Light drives expression of the reporter EGFP with high spatiotemporal resolution only in the presence of both blue light and calcium. Delivery of the Cal-Light components to the motor cortex of mice by viral vectors labels a subset of excitatory and inhibitory neurons related to learned lever-pressing behavior. By using Cal-Light to drive expression of the inhibitory receptor halorhodopsin (eNpHR), which responds to yellow light, we temporarily inhibit the lever-pressing behavior, confirming that the labeled neurons mediate the behavior. Thus, Cal-Light enables dissection of neural circuits underlying complex mammalian behaviors with high spatiotemporal precision.