Arrow Keys neglected on User Control

M

Martijn Mulder

/*
I have problems detecting the Arrow Keys on a User Control. A control
derived from System.Windows.Forms.Control neglects 'bare' Arrow Keys
but does react on the combination <Alt> or <Ctrl> + Arrow Key. The code
below shows what I mean. How can I cure this? (excuse me for the line
breaks)
*/




//class KeyTest
class KeyTest:System.Windows.Forms.Control
{


//data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();


//constructor
KeyTest()
{
Controls.Add(label);
KeyDown+=OnKeyDown;
Text="Arrow Keys are Special?";
label.Text="\nPress <1>...";
Dock=System.Windows.Forms.DockStyle.Fill;
label.Dock=System.Windows.Forms.DockStyle.Fill;
}


//OnKeyDown
void OnKeyDown(object a,System.Windows.Forms.KeyEventArgs b)
{
switch(b.KeyCode)
{
case System.Windows.Forms.Keys.D1:
label.Text+="\nYou pressed 1."+
"\n\nNow hold down <Alt> or <Ctrl> and press the Arrow Up Key...";
break;
case System.Windows.Forms.Keys.Up:
label.Text+="\nYou pressed the Arrow Up Key."+
"\n\nNow release <Alt> or <Ctrl> and press the Arrow Up Key
again..."+
"\n\nIt doesn't work!"+
"\nPress <Esc> or <Space> to Quit";
break;
case System.Windows.Forms.Keys.Escape:
System.Windows.Forms.Application.Exit();
break;
case System.Windows.Forms.Keys.Space:
System.Windows.Forms.Application.Exit();
break;
}
}


//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Form form=new System.Windows.Forms.Form();
form.Controls.Add(new KeyTest());
System.Windows.Forms.Application.Run(form);
}
}
 
P

Peter Thornqvist

override IsInputKey and do something like this:

protected override bool IsInputKey(System.Windows.Forms.Keys
keyData)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Up:
return true;
case Keys.Down:
return true;
case Keys.Right:
return true;
case Keys.Left:
return true;
default:
return base.IsInputKey(keyData);
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top