Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

arrow keys

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pascal Cloup - 13 Feb 2007 20:22 GMT
Hi,

How to intercept th arrows keys in a whole form?

thanks in advance,
Pascal
ClayB - 13 Feb 2007 23:26 GMT
One way ou can do this is to override ProcessCmdKey in your form.

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 {
   Console.WriteLine("ProcessCmdKey " + keyData.ToString());

   return base.ProcessCmdKey(ref msg, keyData);
 }

================
Clay Burch
Syncfusion, Inc.
Deniz Demircioglu - 14 Feb 2007 00:01 GMT
Alternatively, I would recommend setting Form.KeyPreview to true and
overriding OnKeyDown on the Form:

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.KeyData == Keys.Left)
    {
        // Do something
    }
    base.OnKeyDown(e);
}

Signature

Deniz Demircioglu
Developer,
Microsoft Office

> One way ou can do this is to override ProcessCmdKey in your form.
>
[quoted text clipped - 8 lines]
> Clay Burch
> Syncfusion, Inc.
Pascal Cloup - 14 Feb 2007 08:11 GMT
Hi,

thanks to all.

In fact, overiding ProcessCmdKey () works but not OnKeyDown() which is not
called for the arrow keys.

best regards,
Pascal

"Deniz Demircioglu" <DenizDemircioglu@discussions.microsoft.com> a écrit
dans le message de news:
FF98CA15-9875-4FEB-9B80-D3D4068EE7C6@microsoft.com...
> Alternatively, I would recommend setting Form.KeyPreview to true and
> overriding OnKeyDown on the Form:
[quoted text clipped - 20 lines]
>> Clay Burch
>> Syncfusion, Inc.
Deniz Demircioglu - 14 Feb 2007 08:41 GMT
If you set the Form.KeyPreview to true like I said, Form.OnKeyDown would be
called for both arrow and other keys.

One thing I didn't mention is, there is an exception to this rule. If Focus
is on a control deriving from ButtonBase (i.e. Button, RadioButton etc.),
then arrow keys would be used to set the focus to previous (with left and up
arrow keys) and to next (with right and down arrow keys) control. In this
case, Form.OnKeyDown is not called since these keystrokes are interpreted as
Shift+Tab and Tab, respectively.

The approach I'm describing is the preferred way for a container form to
catch some key strokes on its children controls. If you go to documentation
for Control.ProcessCmdKey at msdn:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.processcmd
key.aspx


You will see it says:
"Controls will seldom, if ever, need to override this method."

But if the exception I described above is something you don't want,
overriding ProcessCmdKey may be the only way to go. If that's the case, I
would recommend reading the documentation and returning correct values from
this method as told there.

Signature

Deniz Demircioglu
Developer,
Microsoft Office

> Hi,
>
[quoted text clipped - 33 lines]
> >> Clay Burch
> >> Syncfusion, Inc.
ClayB - 14 Feb 2007 11:50 GMT
Another solution that has worked for me is to have your form implement
IMessageFilter and call Application.AddMessageFilter(this); to hook up
the filter. This will also catch the arrow keys even when buttons (or
grids, or other things that eat arrows) are present.

=================
Clay Burch
Syncfusion, Inc.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.