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

How To Detect Shift+Arrow Key

P: n/a
/*
I override IsInputKey() to direct the Arrow Keys (Cursor Keys) to my
custom System.Windows.Forms.Control. But, holding down the Shift-Key
prevents the Arrow Keys from coming through.

How can I intercept the Arrow Keys when holding down the Shift Key?
*/

//class KeyTest
class KeyTest:System.Windows.Forms.Control
{
//constructor
KeyTest()
{
KeyDown+=OnKeyDown;
Paint+=OnPaint;
Dock=System.Windows.Forms.DockStyle.Fill;
}
//IsInputKey
protected override bool IsInputKey(System.Windows.Forms.Keys a)
{
return
(a==System.Windows.Forms.Keys.Down)||
(a==System.Windows.Forms.Keys.Left)||
(a==System.Windows.Forms.Keys.Right)||
(a==System.Windows.Forms.Keys.Up)
?
true
:
base.IsInputKey(a);
}
//OnKeyDown
void OnKeyDown(object a,System.Windows.Forms.KeyEventArgs b)
{
switch(b.KeyCode)
{
case System.Windows.Forms.Keys.Left:
System.Console.Beep();
break;
case System.Windows.Forms.Keys.Right:
System.Console.Beep();
break;
case System.Windows.Forms.Keys.Up:
System.Console.Beep();
break;
case System.Windows.Forms.Keys.Down:
System.Console.Beep();
break;
case System.Windows.Forms.Keys.Escape:
System.Windows.Forms.Application.Exit();
break;
}
}
//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
b.Graphics.DrawString
(
"Press an Arrow Key to hear a Beep. "+
"Hold down the Shift Key and the Arrow Keys no longer Beep. "+
"How can I solve this?"+
"\n\nPress <Escto quit...",
new System.Drawing.Font
(
"Times New Roman",
26,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Pixel
),
System.Drawing.Brushes.Black,
new System.Drawing.Rectangle
(
new System.Drawing.Point(0,0),
new System.Drawing.Size(Width,Height)
)
);
}
//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Form form=new System.Windows.Forms.Form();
form.Text="Shift+Arrow Keys";
form.Controls.Add(new KeyTest());
System.Windows.Forms.Application.Run(form);
}
}


Nov 1 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.