简体   繁体   中英

Key down event on the slider in C#

I wanted to handle Arrow key press event on the slider control. I tried googling for it but almost all the links gave me information about handling it on the windows(overrideing WndProc or ProcessCmdKey).The KeyDwon and Key Up events aren't fired for the Arrow press. How can i handle it?

Look here .

Here's a short quote from there:

Certain keys, such as the TAB, RETURN, ESC, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true. Instead of overriding the IsInputKey method, you can handle the PreviewKeyDown event and set the IsInputKey property to true. For a code example, see the PreviewKeyDown event.

And here's the code sample from the PreviewKeyDown event from here (PreviewKeyDown) :

// By default, KeyDown does not fire for the ARROW keys
void button1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Down:
        case Keys.Up:
            if (button1.ContextMenuStrip != null)
            {
                button1.ContextMenuStrip.Show(button1,
                    new Point(0, button1.Height), ToolStripDropDownDirection.BelowRight);
            }
            break;
    }
}

// PreviewKeyDown is where you preview the key.
// Do not put any logic here, instead use the
// KeyDown event after setting IsInputKey to true.
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Down:
        case Keys.Up:
            e.IsInputKey = true;
            break;
    }
}

I'm presuming you're using a track bar control when you say slider control? If not, then this answer probably won't help.

Anyway, you need to set the OnKeyDown event for your track bar control. Something as simple as the following code will allow the user to use the left and right arrows to move from side to side.

private void trackBar1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Right) && (trackBar1.Value < trackBar1.Maximum))
                trackBar1.Value += 1;

            if ((e.KeyCode == Keys.Left) && (trackBar1.Value > trackBar1.Maximum))
                trackBar1.Value -= 1;
        }

You simply need to detect a key press, and then decide whether it's a left or right arrow, and then what to do from there.

I've tried it and the left and right arrows do trigger it for me. Again, if you're using a different slider control (there isn't any control called the slider control, so I'm assuming track bar) then it may be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM