Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i use the keyboard right and left arrow keys in my application instead of click the
yes or no buttons
Posted

The KeyDown event as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx[^]
can be used for this purpose.
To capture the key pressed before it is passed on to any control on the form,
KeyPreview property of Form can be set to True, which indicates that the form will receive key events before the event is passed to the control that has focus as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx[^]
 
Share this answer
 
hey,

try this
C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == Keys.Left) {
            Console.WriteLine("left");
            return true;
        }
        // etc..
        return base.ProcessCmdKey(ref msg, keyData);
    }


Best Luck
 
Share this answer
 
Comments
siri2012 24-Apr-12 6:04am    
private void Form1_KeyDown(object sender, KeyEventArgs e)
{

if (e.KeyCode = Keys.Left)//getting error here
{
System.Windows.Forms.MessageBox.Show("left key pressed");
}
else if (e.KeyCode = Keys.Right) //getting error here
{
System.Windows.Forms.MessageBox.Show("right key pressed");
}

// Keys.Left -

-Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'bool'

//e.KeyCode

--Property or indexer 'System.Windows.Forms.KeyEventArgs.KeyCode' cannot be assigned to -- it is read only
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900