Simulate a key being pressed, which is C/C++, not OpenCV: http://stackoverflow.com/questions/5607849/how-to-simulate-a-key-press-in-c
And do this inside a loop that is monitoring your mouse events, have a look at the opencv_annotation code for example: https://github.com/opencv/opencv/blob/master/apps/annotation/opencv_annotation.cpp