I am making a project where i am drawing waveform of an audio file in C#. Currently I am using mouse drag and drop for a part of waveform selection. But now i want that the waveform should also be selected by using following: click at a point press shift and click again on another point. I dont have much knowledge about keybo开发者_运维问答ard events. So need help in this.
Use the Control.ModifierKeys property to detect whether the Shift key is pressed. Sample code:
private void panel1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
if (Control.ModifierKeys == Keys.Shift) SetSelectionEnd(e.X);
else SetSelectionStart(e.X);
}
}
Implementing the SetSelectionStart/End is up to you.
The KeyUp
event won't work for this, because although the KeyEventArgs
argument for this event includes a Shift
property that indicates whether or not the shift key is currently down, the event is not triggered when you only press the shift key (and not any other key). The KeyPress
event is also not triggered just by the shift key.
Fortunately, the PreviewKeyDown
event is exactly what you need for this. The PreviewKeyDownEventArgs
argument includes a Shift
property (also Control
for the ctrl key), and the event is fired when you just click the shift button.
Also, PreviewKeyDown
is triggered on a Form
regardless of whether that form's KeyPreview
is set to true or not, which is handy.
精彩评论