By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Arrow Keys neglected on User Control

P: n/a
/*
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);
}
}

Oct 26 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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:45***********************@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);
}
}

Oct 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.