Connect with Expertise | Find Experts, Get Answers, Share Insights

Arrow Keys neglected on User Control

Martijn Mulder
 
Posts: n/a
#1: Oct 26 '06
/*
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 <Altor <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 <Altor <Ctrland press the Arrow Up Key...";
break;
case System.Windows.Forms.Keys.Up:
label.Text+="\nYou pressed the Arrow Up Key."+
"\n\nNow release <Altor <Ctrland press the Arrow Up Key
again..."+
"\n\nIt doesn't work!"+
"\nPress <Escor <Spaceto 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);
}
}




Peter Thornqvist
 
Posts: n/a
#2: Oct 27 '06

re: Arrow Keys neglected on User Control


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);
}

}

--

Regards, Peter

"Martijn Mulder" <i@mskrev i meddelandet
news:454130e1$0$77254$dbd49001@news.wanadoo.nl...
/*
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 <Altor <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 <Altor <Ctrland press the Arrow Up Key...";
break;
case System.Windows.Forms.Keys.Up:
label.Text+="\nYou pressed the Arrow Up Key."+
"\n\nNow release <Altor <Ctrland press the Arrow Up Key
again..."+
"\n\nIt doesn't work!"+
"\nPress <Escor <Spaceto 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);
}
}
>
>
>

Closed Thread